Skip to content

Commit 960c44b

Browse files
MatteoCnda1nedseb
andauthored
refactor(vl53l1x): Migrate radar_screen example to steami_screen. (#391)
* refactor(vl53l1x): Migrate radar_screen example to steami_screen. * refactor(vl53l1x): Clean up radar_screen.py by removing imports. refactor(vl53l1x): Removed unused imports and sys.path modification. * refactor(vl53l1x): Reorder micropython import statement. * ci: Trigger checks rerun. * fix(vl53l1x): Fix gauge/title draw order and subtitle label in radar_screen. Address Copilot review comments on #391: 1. Call screen.gauge() before screen.title() so the title text layers on top of the arc, as documented in gauge()'s docstring. 2. Change subtitle from "Distance" to "Proximity" — the gauge shows proximity (inverted distance: arc grows as object gets closer), so labelling it "Distance" was misleading. --------- Co-authored-by: Sébastien NEDJAR <sebastien@nedjar.com>
1 parent b097b7d commit 960c44b

1 file changed

Lines changed: 46 additions & 43 deletions

File tree

lib/vl53l1x/examples/radar_screen.py

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,80 @@
22
33
Displays a real-time distance bar on the screen. The closer the object,
44
the longer and brighter the bar, similar to a car parking sensor.
5-
Adapted for a round display bezel.
5+
Adapted for a round display bezel using steami_screen widgets.
66
"""
77

88
from time import sleep_ms
99

10+
import micropython
1011
import ssd1327
1112
from machine import I2C, SPI, Pin
13+
from steami_screen import DARK, GRAY, LIGHT, RED, Screen, SSD1327Display
1214
from vl53l1x import VL53L1X
1315

16+
# === Constants ===
1417
MAX_DISTANCE_MM = 1000
1518

16-
# Layout constants for the round screen
17-
BAR_X = 24
18-
BAR_Y = 70
19-
BAR_MAX_WIDTH = 80
20-
BAR_HEIGHT = 15
21-
TEXT_LBL_X = 28
22-
TEXT_LBL_Y = 35
23-
TEXT_VAL_X = 36
24-
TEXT_VAL_Y = 50
25-
TEXT_OUT_X = 16 # Shifted left to fit the longer "Out of range" text
26-
27-
i2c = I2C(1)
28-
tof = VL53L1X(i2c)
29-
19+
# === Display ===
3020
spi = SPI(1)
3121
dc = Pin("DATA_COMMAND_DISPLAY")
3222
res = Pin("RST_DISPLAY")
3323
cs = Pin("CS_DISPLAY")
24+
display = SSD1327Display(ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs))
25+
screen = Screen(display)
26+
27+
# === Sensor ===
28+
i2c = I2C(1)
29+
tof = VL53L1X(i2c)
30+
31+
32+
@micropython.native
33+
def compute_display(distance):
34+
"""Compute proximity and color from distance value.
35+
36+
Returns (proximity, color) or (None, None) if out of range.
37+
"""
38+
if distance > MAX_DISTANCE_MM:
39+
return None, None
40+
proximity = MAX_DISTANCE_MM - distance
41+
if proximity > 700:
42+
color = RED
43+
elif proximity > 400:
44+
color = LIGHT
45+
else:
46+
color = GRAY
47+
return proximity, color
3448

35-
display = ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs)
3649

3750
try:
3851
while True:
3952
distance = tof.read()
4053

41-
# Clear the display frame buffer
42-
display.fill(0)
43-
44-
# Clamp distance to maximum range and compute integer proximity
45-
clamped_dist = max(0, min(distance, MAX_DISTANCE_MM))
46-
proximity = MAX_DISTANCE_MM - clamped_dist
54+
screen.clear()
4755

48-
# Map proximity to bar width (max 80px) and brightness (max 15)
49-
bar_width = (80 * proximity) // MAX_DISTANCE_MM
50-
brightness = (15 * proximity) // MAX_DISTANCE_MM
56+
proximity, color = compute_display(distance)
5157

52-
# If the object is within the detection range, ensure minimum visibility
53-
if distance <= MAX_DISTANCE_MM:
54-
bar_width = max(1, bar_width)
55-
brightness = max(1, brightness)
58+
# Draw gauge first so that title/value text layers on top of the arc
59+
if proximity is None:
60+
screen.gauge(0, min_val=0, max_val=MAX_DISTANCE_MM, color=DARK)
61+
else:
62+
screen.gauge(proximity, min_val=0, max_val=MAX_DISTANCE_MM, color=color)
5663

57-
# Draw the outline box (1 pixel larger than the max bar size) at max brightness
58-
display.framebuf.rect(BAR_X - 1, BAR_Y - 1, BAR_MAX_WIDTH + 2, BAR_HEIGHT + 2, 15)
59-
# Draw the distance bar centered: x=24, y=70, max_width=80, height=15
60-
display.framebuf.fill_rect(BAR_X, BAR_Y, bar_width, BAR_HEIGHT, brightness)
64+
screen.title("RADAR")
6165

62-
# Display distance or "Out of range"
63-
if distance > MAX_DISTANCE_MM:
64-
display.text("Out of range", TEXT_OUT_X, TEXT_VAL_Y, 15)
66+
if proximity is None:
67+
screen.value("----", unit="mm")
68+
screen.subtitle("Out of range")
6569
else:
66-
display.text("{} mm".format(distance), TEXT_VAL_X, TEXT_VAL_Y, 15)
67-
68-
# Send the buffer to the physical screen
69-
display.show()
70+
screen.value(str(distance), unit="mm")
71+
screen.subtitle("Proximity")
7072

73+
screen.show()
7174
sleep_ms(50)
7275

7376
except KeyboardInterrupt:
7477
pass
78+
7579
finally:
76-
# Clear the screen on exit or error
77-
display.fill(0)
78-
display.show()
80+
screen.clear()
81+
screen.show()

0 commit comments

Comments
 (0)