Lesson 12: Live chart · Round 1.28" touch watch
Lesson 12 - Live chart Goal: plot streaming data with lv.chart (3 series: accel X, Y, Z).
You’ll learn: lv.chart series, streaming data
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 12 - Live chart
# Goal: plot streaming data with lv.chart (3 series: accel X, Y, Z).
import lvgl as lv
import display
import bb_imu
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
PANEL = 0x0C1A12
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
RED = 0xFF6B6B
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)
imu = bb_imu.IMU() # QMI8658 motion sensor, on the touch I2C bus
chart = lv.chart(scr)
chart.set_size(180, 110)
chart.align(lv.ALIGN.CENTER, 0, 4)
chart.set_type(lv.chart.TYPE.LINE)
chart.set_point_count(40)
chart.set_axis_range(lv.chart.AXIS.PRIMARY_Y, -100, 100) # -1.0 .. +1.0 g (LVGL 9.4 name)
chart.set_update_mode(lv.chart.UPDATE_MODE.SHIFT)
chart.set_div_line_count(3, 0)
chart.set_style_bg_color(lv.color_hex(PANEL), 0)
chart.set_style_border_width(0, 0)
chart.set_style_size(0, 0, lv.PART.INDICATOR) # hide point markers
chart.set_style_line_width(2, lv.PART.ITEMS)
sx = chart.add_series(lv.color_hex(RED), lv.chart.AXIS.PRIMARY_Y)
sy = chart.add_series(lv.color_hex(ACCENT), lv.chart.AXIS.PRIMARY_Y)
sz = chart.add_series(lv.color_hex(BLUE), lv.chart.AXIS.PRIMARY_Y)
label(scr, "accel X Y Z", MUTED, 12, y=-72)
info = label(scr, "", TEXT, 12, y=74)
def sample(_):
x, y, z = imu.accel()
chart.set_next_value(sx, int(x * 100))
chart.set_next_value(sy, int(y * 100))
chart.set_next_value(sz, int(z * 100))
info.set_text("%+.2f %+.2f %+.2f g" % (x, y, z))
sample_timer = lv.timer_create(sample, 60, None)
print("Shake and rotate the watch to see the three axes.")
Comments