Lesson 16: Settings app · Round 1.28" touch watch
Lesson 16 - App: settings that survive a reboot Goal: a scrollable settings list with switches and a slider, saved as JSON on flash.
You’ll learn: JSON persistence, sliders, switches
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 16 - App: settings that survive a reboot
# Goal: a scrollable settings list with switches and a slider, saved as JSON on flash.
import json
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
PANEL = 0x0C1A12
TEXT = 0xE3EFE6
ACCENT = 0x46E07C
AMBER = 0xF2B84B
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)
FILE = "/settings.json"
DEFAULTS = {"brightness": 80, "dark": True, "vibrate": False, "24h": True}
try:
with open(FILE) as f:
cfg = dict(DEFAULTS, **json.load(f))
except (OSError, ValueError):
cfg = dict(DEFAULTS)
def save():
with open(FILE, "w") as f:
json.dump(cfg, f)
label(scr, lv.SYMBOL.SETTINGS + " Settings", TEXT, 16, y=-92)
panel = lv.obj(scr)
panel.set_size(180, 150)
panel.align(lv.ALIGN.CENTER, 0, 12)
panel.set_flex_flow(lv.FLEX_FLOW.COLUMN)
panel.set_style_bg_color(lv.color_hex(PANEL), 0)
panel.set_style_border_width(0, 0)
panel.set_style_radius(18, 0)
panel.set_style_pad_row(10, 0)
panel.set_scroll_dir(lv.DIR.VER)
def row(text):
r = lv.obj(panel)
r.remove_style_all()
r.set_size(lv.pct(100), 32)
lb = lv.label(r)
lb.set_text(text)
lb.set_style_text_color(lv.color_hex(TEXT), 0)
lb.align(lv.ALIGN.LEFT_MID, 0, 0)
return r
def toggle(key, text):
sw = lv.switch(row(text))
sw.align(lv.ALIGN.RIGHT_MID, 0, 0)
sw.set_size(44, 24)
sw.set_style_bg_color(lv.color_hex(ACCENT), lv.PART.INDICATOR | lv.STATE.CHECKED)
if cfg[key]:
sw.add_state(lv.STATE.CHECKED)
def changed(e):
cfg[key] = sw.has_state(lv.STATE.CHECKED)
save()
sw.add_event_cb(changed, lv.EVENT.VALUE_CHANGED, None)
toggle("dark", "Dark theme")
toggle("24h", "24-hour clock")
toggle("vibrate", "Vibrate")
slider = lv.slider(panel)
slider.set_width(lv.pct(92))
slider.set_range(10, 100)
slider.set_value(cfg["brightness"], False) # False = no animation (LVGL 9.4)
slider.set_style_bg_color(lv.color_hex(AMBER), lv.PART.INDICATOR)
display.display.set_backlight(cfg["brightness"])
def bright(e):
cfg["brightness"] = slider.get_value()
display.display.set_backlight(cfg["brightness"])
save()
slider.add_event_cb(bright, lv.EVENT.VALUE_CHANGED, None)
print("Settings in", FILE, "->", cfg)
Comments