-- title: Hẹn giờ -- blurb: Đếm ngược 5, 10, 25 phút -- A countdown timer: shows how it uses device.every() for a tick each second. -- The screen is redrawn only when the shown number changes (minutes, then seconds in the last minute), -- because every redraw is an e-paper refresh. local left = 0 -- seconds left local running = false local shown = "" local function text() if left >= 60 then return tostring((left + 59) // 60) .. " phút" end return tostring(left) .. " giây" end function draw() shown = text() if left == 0 and not running then gfx.text(24, 260, "Chọn thời gian", 40, true) else gfx.text(24, 260, shown, 110, true) gfx.text(24, 470, running and "Đang đếm…" or "Tạm dừng", 26) end gfx.button(24, 560, 220, 90, "5 phút", 30) gfx.button(269, 560, 220, 90, "10 phút", 30) gfx.button(514, 560, 220, 90, "25 phút", 30) if left > 0 then gfx.button(24, 700, 710, 90, running and "Tạm dừng" or "Tiếp tục", 32, true) end end local function start(min) left = min * 60 running = true device.every(1) end function tap(x, y) if y >= 560 and y < 650 then start(x < 250 and 5 or x < 495 and 10 or 25) return true end if y >= 700 and y < 790 and left > 0 then running = not running device.every(running and 1 or 0) return true end return false end function tick() if not running then return false end left = left - 1 if left <= 0 then left = 0 running = false device.every(0) shown = "" return true end return text() ~= shown -- redraw only when the number on screen changes end