info = { type = "app", title = "Nixie clock", author = "BRRRO", license = "MIT", desc = "Time glowing in nixie tubes", category = "time", api = 42, } local T = ({ sk = { roll = "Prebeh číslic", roll_o = { "pri zmene", "každú hodinu", "vypnutý" }, roll_h = "Ako skutočné nixie hodiny - trubica preblesne všetky číslice", glow = "Podsvietenie", glow_o = { "žiadne", "modré", "fialové", "zelené" }, date = "Deň a dátum", tdate = "Dátum v trubiciach", tdate_h = "Každú minútu na 5 sekúnd, od 30. sekundy" }, cs = { roll = "Proběh číslic", roll_o = { "při změně", "každou hodinu", "vypnutý" }, roll_h = "Jako skutečné nixie hodiny - trubice problikne všechny číslice", glow = "Podsvícení", glow_o = { "žádné", "modré", "fialové", "zelené" }, date = "Den a datum", tdate = "Datum v trubicích", tdate_h = "Každou minutu na 5 sekund, od 30. sekundy" } })[lang] or { roll = "Digit roll", roll_o = { "on change", "hourly", "off" }, roll_h = "Like real nixie clocks - the tube flashes through all its digits", glow = "Backlight", glow_o = { "none", "blue", "violet", "green" }, date = "Day and date", tdate = "Date in the tubes", tdate_h = "Every minute for 5 seconds, from the 30th second" } settings = { { key = "roll", type = "int", default = "change", label = T.roll, hint = T.roll_h, opts = { keys = { "change", "hour", "off" }, table.unpack(T.roll_o) } }, { key = "glow", type = "int", default = "none", label = T.glow, opts = { keys = { "none", "blue", "violet", "green" }, table.unpack(T.glow_o) } }, { key = "date", type = "bool", default = true, label = T.date }, { key = "tdate", type = "bool", default = false, label = T.tdate, hint = T.tdate_h }, } local W, H = gfx.W, gfx.H local clear, px, blend, rect, blend_rect = gfx.clear, gfx.px, gfx.blend, gfx.rect, gfx.blend_rect local blit_rgb, blend_mask = gfx.blit_rgb, gfx.blend_mask local floor, ceil, sqrt, min, max = math.floor, math.ceil, math.sqrt, math.min, math.max local sin, cos, rad = math.sin, math.cos, math.rad local fmt, char, concat = string.format, string.char, table.concat local DAYS = ({ sk = { "NEDEĽA", "PONDELOK", "UTOROK", "STREDA", "ŠTVRTOK", "PIATOK", "SOBOTA" }, cs = { "NEDĚLE", "PONDĚLÍ", "ÚTERÝ", "STŘEDA", "ČTVRTEK", "PÁTEK", "SOBOTA" }, })[lang] or { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" } local TX, TW = { 1, 15, 36, 50 }, 13 local DW, DH, DY = 9, 21, 20 local GT, GB = DY - 8, DY + DH + 5 local PL = GB + 5 local PAD = 3 local GW, GH = DW + 2 * PAD, DH + 2 * PAD local LAMP_X, LAMP_Y = 31, { DY + 6, DY + 14 } local GLOW_C = { 255, 70, 0 } local CORE_C = { 255, 150, 50 } local HOT_C = { 255, 220, 150 } local METAL = { 92, 82, 76 } local BACKLIGHT = { blue = { 30, 90, 255 }, violet = { 150, 40, 255 }, green = { 20, 220, 90 } } local STACK = { 1, 6, 2, 7, 5, 0, 4, 9, 8, 3 } local function L(p, x, y) p[#p + 1] = x; p[#p + 1] = y end local function A(p, cx, cy, rx, ry, a0, a1) local n = max(2, ceil(math.abs(a1 - a0) / 15)) for k = 0, n do local a = rad(a0 + (a1 - a0) * k / n) L(p, cx + rx * cos(a), cy - ry * sin(a)) end end local SHAPE = { [0] = function(p) A(p, 4, 10, 3.6, 9.6, 90, 450) end, [1] = function(p) L(p, 4, 0.3); L(p, 4, 20) end, [2] = function(p) A(p, 4, 5, 3.6, 4.6, 165, -40); L(p, 0.4, 20); L(p, 8, 20) end, [3] = function(p) L(p, 0.6, 0.3); L(p, 7.6, 0.3); A(p, 4, 14.2, 3.7, 5.8, 100, -150) end, [4] = function(p) L(p, 8.4, 14); L(p, 0.3, 14); L(p, 6, 0.3); L(p, 6, 20) end, [5] = function(p) L(p, 7.8, 0.3); L(p, 1.2, 0.3); A(p, 4, 13.9, 3.7, 6, 140, -150) end, [6] = function(p) A(p, 7.7, 14.2, 7.4, 13.6, 95, 180); A(p, 4, 14.2, 3.7, 5.8, 180, 540) end, [7] = function(p) L(p, 0.3, 0.3); L(p, 8, 0.3); L(p, 2.6, 20) end, [8] = function(p, q) A(p, 4, 4.8, 3.1, 4.5, 0, 360); A(q, 4, 14.6, 3.8, 5.4, 0, 360) end, [9] = function(p) A(p, 4, 6, 3.7, 5.8, 0, 360); A(p, 0.3, 6, 7.4, 13.6, 0, -85) end, } local CORE, GLOW = {}, {} local function build_digit(d) local core, glow = {}, {} for i = 1, DW * DH do core[i] = 0 end for i = 1, GW * GH do glow[i] = 0 end local p, q = {}, {} SHAPE[d](p, q) for _, path in ipairs({ p, q }) do for s = 1, #path - 3, 2 do local ax, ay, bx, by = path[s], path[s + 1], path[s + 2], path[s + 3] local dx, dy = bx - ax, by - ay local l2 = dx * dx + dy * dy for y = max(-PAD, floor(min(ay, by)) - PAD), min(DH - 1 + PAD, ceil(max(ay, by)) + PAD) do for x = max(-PAD, floor(min(ax, bx)) - PAD), min(DW - 1 + PAD, ceil(max(ax, bx)) + PAD) do local t = l2 > 0 and ((x - ax) * dx + (y - ay) * dy) / l2 or 0 if t < 0 then t = 0 elseif t > 1 then t = 1 end local ex, ey = ax + t * dx - x, ay + t * dy - y local e = sqrt(ex * ex + ey * ey) if e < PAD then local gi = (y + PAD) * GW + x + PAD + 1 local g = 1 - e / PAD if g > glow[gi] then glow[gi] = g end if e < 0.9 and x >= 0 and x < DW and y >= 0 and y < DH then local c = 1.4 - 1.6 * e if c > 1 then c = 1 end local ci = y * DW + x + 1 if c > core[ci] then core[ci] = c end end end end end end end for i = 1, #core do core[i] = floor(core[i] * 255) end for i = 1, #glow do glow[i] = floor(glow[i] * 255) end CORE[d] = char(table.unpack(core)) GLOW[d] = char(table.unpack(glow)) end local AW, AH = TW + 8, GB - GT + 8 local AURA = nil local function build_aura() local m = {} for y = 0, AH - 1 do for x = 0, AW - 1 do local ex = math.abs(x - (AW - 1) / 2) / (AW / 2) local ey = math.abs(y - (AH - 1) / 2) / (AH / 2) local k = 1 - sqrt(ex * ex * 1.0 + ey * ey * 1.6) m[y * AW + x + 1] = k > 0 and floor(k * 255) or 0 end end AURA = char(table.unpack(m)) end local BG, BG_KEY = nil, nil local O_DATE = { font = "4x6", fit = "3x5", shadow = { 34, 16, 6 } } local function glass(x0) local rows = { { 4, 4 }, { 2, 8 }, { 1, 10 } } px(x0 + 6, GT - 2, 46, 48, 54); px(x0 + 6, GT - 1, 60, 62, 70) for k, r in ipairs(rows) do local y = GT + k - 1 rect(x0 + r[1], y, r[2] + 1, 1, 14, 12, 14) blend(x0 + r[1], y, 120, 126, 140, 0.45); blend(x0 + r[1] + r[2], y, 120, 126, 140, 0.45) end blend_rect(x0 + 4, GT, 5, 1, 120, 126, 140, 0.45) rect(x0 + 1, GT + 3, TW - 2, GB - GT - 2, 14, 12, 14) blend_rect(x0, GT + 3, 1, GB - GT - 2, 120, 126, 140, 0.4) blend_rect(x0 + TW - 1, GT + 3, 1, GB - GT - 2, 120, 126, 140, 0.3) end local function build_bg(dstr, cold) clear(9, 6, 5) for i = 1, 4 do local x0 = TX[i] glass(x0) if not cold then for d = 0, 9 do blend_mask(CORE[d], METAL[1], METAL[2], METAL[3], x0 + 2, DY, DW, DH, DW, DH, 0.22) end end rect(x0, GB + 1, TW, PL - GB - 1, 22, 18, 16) rect(x0, GB + 1, TW, 1, 58, 50, 44) local bl = BACKLIGHT[cfg.glow] if bl then blend_rect(x0 + 1, GB + 1, TW - 2, 1, bl[1], bl[2], bl[3], 0.9) for k = 0, 15 do blend_rect(x0 + 1, GB - k, TW - 2, 1, bl[1], bl[2], bl[3], 0.7 * (1 - k / 16)) end end end for _, y in ipairs(LAMP_Y) do blend_rect(LAMP_X, y, 2, 2, METAL[1], METAL[2], METAL[3], 0.5) end rect(0, PL, W, 1, 132, 82, 44) rect(0, PL + 1, W, H - PL - 1, 86, 48, 24) for k = 0, 2 do for x = 0, W - 1 do local y = PL + 3 + k * 4 + floor(1.2 * sin(x * 0.19 + k * 2.1) + 0.6 * sin(x * 0.53 + k)) if y < H then px(x, y, 70, 38, 18) end end end if dstr then gfx.text_center(PL + 4, dstr, 222, 178, 92, O_DATE) end if not cold then BG = gfx.snapshot(0, 0, W, H) end end local function time_digits(h, m, fmt12) local a, b if fmt12 then local h12 = h % 12 if h12 == 0 then h12 = 12 end a, b = h12 >= 10 and 1 or -1, h12 % 10 else a, b = h // 10, h % 10 end return { a, b, m // 10, m % 10 } end local function lit(x0, d, k) if d < 0 or k <= 0 then return end blend_mask(AURA, GLOW_C[1], GLOW_C[2], GLOW_C[3], x0 - 4, GT - 4, AW, AH, AW, AH, 0.16 * k) blend_mask(GLOW[d], GLOW_C[1], GLOW_C[2], GLOW_C[3], x0 + 2 - PAD, DY - PAD, GW, GH, GW, GH, 0.62 * k) blend_mask(CORE[d], CORE_C[1], CORE_C[2], CORE_C[3], x0 + 2, DY, DW, DH, DW, DH, k) blend_mask(CORE[d], HOT_C[1], HOT_C[2], HOT_C[3], x0 + 2, DY, DW, DH, DW, DH, 0.45 * k) end local function lamp(y, k) if k <= 0 then return end blend_rect(LAMP_X - 1, y, 4, 2, GLOW_C[1], GLOW_C[2], GLOW_C[3], 0.4 * k) blend_rect(LAMP_X, y - 1, 2, 4, GLOW_C[1], GLOW_C[2], GLOW_C[3], 0.4 * k) blend_rect(LAMP_X, y, 2, 2, CORE_C[1], CORE_C[2], CORE_C[3], k) blend(LAMP_X, y, HOT_C[1], HOT_C[2], HOT_C[3], 0.5 * k) end local FADE = 0.3 local ROLL_HZ = 18 function render() local t = data.time if not (t and t.ok) then clear(0, 0, 0) gfx.text_center(28, "LOADING", 255, 150, 50) return end local dstr if cfg.date then dstr = DAYS[(t.wday % 7) + 1] .. fmt(" %d.%d", t.day, t.month) if t.fmt12 then dstr = dstr .. (t.pm and " PM" or " AM") end end if not GLOW[9] then for d = 0, 9 do if not GLOW[d] then build_digit(d); if d % 2 == 1 then break end end end build_bg(dstr, true) return end if not AURA then build_aura() end local key = fmt("%s|%s|%s", tostring(dstr), cfg.glow, tostring(cfg.date)) if key ~= BG_KEY then build_bg(dstr); BG_KEY = key end blit_rgb(BG, 0, 0, W, H) local e = t.s + t.ms / 1000 local cur = time_digits(t.h, t.m, t.fmt12) local prev, t0, full local date_now = cfg.tdate and e >= 30 and e < 35 local dd = { t.day // 10, t.day % 10, t.month // 10, t.month % 10 } if date_now then prev, cur, t0 = cur, dd, 30 elseif cfg.tdate and e >= 35 then prev, t0 = dd, 35 else local pm = (t.h * 60 + t.m + 1439) % 1440 prev, t0, full = time_digits(pm // 60, pm % 60, t.fmt12), 0, t.m == 0 end local dt = e - t0 local arr = data.arrive and frame / gfx.FPS or 3600 local roll = cfg.roll local warm = roll == "off" and min(1, arr / 0.5) or 1 for i = 1, 4 do local x0, a, b = TX[i], prev[i], cur[i] local land if roll ~= "off" and arr < 0.5 + 0.22 * i then land = 0.5 + 0.22 * i - arr elseif full and roll ~= "off" then land = 0.9 + 0.25 * i - dt elseif roll == "change" and a ~= b then land = 0.45 + 0.1 * i - dt end if land and land > 0 then lit(x0, STACK[(floor((e + i * 0.37) * ROLL_HZ)) % 10 + 1], 1) elseif a ~= b and dt < FADE then local k = dt / FADE lit(x0, a, (1 - k) * warm); lit(x0, b, k * warm) else lit(x0, b, warm) end end if date_now then lamp(LAMP_Y[2], 1) else local k = t.s % 2 == 0 and 1 or max(0, 1 - t.ms / 160) lamp(LAMP_Y[1], k); lamp(LAMP_Y[2], k) end for i = 1, 4 do blend_rect(TX[i] + 1, GT + 5, 1, GB - GT - 9, 200, 210, 230, 0.10) blend(TX[i] + 3, GT + 1, 220, 230, 250, 0.35) end end