Lesson 11: IMU bubble level · Round 1.28" touch watch
Lesson 11 - Sensors: a bubble level with the QMI8658 IMU Goal: read real sensor data in an lv.timer and map it to the UI. bb_imu (frozen in the firmware) gives accel in g and tilt in degrees.
You’ll learn: bb_imu, lv.timer, smoothing
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Learning roadmap, Level 4 · Sensors and data. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Lesson 11 - Sensors: a bubble level with the QMI8658 IMU
# Goal: read real sensor data in an lv.timer and map it to the UI.
# bb_imu (frozen in the firmware) gives accel in g and tilt in degrees.
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
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
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
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)
imu = bb_imu.IMU() # QMI8658 motion sensor, on the touch I2C bus
for r in (200, 130, 60): # target rings
o = lv.obj(scr)
o.set_size(r, r)
o.center()
o.set_style_radius(lv.RADIUS_CIRCLE, 0)
o.set_style_bg_opa(lv.OPA.TRANSP, 0)
o.set_style_border_color(lv.color_hex(LINE), 0)
o.set_style_border_width(2, 0)
o.remove_flag(lv.obj.FLAG.CLICKABLE)
bubble = dot(scr, 34, ACCENT)
bubble.set_style_shadow_width(20, 0)
bubble.set_style_shadow_color(lv.color_hex(ACCENT), 0)
readout = label(scr, "", TEXT, 12, y=80)
pos = [0.0, 0.0]
def update(_):
ax, ay, az = imu.accel()
# target position: 80 px per g, smoothed (low-pass filter) so it glides
tx, ty = max(-80, min(80, -ax * 80)), max(-80, min(80, ay * 80))
pos[0] += (tx - pos[0]) * 0.3
pos[1] += (ty - pos[1]) * 0.3
bubble.align(lv.ALIGN.CENTER, int(pos[0]), int(pos[1]))
level = abs(pos[0]) < 6 and abs(pos[1]) < 6
bubble.set_style_bg_color(lv.color_hex(ACCENT if level else AMBER), 0)
p, r = imu.tilt()
readout.set_text("LEVEL" if level else "%+d / %+d deg" % (p, r))
update_timer = lv.timer_create(update, 40, None) # 25 updates per second
print("Hold the watch flat and tilt it.")
Comments