Skip to content

Commit 9a4e34e

Browse files
Kaanoz-enKaan Ozen
authored andcommitted
fix(vl53l1x): Optimize radar math and fix boundaries.
1 parent b7eb5e9 commit 9a4e34e

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

lib/vl53l1x/examples/radar_screen.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313

1414
MAX_DISTANCE_MM = 1000
1515

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+
1627
i2c = I2C(1)
1728
tof = VL53L1X(i2c)
1829

@@ -30,29 +41,29 @@
3041
# Clear the display frame buffer
3142
display.fill(0)
3243

33-
# Clamp distance to maximum range to avoid negative ratios
44+
# Clamp distance to maximum range and compute integer proximity
3445
clamped_dist = max(0, min(distance, MAX_DISTANCE_MM))
46+
proximity = MAX_DISTANCE_MM - clamped_dist
3547

36-
# Calculate ratio (1.0 = extremely close, 0.0 = MAX_DISTANCE_MM or further)
37-
ratio = 1.0 - (clamped_dist / MAX_DISTANCE_MM)
38-
39-
# Map ratio to a maximum width of 80 pixels (to fit the round screen) and brightness (15 levels)
40-
bar_width = int(80 * ratio)
41-
brightness = int(15 * ratio)
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
4251

4352
# If the object is within the detection range, ensure minimum visibility
44-
if distance < MAX_DISTANCE_MM:
53+
if distance <= MAX_DISTANCE_MM:
4554
bar_width = max(1, bar_width)
4655
brightness = max(1, brightness)
4756

4857
# Draw the outline box (1 pixel larger than the max bar size) at max brightness
49-
display.framebuf.rect(23, 69, 82, 17, 15)
58+
display.framebuf.rect(BAR_X - 1, BAR_Y - 1, BAR_MAX_WIDTH + 2, BAR_HEIGHT + 2, 15)
5059
# Draw the distance bar centered: x=24, y=70, max_width=80, height=15
51-
display.framebuf.fill_rect(24, 70, bar_width, 15, brightness)
60+
display.framebuf.fill_rect(BAR_X, BAR_Y, bar_width, BAR_HEIGHT, brightness)
5261

53-
# Display the text information centered in the round screen
54-
display.text("Distance:", 28, 35, 15)
55-
display.text("{} mm".format(distance), 36, 50, 15)
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)
65+
else:
66+
display.text("{} mm".format(distance), TEXT_VAL_X, TEXT_VAL_Y, 15)
5667

5768
# Send the buffer to the physical screen
5869
display.show()

0 commit comments

Comments
 (0)