MantisIDE

Lesson 15: Stopwatch · Round 1.28" touch watch

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Lesson 15 - App: stopwatch with laps Goal: app state (running / paused / laps), time with ticks_ms, a scrollable list.

You’ll learn: app state, ticks_ms, lists

Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Learning roadmap, Level 5 · Complete apps. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.

# Lesson 15 - App: stopwatch with laps
# Goal: app state (running / paused / laps), time with ticks_ms, a scrollable list.
import time
import lvgl as lv
import display

display.init()                     # start the display and touch (does nothing if already on)

BG = 0x05110A
LINE = 0x24452F
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C

FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}


def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
    """lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
    lb = lv.label(parent)
    lb.set_text(text)
    lb.set_style_text_color(lv.color_hex(color), 0)
    lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
    if zoom:
        lb.set_style_transform_scale(int(256 * zoom), 0)   # 256 = 100 %
        lb.update_layout()
        lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
        lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
    lb.align(align, int(x), int(y))
    return lb


def ring(parent, size, width, color, value=100, bg_color=LINE):
    """An lv.arc used as a progress ring (no knob, not touchable)."""
    a = lv.arc(parent)
    a.set_size(size, size)
    a.center()
    a.set_rotation(270)                  # start at 12 o'clock
    a.set_bg_angles(0, 360)
    a.set_range(0, 100)
    a.set_value(value)
    a.remove_style(None, lv.PART.KNOB)
    a.remove_flag(lv.obj.FLAG.CLICKABLE)
    a.set_style_arc_width(width, lv.PART.MAIN)
    a.set_style_arc_width(width, lv.PART.INDICATOR)
    a.set_style_arc_color(lv.color_hex(bg_color), lv.PART.MAIN)
    a.set_style_arc_color(lv.color_hex(color), lv.PART.INDICATOR)
    return a

scr = lv.screen_active()             # the screen LVGL is showing
scr.clean()                           # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)

st = {"running": False, "start": 0, "acc": 0, "laps": []}
progress_ring = ring(scr, 224, 6, ACCENT, 0)
clock = label(scr, "00:00.0", TEXT, 16, y=-38, zoom=2.2)

laps = lv.list(scr)
laps.set_size(130, 62)
laps.align(lv.ALIGN.CENTER, 0, 10)
laps.set_style_bg_opa(lv.OPA.TRANSP, 0)
laps.set_style_border_width(0, 0)
laps.set_style_pad_all(0, 0)


def elapsed():
    return st["acc"] + (time.ticks_diff(time.ticks_ms(), st["start"]) if st["running"] else 0)


def fmt(ms):
    return "%02d:%02d.%d" % (ms // 60000, ms // 1000 % 60, ms // 100 % 10)


def round_btn(sym, x, color, cb):
    b = lv.button(scr)
    b.set_size(54, 54)
    b.align(lv.ALIGN.CENTER, x, 72)
    b.set_style_radius(lv.RADIUS_CIRCLE, 0)
    b.set_style_bg_color(lv.color_hex(color), 0)
    lb = lv.label(b)
    lb.set_text(sym)
    lb.center()
    b.add_event_cb(cb, lv.EVENT.CLICKED, None)
    return lb


def start_stop(e):
    if st["running"]:
        st["acc"] = elapsed()
        st["running"] = False
        play.set_text(lv.SYMBOL.PLAY)
    else:
        st["start"] = time.ticks_ms()
        st["running"] = True
        play.set_text(lv.SYMBOL.PAUSE)


def lap_reset(e):
    if st["running"]:
        st["laps"].append(elapsed())
        b = laps.add_button(None, "Lap %d   %s" % (len(st["laps"]), fmt(st["laps"][-1])))
        b.set_style_bg_opa(lv.OPA.TRANSP, 0)
        b.set_style_text_color(lv.color_hex(MUTED), 0)
        b.scroll_to_view(True)          # True = animate (LVGL 9.4)
    else:
        st.update(acc=0, laps=[])
        laps.clean()

play = round_btn(lv.SYMBOL.PLAY, 34, ACCENT, start_stop)
round_btn(lv.SYMBOL.LOOP, -34, LINE, lap_reset)


def tick(_):
    ms = elapsed()
    clock.set_text(fmt(ms))
    progress_ring.set_value(ms // 600 % 100)       # one lap of the ring per minute

tick_timer = lv.timer_create(tick, 50, None)
print("Play/pause on the right, lap (running) or reset (paused) on the left.")
Loading post…

Dialog