Skip to content

Commit c53e048

Browse files
Kaanoz-enKaan Ozen
authored andcommitted
fix(vl53l1x): Optimize radar math, fix boundaries
1 parent b7eb5e9 commit c53e048

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

lib/vl53l1x/examples/radar_screen.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@
3030
# Clear the display frame buffer
3131
display.fill(0)
3232

33-
# Clamp distance to maximum range to avoid negative ratios
33+
# Clamp distance to maximum range and compute integer proximity
3434
clamped_dist = max(0, min(distance, MAX_DISTANCE_MM))
35+
proximity = MAX_DISTANCE_MM - clamped_dist
3536

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)
37+
# Map proximity linearly to a maximum width of 80 pixels and brightness (15 levels) using integer math
38+
bar_width = (80 * proximity) // MAX_DISTANCE_MM
39+
brightness = (15 * proximity) // MAX_DISTANCE_MM
4240

4341
# If the object is within the detection range, ensure minimum visibility
44-
if distance < MAX_DISTANCE_MM:
42+
if distance <= MAX_DISTANCE_MM:
4543
bar_width = max(1, bar_width)
4644
brightness = max(1, brightness)
4745

0 commit comments

Comments
 (0)