Lesson 6: Swipe pages · Round 1.28" touch watch
Lesson 6 - Swipe between pages (tileview) Goal: build a multi-page watch UI. A tileview is a grid of full-screen tiles you move between with swipes - the classic smartwatch navigation.
You’ll learn: tileview navigation, page dots
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Learning roadmap, Level 2 · Widgets and touch. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Lesson 6 - Swipe between pages (tileview)
# Goal: build a multi-page watch UI. A tileview is a grid of full-screen tiles you
# move between with swipes - the classic smartwatch navigation.
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
LINE = 0x24452F
TEXT = 0xE3EFE6
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
AMBER = 0xF2B84B
VIOLET = 0xB593FF
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
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)
tv = lv.tileview(scr)
tv.set_size(240, 240)
tv.set_style_bg_opa(lv.OPA.TRANSP, 0)
tv.remove_flag(lv.obj.FLAG.SCROLL_ELASTIC)
pages = [("Home", lv.SYMBOL.HOME, ACCENT),
("Music", lv.SYMBOL.AUDIO, VIOLET),
("Alerts", lv.SYMBOL.BELL, AMBER),
("Settings", lv.SYMBOL.SETTINGS, BLUE)]
dots = []
for i, (name, sym, color) in enumerate(pages):
tile = tv.add_tile(i, 0, lv.DIR.LEFT | lv.DIR.RIGHT)
page_ring = ring(tile, 170, 6, color, 100)
label(tile, sym, color, 16, y=-14, zoom=2.5)
label(tile, name, TEXT, 16, y=34)
# page indicator dots near the bottom edge
for i in range(len(pages)):
dots.append(dot(scr, 8, LINE, (i - 1.5) * 16, 98))
def on_scroll_end(e):
act = tv.get_tile_active()
idx = act.get_x() // 240 if act else 0 # None until the first layout pass
for i, d in enumerate(dots):
d.set_style_bg_color(lv.color_hex(pages[idx][2] if i == idx else LINE), 0)
tv.add_event_cb(on_scroll_end, lv.EVENT.VALUE_CHANGED, None)
tv.update_layout()
on_scroll_end(None)
print("Swipe left/right on the watch.")
Comments