Orbit loader · Round 1.28" touch watch
Showcase 1 - Orbit loader: dots orbiting on three tilted ellipses with comet tails
You’ll learn: tilted ellipses, comet tails
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Showcase and benchmarks. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Showcase 1 - Orbit loader: dots orbiting on three tilted ellipses with comet tails
import math
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
VIOLET = 0xB593FF
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
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(0x02060A), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
ORBITS = [(95, 0.35, 0.050, ACCENT), (75, 0.80, -0.070, BLUE), (55, 1.40, 0.095, VIOLET)]
TAIL = 6
bodies = []
for rx, tilt, speed, col in ORBITS:
trail = []
for k in range(TAIL):
d = dot(scr, 12 - k * 1.5, col)
d.set_style_opa(255 - k * 38, 0)
trail.append(d)
bodies.append({"rx": rx, "tilt": tilt, "speed": speed, "trail": trail, "hist": []})
core = dot(scr, 30, 0xFFFFFF)
core.set_style_shadow_width(30, 0)
core.set_style_shadow_color(lv.color_hex(ACCENT), 0)
anim(core, 200, 300, 900, lambda o, v: o.set_style_transform_scale(v, 0), repeat=-1, playback=900)
loading_label = label(scr, "loading", MUTED, 12, y=100)
t = [0.0]
def frame(_):
t[0] += 1
for b in bodies:
a = t[0] * b["speed"]
x = math.cos(a) * b["rx"]
y = math.sin(a) * b["rx"] * 0.45
# rotate the ellipse by its tilt
ct, st = math.cos(b["tilt"]), math.sin(b["tilt"])
h = b["hist"]
h.insert(0, (x * ct - y * st, x * st + y * ct))
del h[TAIL:]
for k, d in enumerate(b["trail"]):
px, py = h[min(k, len(h) - 1)]
d.align(lv.ALIGN.CENTER, int(px), int(py))
loading_label.set_text("loading" + "." * (int(t[0] / 10) % 4))
frame_timer = lv.timer_create(frame, 33, None)
Comments