Skip to content

Commit dbb0134

Browse files
committed
fix(simulator): debug and correct starter snippet APIs for LVGL, pdwidgets, and pygraphics
1 parent 58b8721 commit dbb0134

1 file changed

Lines changed: 153 additions & 56 deletions

File tree

simulator/templates.js

Lines changed: 153 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def _create_btn(parent):
4242
btn_cls = getattr(lv, "button", getattr(lv, "btn", None))
4343
return btn_cls(parent)
4444
45+
# Clean active screen from previous runs
4546
scr = lv.screen_active()
47+
scr.clean()
4648
scr.set_style_bg_color(lv.color_hex(0x0F172A), 0)
4749
4850
# Card Container
@@ -57,7 +59,7 @@ card.set_style_pad_all(16, 0)
5759
5860
# Title Label
5961
title = lv.label(card)
60-
title.set_text("PyDevices · LVGL v9")
62+
title.set_text("PyDevices - LVGL v9")
6163
title.set_style_text_color(lv.color_hex(0xF8FAFC), 0)
6264
title.align(lv.ALIGN.TOP_MID, 0, 0)
6365
@@ -154,7 +156,9 @@ def _font(size):
154156
return f() if callable(f) else f
155157
return lv.font_default() if hasattr(lv, "font_default") else None
156158
159+
# Clean active screen from previous runs
157160
scr = lv.screen_active()
161+
scr.clean()
158162
scr.set_style_bg_color(lv.color_hex(0x0B0F19), 0)
159163
160164
# Temperature Arc (Interactive drag)
@@ -173,31 +177,31 @@ arc.set_style_arc_color(lv.color_hex(0xEC4899), lv.PART.INDICATOR)
173177
174178
# Labels
175179
lbl_temp = lv.label(scr)
176-
lbl_temp.set_text("22°C")
180+
lbl_temp.set_text("22 C")
177181
lbl_temp.set_style_text_color(lv.color_hex(0xF9FAFB), 0)
178182
f_temp = _font(28)
179183
if f_temp:
180184
lbl_temp.set_style_text_font(f_temp, 0)
181185
lbl_temp.align(lv.ALIGN.CENTER, 0, -10)
182186
183187
lbl_status = lv.label(scr)
184-
lbl_status.set_text("COMFORT · HEATING")
188+
lbl_status.set_text("COMFORT - HEATING")
185189
lbl_status.set_style_text_color(lv.color_hex(0xF472B6), 0)
186190
lbl_status.align(lv.ALIGN.CENTER, 0, 24)
187191
188192
def arc_event_cb(e):
189193
val = arc.get_value()
190-
lbl_temp.set_text(f"{val}°C")
194+
lbl_temp.set_text(f"{val} C")
191195
if val >= 25:
192-
lbl_status.set_text("HIGH · WARMING")
196+
lbl_status.set_text("HIGH - WARMING")
193197
arc.set_style_arc_color(lv.color_hex(0xEF4444), lv.PART.INDICATOR)
194198
elif val <= 18:
195-
lbl_status.set_text("ECO · COOLING")
199+
lbl_status.set_text("ECO - COOLING")
196200
arc.set_style_arc_color(lv.color_hex(0x3B82F6), lv.PART.INDICATOR)
197201
else:
198-
lbl_status.set_text("COMFORT · BALANCED")
202+
lbl_status.set_text("COMFORT - BALANCED")
199203
arc.set_style_arc_color(lv.color_hex(0xEC4899), lv.PART.INDICATOR)
200-
print(f"Target Temperature: {val}°C")
204+
print(f"Target Temperature: {val} C")
201205
202206
arc.add_event_cb(arc_event_cb, lv.EVENT.VALUE_CHANGED, None)
203207
print("Drag the outer ring to adjust temperature!")
@@ -227,34 +231,121 @@ display = pd.Display(display_drv, app)
227231
# Screen background
228232
screen = pd.Screen(display, bg=0x0842)
229233
230-
# Title Label
231-
lbl_title = pd.Label(screen, text="PYDEVICES SENSOR DECK", x=12, y=10, color=0x38BDF8)
234+
# Header Title
235+
lbl_title = pd.Label(
236+
screen,
237+
value="PDWIDGETS INSTRUMENT",
238+
x=16,
239+
y=12,
240+
align=pd.ALIGN.TOP_LEFT,
241+
text_height=pd.TEXT_SIZE.SMALL,
242+
fg=0x8C71,
243+
bg=screen.bg,
244+
)
245+
246+
# Gauge Widget
247+
gauge = pd.Gauge(
248+
screen,
249+
x=16,
250+
y=30,
251+
w=78,
252+
h=78,
253+
align=pd.ALIGN.TOP_LEFT,
254+
value=0.68,
255+
fg=0x156A,
256+
track_color=0x18E3,
257+
label="68%",
258+
)
259+
260+
# Switch Widget
261+
switch_label = pd.Label(
262+
screen,
263+
value="ONLINE",
264+
x=124,
265+
y=36,
266+
align=pd.ALIGN.TOP_LEFT,
267+
text_height=pd.TEXT_SIZE.SMALL,
268+
fg=0xFFFF,
269+
bg=screen.bg,
270+
)
271+
switch = pd.Switch(
272+
screen,
273+
x=124,
274+
y=54,
275+
w=68,
276+
h=28,
277+
align=pd.ALIGN.TOP_LEFT,
278+
value=True,
279+
on_color=0x04C6,
280+
off_color=0x31A6,
281+
knob_color=0xFFFF,
282+
)
283+
284+
def on_switch_change(s):
285+
switch_label.value = "ONLINE" if s.value else "MUTED"
286+
switch_label.fg = 0xFFFF if s.value else 0x8C71
287+
print(f"Switch toggled: {'ONLINE' if s.value else 'MUTED'}")
288+
289+
switch.set_change_cb(on_switch_change)
232290
233291
# Telemetry Readouts
234-
lbl_temp = pd.Label(screen, text="TEMP: 24.5 °C", x=12, y=34, color=0x4ADE80)
235-
lbl_pres = pd.Label(screen, text="PRES: 1013 hPa", x=12, y=54, color=0xFCD34D)
236-
lbl_hum = pd.Label(screen, text="HUM : 48.0 %", x=12, y=74, color=0x60A5FA)
292+
lbl_telemetry = pd.Label(
293+
screen,
294+
value="BUS TELEMETRY: 48 kHz",
295+
x=16,
296+
y=116,
297+
align=pd.ALIGN.TOP_LEFT,
298+
text_height=pd.TEXT_SIZE.SMALL,
299+
fg=0x35FA,
300+
bg=screen.bg,
301+
)
302+
303+
prog = pd.ProgressBar(
304+
screen,
305+
x=16,
306+
y=134,
307+
w=208,
308+
h=12,
309+
align=pd.ALIGN.TOP_LEFT,
310+
value=0.55,
311+
fg=0x35FA,
312+
bg=0x1082,
313+
)
237314
238315
# Interactive Slider
239-
lbl_slider = pd.Label(screen, text="OUTPUT GAIN: 65%", x=12, y=106, color=0xE2E8F0)
240-
slider = pd.Slider(screen, x=12, y=130, w=216, h=24, min_val=0, max_val=100, val=65)
241-
242-
def on_slider_change(val):
243-
lbl_slider.text = f"OUTPUT GAIN: {int(val)}%"
244-
print(f"Output Gain set to: {int(val)}%")
245-
246-
slider.on_change = on_slider_change
247-
248-
# Status Badge Button
249-
btn_status = pd.Button(screen, text="SYS: ACTIVE [OK]", x=12, y=175, w=216, h=36, bg=0x064E3B, fg=0x34D399)
250-
251-
def on_btn_click():
252-
print("System health check requested - All sensors operational.")
253-
btn_status.text = "SYS: HEALTHY 100%"
316+
lbl_slider = pd.Label(
317+
screen,
318+
value="GAIN DAMPING: 72%",
319+
x=16,
320+
y=158,
321+
align=pd.ALIGN.TOP_LEFT,
322+
text_height=pd.TEXT_SIZE.SMALL,
323+
fg=0xFD20,
324+
bg=screen.bg,
325+
)
326+
327+
slider = pd.Slider(
328+
screen,
329+
x=16,
330+
y=176,
331+
w=208,
332+
h=20,
333+
align=pd.ALIGN.TOP_LEFT,
334+
value=0.72,
335+
fg=0xF440,
336+
bg=0x2124,
337+
knob_color=0xFFFF,
338+
)
339+
340+
def on_slider_change(s):
341+
pct = int(s.value * 100)
342+
lbl_slider.value = f"GAIN DAMPING: {pct}%"
343+
gauge.value = s.value
344+
gauge.label = f"{pct}%"
345+
print(f"Gain adjusted to: {pct}%")
346+
347+
slider.set_change_cb(on_slider_change)
254348
255-
btn_status.on_click = on_btn_click
256-
257-
screen.show()
258349
print("pdwidgets Sensor Deck is live! Move the slider or click buttons.")
259350
`
260351
},
@@ -269,48 +360,54 @@ print("pdwidgets Sensor Deck is live! Move the slider or click buttons.")
269360
shape: "rectangle",
270361
deps: ["pydevices", "pydevices-pygraphics", "pydevices-palettes"],
271362
code: `# pygraphics: High-Performance Vector & FrameBuffer Graphics
272-
import math, time
273-
import pygraphics as pg
363+
import math
364+
from displaydev import color565
274365
from displaydev.psdisplay import PSDisplay
275-
from displaybuf import DisplayBuffer
366+
import pygraphics as pg
276367
277368
print("Initializing pygraphics vector canvas...")
278369
279-
display_drv = PSDisplay("display_canvas", width=320, height=240)
280-
ssd = DisplayBuffer(display_drv, DisplayBuffer.RGB565)
370+
width, height = 320, 240
371+
drv = PSDisplay("display_canvas", width=width, height=height)
372+
373+
# Create 16-bit RGB565 frame buffer
374+
buf = bytearray(width * height * 2)
375+
fb = pg.FrameBuffer(buf, width, height, pg.RGB565)
281376
282-
# Clear background to deep navy
283-
ssd.fill(pg.color565(10, 15, 30))
377+
# Fill background with dark slate
378+
fb.fill(color565(15, 23, 42))
284379
285-
cx, cy = 160, 120
380+
cx, cy = width // 2, height // 2
286381
287-
# Draw animated geometric rings and starburst
382+
# Draw concentric geometric circles & ellipses
288383
for r in range(20, 110, 15):
289-
color = pg.color565(56, 189, int(200 + r / 2))
290-
ssd.ellipse(cx, cy, r, int(r * 0.7), color, False)
384+
c = color565(56, 189, int(200 + r / 2))
385+
pg.ellipse(fb, cx, cy, r, int(r * 0.7), c)
291386
292-
# Draw radiating lines
387+
# Radiating vector lines
293388
for i in range(16):
294389
angle = i * (2 * math.pi / 16)
295390
x2 = int(cx + 100 * math.cos(angle))
296391
y2 = int(cy + 70 * math.sin(angle))
297-
ssd.line(cx, cy, x2, y2, pg.color565(245, 158, 11))
392+
pg.line(fb, cx, cy, x2, y2, color565(245, 158, 11))
298393
299-
# Central Orb
300-
ssd.fill_circle(cx, cy, 18, pg.color565(239, 68, 68))
301-
ssd.fill_circle(cx, cy, 8, pg.color565(255, 255, 255))
394+
# Central Orbs
395+
pg.circle(fb, cx, cy, 18, color565(239, 68, 68))
396+
pg.circle(fb, cx, cy, 8, color565(255, 255, 255))
302397
303-
# Corner Badges
304-
ssd.fill_rect(10, 10, 130, 26, pg.color565(30, 41, 59))
305-
ssd.rect(10, 10, 130, 26, pg.color565(71, 85, 105))
306-
ssd.text("PyGraphics v1.0", 16, 18, pg.color565(56, 189, 248))
398+
# Header Badge and Text
399+
pg.fill_rect(fb, 10, 10, 150, 24, color565(30, 41, 59))
400+
pg.rect(fb, 10, 10, 150, 24, color565(71, 85, 105))
401+
pg.text8(fb, "PyGraphics v1.0", 18, 18, color565(56, 189, 248))
307402
308-
ssd.fill_rect(10, 204, 150, 26, pg.color565(30, 41, 59))
309-
ssd.rect(10, 204, 150, 26, pg.color565(71, 85, 105))
310-
ssd.text("FPS: 60 | RGB565", 16, 212, pg.color565(74, 222, 128))
403+
# Footer Badge
404+
pg.fill_rect(fb, 10, height - 34, 180, 24, color565(30, 41, 59))
405+
pg.rect(fb, 10, height - 34, 180, 24, color565(71, 85, 105))
406+
pg.text8(fb, "RGB565 FrameBuffer", 18, height - 26, color565(74, 222, 128))
311407
312-
ssd.show()
313-
print("Drawn vector frame to display successfully!")
408+
# Blit frame to canvas display
409+
drv.blit_rect(buf, 0, 0, width, height)
410+
print("Rendered pygraphics vector FrameBuffer successfully to display!")
314411
`
315412
},
316413

0 commit comments

Comments
 (0)