Scales and meters · Round 1.28" touch watch
Widget gallery 4 - Scales and meters: speedometer, compass, linear scale
You’ll learn: speedometer, compass, linear scales
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Widget gallery. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Widget gallery 4 - Scales and meters: speedometer, compass, linear scale
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
AMBER = 0xF2B84B
RED = 0xFF6B6B
keep = [] # styles, buffers and callbacks LVGL uses must stay referenced
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 anim(obj, start, end, ms, setter, path=None, repeat=1, playback=0, delay=0, done=None):
"""Animate start -> end over ms, calling setter(obj, value). repeat=-1 loops forever."""
a = lv.anim_t()
a.init()
a.set_var(obj)
a.set_values(start, end)
a.set_duration(ms)
a.set_delay(delay)
a.set_custom_exec_cb(lambda _a, v: setter(obj, v))
a.set_path_cb(path or lv.anim_t.path_ease_in_out)
if repeat < 0:
a.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
elif repeat > 1:
a.set_repeat_count(repeat)
if playback:
a.set_reverse_duration(playback) # play back to start
if done:
a.set_completed_cb(lambda _a: done())
a.start()
keep.append(a)
return a
imu = bb_imu.IMU() # QMI8658 motion sensor, on the touch I2C bus
S = lv.scale
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_style_bg_opa(lv.OPA.TRANSP, 0)
# 1. Speedometer with coloured sections and an animated needle
t = tv.add_tile(0, 0, lv.DIR.RIGHT)
sp = lv.scale(t)
sp.set_size(210, 210)
sp.center()
sp.set_mode(S.MODE.ROUND_INNER)
sp.set_range(0, 200)
sp.set_angle_range(270)
sp.set_rotation(135)
sp.set_total_tick_count(41)
sp.set_major_tick_every(5)
sp.set_label_show(True)
sp.set_style_text_color(lv.color_hex(MUTED), lv.PART.INDICATOR)
sp.set_style_line_color(lv.color_hex(TEXT), lv.PART.INDICATOR)
sp.set_style_line_color(lv.color_hex(LINE), lv.PART.ITEMS)
sp.set_style_length(10, lv.PART.INDICATOR)
for lo, hi, col in ((0, 120, ACCENT), (120, 170, AMBER), (170, 200, RED)):
st = lv.style_t()
st.init()
st.set_arc_color(lv.color_hex(col))
st.set_arc_width(5)
sec = sp.add_section()
sec.set_range(lo, hi)
sec.set_style(lv.PART.MAIN, st)
keep.append(st)
needle = lv.line(sp)
needle.set_style_line_width(4, 0)
needle.set_style_line_color(lv.color_hex(RED), 0)
needle.set_style_line_rounded(True, 0)
dot(t, 14, TEXT)
kmh = label(t, "", TEXT, 16, y=50)
def speed(o, v):
sp.set_line_needle_value(needle, 85, v)
kmh.set_text("%d km/h" % v)
anim(sp, 0, 180, 2500, speed, repeat=-1, playback=2500)
# 2. Compass that follows the IMU (rotate the whole scale)
t = tv.add_tile(1, 0, lv.DIR.LEFT | lv.DIR.RIGHT)
cp = lv.scale(t)
cp.set_size(200, 200)
cp.center()
cp.set_mode(S.MODE.ROUND_INNER)
cp.set_range(0, 360)
cp.set_angle_range(360)
cp.set_total_tick_count(37)
cp.set_major_tick_every(9)
cp.set_label_show(True)
cp.set_text_src(["N", "E", "S", "W", "N", None])
cp.set_style_text_color(lv.color_hex(ACCENT), lv.PART.INDICATOR)
cp.set_style_line_color(lv.color_hex(MUTED), lv.PART.ITEMS)
label(t, lv.SYMBOL.UP, RED, 16, y=-80)
heading = label(t, "", TEXT, 16)
def compass(_):
# no magnetometer on this board: use roll from the IMU as a demo "heading"
p, r = imu.tilt()
deg = int(r) % 360
cp.set_rotation(270 - deg)
heading.set_text("%d deg" % deg)
compass_timer = lv.timer_create(compass, 60, None)
# 3. Linear scales (horizontal + vertical) with a moving marker
t = tv.add_tile(2, 0, lv.DIR.LEFT)
h = lv.scale(t)
h.set_size(170, 40)
h.align(lv.ALIGN.CENTER, 0, -30)
h.set_mode(S.MODE.HORIZONTAL_BOTTOM)
h.set_range(0, 100)
h.set_total_tick_count(21)
h.set_major_tick_every(5)
h.set_label_show(True)
h.set_style_text_color(lv.color_hex(MUTED), lv.PART.INDICATOR)
marker = dot(t, 12, AMBER, -85, -48)
anim(marker, -85, 85, 1600, lambda o, v: o.set_x(v), repeat=-1, playback=1600)
v = lv.scale(t)
v.set_size(40, 90)
v.align(lv.ALIGN.CENTER, 0, 50)
v.set_mode(S.MODE.VERTICAL_LEFT)
v.set_range(0, 40)
v.set_total_tick_count(9)
v.set_major_tick_every(4)
v.set_label_show(True)
v.set_style_text_color(lv.color_hex(MUTED), lv.PART.INDICATOR)
print("Swipe: speedometer, IMU compass, linear scales.")
Comments