info = { type = "image", title = "Heartbeat", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "A beating heart with a running ECG line", api = 33, } local ltr = ({ sk = { ["Heart rate (bpm)"] = "Tep (úderov/min)", ["Heart colour"] = "Farba srdca" }, cs = { ["Heart rate (bpm)"] = "Tep (úderů/min)", ["Heart colour"] = "Barva srdce" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "bpm", label = LT("Heart rate (bpm)"), type = "int", min = 40, max = 140, default = 72, unit = "bpm" }, { key = "color", label = LT("Heart colour"), type = "color", default = "#FF2A48" }, } local rect, px = gfx.rect, gfx.px local floor, sqrt, max = math.floor, math.sqrt, math.max local rep = string.rep local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local SW, SH = 28, 26 local function heart_grid() local r, a, yc = 7.2, 6.8, 7.4 local cl, cr = 14 - a, 14 + a local inside = {} for y = 0, SH - 1 do local yy, row = y + 0.5, {} for x = 0, SW - 1 do local xx = x + 0.5 local ok = false if yy <= yc then local dy = yy - yc local v = r * r - dy * dy if v > 0 then local c = sqrt(v) ok = (xx >= cl - c and xx <= cl + c) or (xx >= cr - c and xx <= cr + c) end else local s = (yy - yc) / (SH - yc) if s < 1 then local hw = 14 * (1 - s) ^ 0.9 ok = xx >= 14 - hw and xx <= 14 + hw end end row[x] = ok end inside[y] = row end local out = {} for y = 0, SH - 1 do local r2 = {} for x = 0, SW - 1 do local c = 0 if inside[y][x] then local edge = x == 0 or x == SW - 1 or y == 0 or y == SH - 1 or not inside[y][x - 1] or not inside[y][x + 1] or not inside[y - 1][x] or not inside[y + 1][x] c = edge and 1 or 2 end r2[x + 1] = c end out[y + 1] = string.char(table.unpack(r2, 1, SW)) end local s = table.concat(out) local function set(x, y, v) local i = y * SW + x + 1 s = s:sub(1, i - 1) .. string.char(v) .. s:sub(i + 1) end set(5, 4, 3); set(6, 4, 3); set(4, 5, 3); set(4, 6, 3) return s end local HEART = heart_grid() local PAL, HALO1, HALO2 = nil, nil, nil local ECG = { 0, 0, 0 } local ECGC = { {}, {}, {} } local LABEL = nil local BPMTXT = "" local TC, WY = 25, {} local WAVE = { { 0, 0 }, { 0.06, 0 }, { 0.10, 0.14 }, { 0.14, 0 }, { 0.20, 0 }, { 0.22, -0.14 }, { 0.25, 1.0 }, { 0.285, -0.32 }, { 0.32, 0 }, { 0.40, 0 }, { 0.50, 0.28 }, { 0.60, 0 }, { 1.0, 0 } } local function wave(ph) for i = 2, #WAVE do local a, b = WAVE[i - 1], WAVE[i] if ph <= b[1] then return a[2] + (b[2] - a[2]) * (ph - a[1]) / (b[1] - a[1]) end end return 0 end local BASE, AMP = 52, 13 function on_settings() local c = cfg.color or { r = 255, g = 42, b = 72 } PAL = { 0, 0, 0, c.r * 55 // 100, c.g * 55 // 100, c.b * 55 // 100, c.r, c.g, c.b, c.r + (255 - c.r) * 55 // 100, c.g + (255 - c.g) * 55 // 100, c.b + (255 - c.b) * 55 // 100 } HALO1 = { 0, 0, 0, c.r * 32 // 100, c.g * 32 // 100, c.b * 32 // 100, c.r * 32 // 100, c.g * 32 // 100, c.b * 32 // 100, c.r * 32 // 100, c.g * 32 // 100, c.b * 32 // 100 } HALO2 = { 0, 0, 0, c.r * 18 // 100, c.g * 18 // 100, c.b * 18 // 100, c.r * 18 // 100, c.g * 18 // 100, c.b * 18 // 100, c.r * 18 // 100, c.g * 18 // 100, c.b * 18 // 100 } local lr, lg, lb = c.r + (255 - c.r) * 45 // 100, c.g + (255 - c.g) * 45 // 100, c.b + (255 - c.b) * 45 // 100 ECGC = { { lr * 38 // 100, lg * 38 // 100, lb * 38 // 100 }, { lr * 68 // 100, lg * 68 // 100, lb * 68 // 100 }, { lr, lg, lb } } LABEL = { c.r * 55 // 100, c.g * 55 // 100, c.b * 55 // 100 } local bpm = cfg.bpm or 72 TC = floor(1800 / bpm + 0.5) for a = 0, TC - 1 do WY[a] = BASE - floor(AMP * wave(a / TC) + 0.5) end BPMTXT = tostring(bpm) .. " BPM" end local F_TXT = { font = "3x5" } local function pulse(u, k) if u < 0 then return 0 end if u < 2 then return k * (u + 1) / 3 end local v = 1 - (u - 2) / 6 return v > 0 and k * v or 0 end function render() if not PAL then on_settings() end local t = frame * STEP_HZ // FPS gfx.clear(0, 0, 0) local aR = floor(0.25 * TC + 0.5) local u = (59 + t - aR) % TC local d = floor(0.25 * TC + 0.5) local amp = max(pulse(u, 1), pulse(u - d, 0.6)) local sc = 0.98 + 0.2 * amp local w, h = floor(SW * sc + 0.5), floor(SH * sc + 0.5) local cy = 20 if amp > 0.55 then local gw, gh = floor(w * 1.26), floor(h * 1.26) gfx.blit_indexed(HEART, HALO1, 32 - gw // 2, cy - gh // 2, gw, gh, SW, SH, true) elseif amp > 0.2 then local gw, gh = floor(w * 1.2), floor(h * 1.2) gfx.blit_indexed(HEART, HALO2, 32 - gw // 2, cy - gh // 2, gw, gh, SW, SH, true) end gfx.blit_indexed(HEART, PAL, 32 - w // 2, cy - h // 2, w, h, SW, SH, true) local x0, run_lo, run_hi, run_z, run_x = 0, nil, nil, 0, 0 local prev = WY[(t) % TC] for c = 0, 59 do local y = WY[(c + t) % TC] local lo, hi = y, y if prev < lo then lo = prev elseif prev > hi then hi = prev end if c == 0 then lo, hi = y, y end prev = y local z = c < 20 and 1 or (c < 40 and 2 or 3) if run_lo and lo == hi and run_lo == run_hi and lo == run_lo and z == run_z then else if run_lo then local col = ECGC[run_z] rect(2 + run_x, run_lo, c - run_x, run_hi - run_lo + 1, col[1], col[2], col[3]) end run_lo, run_hi, run_z, run_x = lo, hi, z, c end end local col = ECGC[run_z] rect(2 + run_x, run_lo, 60 - run_x, run_hi - run_lo + 1, col[1], col[2], col[3]) local py = WY[(59 + t) % TC] rect(60, py, 3, 1, 255, 255, 255) gfx.text(2, 58, BPMTXT, LABEL[1], LABEL[2], LABEL[3], F_TXT) end