Lesson 5: Arc knob → backlight · Round 1.28" touch watch
Lesson 5 - A rotary knob that controls the backlight Goal: use a value widget (lv.arc) and VALUE_CHANGED to drive real hardware.
You’ll learn: VALUE_CHANGED, driving hardware
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 5 - A rotary knob that controls the backlight
# Goal: use a value widget (lv.arc) and VALUE_CHANGED to drive real hardware.
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
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)
knob = lv.arc(scr)
knob.set_size(210, 210)
knob.center()
knob.set_rotation(135)
knob.set_bg_angles(0, 270) # a 270 degree dial
knob.set_range(5, 100)
knob.set_value(100)
knob.set_style_arc_width(14, lv.PART.MAIN)
knob.set_style_arc_width(14, lv.PART.INDICATOR)
knob.set_style_arc_color(lv.color_hex(LINE), lv.PART.MAIN)
knob.set_style_arc_color(lv.color_hex(AMBER), lv.PART.INDICATOR)
knob.set_style_bg_color(lv.color_hex(0xFFFFFF), lv.PART.KNOB)
knob.set_style_pad_all(4, lv.PART.KNOB)
pct = label(scr, "100%", TEXT, 16, y=-6, zoom=2)
label(scr, lv.SYMBOL.IMAGE + " brightness", MUTED, 12, y=34)
def changed(e):
v = knob.get_value()
pct.set_text("%d%%" % v)
display.display.set_backlight(v) # display.display.set_backlight()
knob.add_event_cb(changed, lv.EVENT.VALUE_CHANGED, None)
print("Drag the knob around the edge to dim the screen.")
Comments