Lesson 14: Watch face · Round 1.28" touch watch
Lesson 14 - App: analog + digital watch face Goal: put it all together - scale with hour numbers, needle hands driven by the RTC, a timer for updates and a tap to switch to a sensor page. Set the clock once from the shell: import machine; machine.RTC().datetime((2026,1,1,3,12,0,0,0)) (year, month, day, weekday 0=Mon, hour, min, sec, subsec) - or sync over Wi-Fi with ntptime.
You’ll learn: RTC, analog hands, pages
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 14 - App: analog + digital watch face
# Goal: put it all together - scale with hour numbers, needle hands driven by the RTC,
# a timer for updates and a tap to switch to a sensor page.
# Set the clock once from the shell: import machine; machine.RTC().datetime((2026,1,1,3,12,0,0,0))
# (year, month, day, weekday 0=Mon, hour, min, sec, subsec) - or sync over Wi-Fi with ntptime.
import machine
import lvgl as lv
import display
import bb_imu
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
LINE = 0x24452F
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
RED = 0xFF6B6B
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 dot(parent, size, color, x=0, y=0, align=lv.ALIGN.CENTER):
"""A filled circle: a plain lv.obj with every theme style removed."""
o = lv.obj(parent)
o.remove_style_all()
size = max(1, int(size))
o.set_size(size, size)
o.set_style_radius(lv.RADIUS_CIRCLE, 0)
o.set_style_bg_color(lv.color_hex(color), 0)
o.set_style_bg_opa(lv.OPA.COVER, 0)
o.remove_flag(lv.obj.FLAG.CLICKABLE)
o.align(align, int(x), int(y))
return o
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
imu = bb_imu.IMU() # QMI8658 motion sensor, on the touch I2C bus
rtc = machine.RTC()
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)
face = lv.obj(scr)
face.remove_style_all()
face.set_size(240, 240)
face.center()
face.remove_flag(lv.obj.FLAG.CLICKABLE)
scale = lv.scale(face)
scale.set_size(228, 228)
scale.center()
scale.set_mode(lv.scale.MODE.ROUND_INNER)
scale.set_range(0, 60)
scale.set_angle_range(360)
scale.set_rotation(270)
scale.set_total_tick_count(61)
scale.set_major_tick_every(5)
scale.set_label_show(True)
scale.set_text_src(["12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", None])
scale.set_style_length(10, lv.PART.INDICATOR)
scale.set_style_length(4, lv.PART.ITEMS)
scale.set_style_line_color(lv.color_hex(ACCENT), lv.PART.INDICATOR)
scale.set_style_line_width(3, lv.PART.INDICATOR)
scale.set_style_line_color(lv.color_hex(LINE), lv.PART.ITEMS)
scale.set_style_text_color(lv.color_hex(TEXT), lv.PART.INDICATOR)
scale.set_style_arc_width(0, lv.PART.MAIN)
def hand(width, color):
h = lv.line(scale)
h.set_style_line_width(width, 0)
h.set_style_line_color(lv.color_hex(color), 0)
h.set_style_line_rounded(True, 0)
return h
h_hand, m_hand, s_hand = hand(6, TEXT), hand(4, TEXT), hand(2, RED)
dot(face, 12, RED)
digital = label(face, "", ACCENT, 14, y=40)
date = label(face, "", MUTED, 12, y=-40)
info = lv.obj(scr)
info.remove_style_all()
info.set_size(240, 240)
info.center()
info.add_flag(lv.obj.FLAG.HIDDEN)
info.remove_flag(lv.obj.FLAG.CLICKABLE)
sec_ring = ring(info, 220, 8, ACCENT, 0)
big = label(info, "", TEXT, 16, y=-20, zoom=2.4)
sensors = label(info, "", MUTED, 12, y=36)
DAYS = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
def tick(_):
y, mo, d, wd, h, mi, s, _sub = rtc.datetime()
scale.set_line_needle_value(h_hand, 55, (h % 12) * 5 + mi // 12)
scale.set_line_needle_value(m_hand, 85, mi)
scale.set_line_needle_value(s_hand, 95, s)
digital.set_text("%02d:%02d:%02d" % (h, mi, s))
date.set_text("%s %02d/%02d" % (DAYS[wd % 7], d, mo))
big.set_text("%02d:%02d" % (h, mi))
sec_ring.set_value(s * 100 // 59)
if not info.has_flag(lv.obj.FLAG.HIDDEN):
p, r = imu.tilt()
sensors.set_text("%.1f C tilt %+d %+d" % (imu.temperature(), p, r))
def flip(e):
for page in (face, info):
if page.has_flag(lv.obj.FLAG.HIDDEN):
page.remove_flag(lv.obj.FLAG.HIDDEN)
else:
page.add_flag(lv.obj.FLAG.HIDDEN)
scr.add_event_cb(flip, lv.EVENT.CLICKED, None)
tick(None)
tick_timer = lv.timer_create(tick, 250, None)
print("Tap to switch pages. Save as main.py (Run menu > Run at power-on) to make it your watch.")
Comments