Lesson 7: Roller picker · Round 1.28" touch watch
Lesson 7 - Pickers: roller and switches Goal: choose values like an alarm clock. A roller is the iOS-style spinning picker.
You’ll learn: roller, switch, combined state
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 7 - Pickers: roller and switches
# Goal: choose values like an alarm clock. A roller is the iOS-style spinning picker.
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
PANEL = 0x0C1A12
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
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)
label(scr, "Set alarm", MUTED, 14, y=-88)
def roller(options, x, selected):
r = lv.roller(scr)
r.set_options(options, lv.roller.MODE.INFINITE)
r.set_visible_row_count(3)
r.set_width(62)
r.align(lv.ALIGN.CENTER, x, -10)
r.set_selected(selected, False)
r.set_style_bg_color(lv.color_hex(PANEL), 0)
r.set_style_text_color(lv.color_hex(MUTED), 0)
r.set_style_border_width(0, 0)
r.set_style_bg_color(lv.color_hex(ACCENT), lv.PART.SELECTED)
r.set_style_text_color(lv.color_hex(0x032010), lv.PART.SELECTED)
return r
hours = roller("\n".join("%02d" % h for h in range(24)), -36, 7)
mins = roller("\n".join("%02d" % m for m in range(0, 60, 5)), 36, 6)
label(scr, ":", TEXT, 16, y=-10, zoom=1.5)
sw = lv.switch(scr)
sw.align(lv.ALIGN.CENTER, 0, 62)
sw.add_state(lv.STATE.CHECKED)
sw.set_style_bg_color(lv.color_hex(ACCENT), lv.PART.INDICATOR | lv.STATE.CHECKED)
status = label(scr, "", TEXT, 12, y=90)
def update(e=None):
h = hours.get_selected()
m = mins.get_selected() * 5
on = sw.has_state(lv.STATE.CHECKED)
status.set_text("Alarm %02d:%02d %s" % (h, m, "ON" if on else "off"))
for w in (hours, mins, sw):
w.add_event_cb(update, lv.EVENT.VALUE_CHANGED, None)
update()
Comments