3D cube FPS test · Round 1.28" touch watch
Showcase 6 - 3D cube FPS test Spins a 3D cube in four timed phases and measures real frames per second: 1. LVGL objects - 12 lv.line edges, LVGL only redraws the areas that changed 2. Canvas frame - clear + draw into a 240x240 canvas, push the WHOLE frame (raw throughput, like the TFT_eSPI cube demo) 3. Stress - three cubes at once (objects mode) 4. Filled faces - shaded triangles with back-face culling on the canvas Each phase drives LVGL directly with lv.refr_now() so it is not capped by the normal 30 FPS refresh timer. Results are shown on the watch and printed. Press Stop in the IDE to abort.
You’ll learn: 4 timed phases: objects, full-frame canvas, 3 cubes, shaded faces
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 6 - 3D cube FPS test
# Spins a 3D cube in four timed phases and measures real frames per second:
# 1. LVGL objects - 12 lv.line edges, LVGL only redraws the areas that changed
# 2. Canvas frame - clear + draw into a 240x240 canvas, push the WHOLE frame (raw throughput,
# like the TFT_eSPI cube demo)
# 3. Stress - three cubes at once (objects mode)
# 4. Filled faces - shaded triangles with back-face culling on the canvas
# Each phase drives LVGL directly with lv.refr_now() so it is not capped by the normal 30 FPS
# refresh timer. Results are shown on the watch and printed. Press Stop in the IDE to abort.
import math
import time
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
BG = 0x05110A
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
BLUE = 0x6CB8FF
AMBER = 0xF2B84B
RED = 0xFF6B6B
VIOLET = 0xB593FF
keep = [] # styles, buffers and callbacks LVGL uses must stay referenced
def new_screen(bg=BG):
"""Load a fresh screen and delete the old one (and its running animations)."""
lv.anim_delete_all()
s = lv.obj()
s.set_style_bg_color(lv.color_hex(bg), 0)
s.remove_flag(lv.obj.FLAG.SCROLLABLE)
lv.screen_load_anim(s, lv.SCREEN_LOAD_ANIM.NONE, 0, 0, True)
return s
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 points(*xy):
"""[(x, y), ...] -> lv.point_precise_t list for lv.line.set_points()."""
out = []
for x, y in xy:
p = lv.point_precise_t()
p.x, p.y = int(x), int(y)
out.append(p)
return out
PHASE_SECONDS = 5
SIZE = 58 # half edge length in pixels (before perspective)
VERTS = [(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1),
(-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)]
EDGES = [(0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7)]
FACES = [((0, 1, 2, 3), 0xFF6B6B), ((5, 4, 7, 6), 0x46E07C), ((4, 0, 3, 7), 0x6CB8FF),
((1, 5, 6, 2), 0xF2B84B), ((4, 5, 1, 0), 0xB593FF), ((3, 2, 6, 7), 0xFF4FA3)]
def project(ax, ay, az, scale, cx=120, cy=120):
"""Rotate the 8 vertices and project them to screen coordinates (+ depth)."""
sx, cxr = math.sin(ax), math.cos(ax)
sy, cyr = math.sin(ay), math.cos(ay)
sz, czr = math.sin(az), math.cos(az)
out = []
for x, y, z in VERTS:
y, z = y * cxr - z * sx, y * sx + z * cxr # rotate around X
x, z = x * cyr + z * sy, -x * sy + z * cyr # rotate around Y
x, y = x * czr - y * sz, x * sz + y * czr # rotate around Z
f = 3.2 / (z + 4.2) # perspective
out.append((cx + x * scale * f, cy + y * scale * f, z))
return out
# ------------------------------------------------------------------ shared HUD
scr = new_screen(bg=0x02060A)
hud = label(scr, "", ACCENT, 14, y=88)
title = label(scr, "", MUTED, 12, y=-96)
disp = lv.display_get_default()
frames = [0]
def on_refresh(e):
frames[0] += 1
disp.add_event_cb(on_refresh, lv.EVENT.REFR_READY, None)
keep.append(on_refresh)
def make_lines(color, parent=None):
lines = []
for _ in EDGES:
ln = lv.line(parent or scr)
ln.set_style_line_width(3, 0)
ln.set_style_line_color(lv.color_hex(color), 0)
ln.set_style_line_rounded(True, 0)
lines.append(ln)
return lines
def set_edges(lines, pts):
for ln, (a, b) in zip(lines, EDGES):
ln.set_points(points(pts[a][:2], pts[b][:2]), 2)
def run_phase(name, step, seconds=PHASE_SECONDS):
"""Call step(t) then force a refresh as fast as possible. Returns (fps, ms/frame, cpu%)."""
title.set_text(name)
frames[0] = 0
n = 0
t0 = time.ticks_ms()
last = t0
busy = 0
while time.ticks_diff(time.ticks_ms(), t0) < seconds * 1000:
a = time.ticks_ms()
step(n)
lv.refr_now(None)
b = time.ticks_ms()
busy += time.ticks_diff(b, a)
n += 1
lv.tick_inc(time.ticks_diff(b, last))
last = b
if n % 10 == 0:
el = time.ticks_diff(b, t0) / 1000
hud.set_text("%.1f FPS" % (n / el))
el = time.ticks_diff(time.ticks_ms(), t0) / 1000
fps = n / el
return fps, 1000 / fps, min(100, busy * 100 / (el * 1000))
results = []
handler = display.handler
if handler:
handler.disable() # we drive LVGL ourselves - no 30 FPS cap
try:
# ---------------------------------------------------------------- 1. objects
lines = make_lines(ACCENT)
dots = [dot(scr, 8, 0xFFFFFF) for _ in VERTS]
def objects(n):
pts = project(n * 0.05, n * 0.037, n * 0.021, SIZE)
set_edges(lines, pts)
for d, (x, y, z) in zip(dots, pts):
d.set_pos(int(x) - 4, int(y) - 4)
results.append(("LVGL objects",) + run_phase("1/4 LVGL objects (partial redraw)", objects))
for o in lines + dots:
o.delete()
# ---------------------------------------------------------------- 2. canvas full frame
W = H = 240
buf = lv.draw_buf_create(W, H, lv.COLOR_FORMAT.RGB565, 0)
cv = lv.canvas(scr)
cv.set_draw_buf(buf)
cv.set_pos(0, 0)
cv.move_background()
keep.append(buf)
layer = lv.layer_t()
line_dsc = lv.draw_line_dsc_t()
line_dsc.init()
line_dsc.width = 3
line_dsc.round_start = line_dsc.round_end = 1
bg = lv.color_hex(0x02060A)
def canvas_frame(n, cubes=((0, 0, 1.0, BLUE),)):
cv.fill_bg(bg, lv.OPA.COVER)
cv.init_layer(layer)
for i, (ox, oy, k, col) in enumerate(cubes):
pts = project(n * 0.05 + i, n * 0.037 + i * 0.5, n * 0.021, SIZE * k, 120 + ox, 120 + oy)
line_dsc.color = lv.color_hex(col)
for a, b in EDGES:
line_dsc.p1.x, line_dsc.p1.y = pts[a][0], pts[a][1]
line_dsc.p2.x, line_dsc.p2.y = pts[b][0], pts[b][1]
lv.draw_line(layer, line_dsc)
cv.finish_layer(layer)
results.append(("Canvas full frame",) + run_phase("2/4 canvas full frame (240x240)", canvas_frame))
# ---------------------------------------------------------------- 3. stress: 3 cubes
trio = ((-45, -35, 0.55, RED), (45, -35, 0.55, AMBER), (0, 40, 0.55, ACCENT))
results.append(("3 cubes (canvas)",) + run_phase("3/4 stress: 3 cubes", lambda n: canvas_frame(n, trio)))
# ---------------------------------------------------------------- 4. filled, shaded faces
tri = lv.draw_triangle_dsc_t()
tri.init()
P = [lv.point_precise_t() for _ in range(3)]
def filled(n):
pts = project(n * 0.04, n * 0.031, n * 0.017, SIZE)
cv.fill_bg(bg, lv.OPA.COVER)
cv.init_layer(layer)
for idx, col in FACES:
a, b, c, d = (pts[i] for i in idx)
# back-face culling: skip faces turned away (2D cross product)
if (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) >= 0:
continue
shade = max(0.35, min(1.0, 0.75 - (a[2] + c[2]) * 0.25))
r, g, bl = (col >> 16) & 255, (col >> 8) & 255, col & 255
tri.color = lv.color_make(int(r * shade), int(g * shade), int(bl * shade))
for t1, t2, t3 in ((a, b, c), (a, c, d)):
for p, q in zip(P, (t1, t2, t3)):
p.x, p.y = q[0], q[1]
tri.p = P
lv.draw_triangle(layer, tri)
cv.finish_layer(layer)
results.append(("Filled faces",) + run_phase("4/4 filled shaded faces", filled))
finally:
if handler:
handler.enable()
# ------------------------------------------------------------------ results
print("\n3D cube FPS test - Waveshare ESP32-S3 1.28\" GC9A01, draw buffers %s lines x2, LVGL %d.%d"
% (getattr(display, "BUFFER_LINES", "default"), lv.version_major(), lv.version_minor()))
print("%-20s %8s %10s %6s" % ("phase", "FPS", "ms/frame", "CPU"))
for name, fps, ms, cpu in results:
print("%-20s %8.1f %10.1f %5.0f%%" % (name, fps, ms, cpu))
full = [r for r in results if r[0] == "Canvas full frame"]
if full:
print("full-frame throughput: %.2f MB/s (240x240 RGB565 = 115 KB/frame)" % (full[0][1] * 240 * 240 * 2 / 1e6))
res = new_screen(bg=0x05110A)
label(res, "3D CUBE FPS", MUTED, 12, y=-80)
for i, (name, fps, ms, cpu) in enumerate(results):
label(res, "%-17s %5.1f" % (name, fps), TEXT if i else ACCENT, 14, y=-48 + i * 24)
label(res, "frames per second - F5 to rerun", MUTED, 12, y=58)
# Idle demo: a small cube spinning at the normal (TaskHandler) refresh rate
idle = make_lines(VIOLET, res)
step = [0]
def spin(_):
step[0] += 1
set_edges(idle, project(step[0] * 0.03, step[0] * 0.02, 0, 22, 120, 178))
spin_timer = lv.timer_create(spin, 33, None)
Comments