info = { type = "image", title = "Pulse watch", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "LCD watch with a beating heart and ECG trace", api = 33, } local tr = ({ sk = { ["Backlight"] = "Podsvietenie", ["Green"] = "Zelená", ["Blue"] = "Modrá", ["Amber"] = "Oranžová", ["Trace speed"] = "Rýchlosť krivky", ["Normal"] = "Normálna", ["Fast"] = "Rýchla" }, cs = { ["Backlight"] = "Podsvícení", ["Green"] = "Zelená", ["Blue"] = "Modrá", ["Amber"] = "Oranžová", ["Trace speed"] = "Rychlost křivky", ["Normal"] = "Normální", ["Fast"] = "Rychlá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "light", label = T("Backlight"), type = "int", default = "green", opts = { keys = { "green", "blue", "amber" }, T("Green"), T("Blue"), T("Amber") } }, { key = "speed", label = T("Trace speed"), type = "int", default = "normal", opts = { keys = { "normal", "fast" }, T("Normal"), T("Fast") } }, } local rect, px = gfx.rect, gfx.px local floor, sin, cos, exp, abs, min = math.floor, math.sin, math.cos, math.exp, math.abs, math.min local W = gfx.W local THEME = { green = { { 178, 210, 156 }, { 22, 44, 32 }, { 166, 198, 144 }, { 112, 150, 110 }, { 222, 240, 204 }, { 30, 52, 40 } }, blue = { { 168, 208, 240 }, { 10, 28, 68 }, { 156, 196, 228 }, { 96, 140, 204 }, { 220, 240, 255 }, { 16, 34, 74 } }, amber = { { 242, 186, 106 }, { 72, 28, 8 }, { 230, 174, 94 }, { 196, 124, 60 }, { 255, 220, 156 }, { 80, 36, 14 } }, } local BG, INK, GHOST, MID, HI, BEZ local HEART_PAL local BGRAW, SP = nil, 1 local SEG = { "abcdef", "bc", "abdeg", "abcdg", "bcfg", "acdfg", "acdefg", "abc", "abcdefg", "abcdfg" } local function digits(w, h, th) local half = (h - th) // 2 local S = { a = { 1, 0, w - 2, th }, b = { w - th, 1, th, half }, c = { w - th, h // 2, th, h - th - h // 2 }, d = { 1, h - th, w - 2, th }, e = { 0, h // 2, th, h - th - h // 2 }, f = { 0, 1, th, half }, g = { 1, half, w - 2, th }, } local out = {} for d = 0, 9 do out[d] = {} for ch in SEG[d + 1]:gmatch(".") do out[d][#out[d] + 1] = S[ch] end end return out end local BIGD, SMLD = digits(9, 20, 2), digits(6, 9, 1) local function seg_draw(D, x, y, d, c) local s = D[d] for i = 1, #s do rect(x + s[i][1], y + s[i][2], s[i][3], s[i][4], c[1], c[2], c[3]) end end local HEARTS = { { w = 15, h = 14, rows = { ".111111.111111.", "122222212222221", "122332222222221", "122322222222221", "122222222222221", "122222222222221", "122222222222221", ".1222222222221.", ".1222222222221.", "..12222222221..", "...112222211...", ".....12221.....", "......121......", ".......1......." } }, { w = 19, h = 15, rows = { "..1111111.1111111..", ".12222222122222221.", "1223322222222222221", "1223222222222222221", "1222222222222222221", "1222222222222222221", "1222222222222222221", "1222222222222222221", ".12222222222222221.", "..122222222222221..", "...1222222222221...", "....12222222221....", ".....122222221.....", "......1122211......", "........111........" } }, { w = 21, h = 18, rows = { "...111111...111111...", ".112222221.122222211.", ".1233222221222222221.", "122322222222222222221", "122222222222222222221", "122222222222222222221", "122222222222222222221", "122222222222222222221", "122222222222222222221", ".1222222222222222221.", ".1222222222222222221.", "..12222222222222221..", "...122222222222221...", "....1222222222221....", ".....12222222221.....", "......112222211......", "........11211........", "..........1.........." } }, } for i = 1, #HEARTS do local H = HEARTS[i] H.cells = table.concat(H.rows):gsub(".", function(c) return string.char(c == "." and 0 or c + 0) end) H.x, H.y = 15 - H.w // 2, 20 - H.h // 2 end local BASE_BPM, SWING = 75, 30 local CYC = 720 local OMEGA = 6.28318 / CYC local function beats(tau) return BASE_BPM * tau / 1800 + SWING / (1800 * OMEGA) * (1 - cos(OMEGA * tau)) end local function gauss(p, c, wd, a) local d = (p - c) / wd return a * exp(-d * d) end local AMP = {} for i = 1, 128 do local p = (i - 0.5) / 128 AMP[i] = floor(gauss(p, 0.14, 0.03, 1.5) + gauss(p, 0.265, 0.012, -1.5) + gauss(p, 0.29, 0.014, 9.5) + gauss(p, 0.315, 0.014, -4) + gauss(p, 0.52, 0.05, 2.6) + 0.5) end local EX, EY, EW, EH, EBASE, GAP = 3, 32, 58, 18, 41, 6 local function build_bg() rect(0, 0, W, W, BEZ[1], BEZ[2], BEZ[3]) rect(2, 2, W - 4, W - 4, BG[1], BG[2], BG[3]) for _, c in ipairs({ { 2, 2 }, { 61, 2 }, { 2, 61 }, { 61, 61 } }) do px(c[1], c[2], BEZ[1], BEZ[2], BEZ[3]) end gfx.text(5, 4, "BPM", INK[1], INK[2], INK[3], { font = "3x5" }) rect(45, 4, 11, 5, INK[1], INK[2], INK[3]); rect(46, 5, 9, 3, BG[1], BG[2], BG[3]); rect(56, 5, 1, 3, INK[1], INK[2], INK[3]) rect(47, 5, 2, 3, INK[1], INK[2], INK[3]); rect(50, 5, 2, 3, INK[1], INK[2], INK[3]) for i = 0, 2 do seg_draw(BIGD, 29 + 11 * i, 10, 8, GHOST) end rect(EX, EY - 1, EW, 1, MID[1], MID[2], MID[3]); rect(EX, EY + EH, EW, 1, MID[1], MID[2], MID[3]) for x = EX, EX + EW - 1, 2 do px(x, EBASE, GHOST[1], GHOST[2], GHOST[3]) end for _, gx in ipairs({ 15, 27, 39, 51 }) do for y = EY, EY + EH - 1, 2 do px(gx, y, GHOST[1], GHOST[2], GHOST[3]) end end for _, x in ipairs({ 12, 19, 29, 36, 46 }) do seg_draw(SMLD, x, 52, 8, GHOST) end rect(26, 54, 2, 2, GHOST[1], GHOST[2], GHOST[3]); rect(26, 57, 2, 2, GHOST[1], GHOST[2], GHOST[3]) rect(43, 59, 2, 2, INK[1], INK[2], INK[3]) BGRAW = gfx.snapshot(0, 0, W, W) end function on_settings() local th = THEME[cfg.light] or THEME.green BG, INK, GHOST, MID, HI, BEZ = th[1], th[2], th[3], th[4], th[5], th[6] HEART_PAL = { 0, 0, 0, INK[1], INK[2], INK[3], MID[1], MID[2], MID[3], HI[1], HI[2], HI[3] } SP = cfg.speed == "fast" and 2 or 1 BGRAW = nil end function render() if not BGRAW then build_bg() end gfx.blit_rgb(BGRAW, 0, 0, W, W) local t = frame % CYC local ph = beats(t) % 1 local H = ph >= 0.27 and ph < 0.42 and HEARTS[3] or ph >= 0.42 and ph < 0.52 and HEARTS[2] or HEARTS[1] gfx.blit_indexed(H.cells, HEART_PAL, H.x, H.y, H.w, H.h, H.w, H.h, true) local bpm = floor(BASE_BPM + SWING * sin(OMEGA * (t // 15 * 15)) + 0.5) if bpm >= 100 then seg_draw(BIGD, 29, 10, bpm // 100, INK) end seg_draw(BIGD, 40, 10, bpm // 10 % 10, INK) seg_draw(BIGD, 51, 10, bpm % 10, INK) local cur = (frame % 20880 * SP) % EW local prevY, prevA for x = 0, EW - 1 do local a = (cur - x) % EW if a < EW - GAP then local tau = (t - a / SP) % CYC local y = EBASE - AMP[min(128, floor((beats(tau) % 1) * 128) + 1)] if prevA == a + 1 then rect(EX + x, min(y, prevY), 1, abs(y - prevY) + 1, INK[1], INK[2], INK[3]) else px(EX + x, y, INK[1], INK[2], INK[3]) end prevY, prevA = y, a else prevA = nil end end rect(EX + (cur + 1) % EW, EY, 1, EH, MID[1], MID[2], MID[3]) local f = frame local mm, ss, tt = f // 1800 % 100, f // 30 % 60, f // 3 % 10 seg_draw(SMLD, 12, 52, mm // 10, INK); seg_draw(SMLD, 19, 52, mm % 10, INK) seg_draw(SMLD, 29, 52, ss // 10, INK); seg_draw(SMLD, 36, 52, ss % 10, INK) seg_draw(SMLD, 46, 52, tt, INK) if f % 30 < 15 then rect(26, 54, 2, 2, INK[1], INK[2], INK[3]); rect(26, 57, 2, 2, INK[1], INK[2], INK[3]) end if f % 30 < 15 then rect(53, 5, 2, 3, INK[1], INK[2], INK[3]) end end