Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 48 additions & 45 deletions lib/vl53l1x/examples/radar_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,80 @@

Displays a real-time distance bar on the screen. The closer the object,
the longer and brighter the bar, similar to a car parking sensor.
Adapted for a round display bezel.
Adapted for a round display bezel using steami_screen widgets.
"""

import sys

import micropython

sys.path.insert(0, "/remote")

Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.path.insert(0, "/remote") is unusual for a library example and can change module resolution (shadowing installed packages) for anyone running this on-device. Since this example already imports from standard locations (vl53l1x, steami_screen, etc.), please remove this path mutation (or gate it behind a clearly documented, opt-in dev-only flag).

Suggested change
import sys
import micropython
sys.path.insert(0, "/remote")
import micropython

Copilot uses AI. Check for mistakes.
from time import sleep_ms

import ssd1327
from machine import I2C, SPI, Pin
from steami_screen import DARK, GRAY, LIGHT, RED, Screen, SSD1327Display
from vl53l1x import VL53L1X

# === Constants ===
MAX_DISTANCE_MM = 1000

# Layout constants for the round screen
BAR_X = 24
BAR_Y = 70
BAR_MAX_WIDTH = 80
BAR_HEIGHT = 15
TEXT_LBL_X = 28
TEXT_LBL_Y = 35
TEXT_VAL_X = 36
TEXT_VAL_Y = 50
TEXT_OUT_X = 16 # Shifted left to fit the longer "Out of range" text

i2c = I2C(1)
tof = VL53L1X(i2c)

# === Display ===
spi = SPI(1)
dc = Pin("DATA_COMMAND_DISPLAY")
res = Pin("RST_DISPLAY")
cs = Pin("CS_DISPLAY")
display = SSD1327Display(ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs))
screen = Screen(display)

# === Sensor ===
i2c = I2C(1)
tof = VL53L1X(i2c)

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

try:
while True:
distance = tof.read()
@micropython.native
def compute_display(distance):
"""Compute proximity and color from distance value.

# Clear the display frame buffer
display.fill(0)
Returns (proximity, color) or (None, None) if out of range.
"""
if distance > MAX_DISTANCE_MM:
return None, None
proximity = MAX_DISTANCE_MM - distance
if proximity > 700:
color = RED
elif proximity > 400:
color = LIGHT
else:
color = GRAY
return proximity, color

# Clamp distance to maximum range and compute integer proximity
clamped_dist = max(0, min(distance, MAX_DISTANCE_MM))
proximity = MAX_DISTANCE_MM - clamped_dist

# Map proximity to bar width (max 80px) and brightness (max 15)
bar_width = (80 * proximity) // MAX_DISTANCE_MM
brightness = (15 * proximity) // MAX_DISTANCE_MM
try:
while True:
distance = tof.read()

# If the object is within the detection range, ensure minimum visibility
if distance <= MAX_DISTANCE_MM:
bar_width = max(1, bar_width)
brightness = max(1, brightness)
screen.clear()
screen.title("RADAR")

# Draw the outline box (1 pixel larger than the max bar size) at max brightness
display.framebuf.rect(BAR_X - 1, BAR_Y - 1, BAR_MAX_WIDTH + 2, BAR_HEIGHT + 2, 15)
# Draw the distance bar centered: x=24, y=70, max_width=80, height=15
display.framebuf.fill_rect(BAR_X, BAR_Y, bar_width, BAR_HEIGHT, brightness)
proximity, color = compute_display(distance)

# Display distance or "Out of range"
if distance > MAX_DISTANCE_MM:
display.text("Out of range", TEXT_OUT_X, TEXT_VAL_Y, 15)
if proximity is None:
screen.gauge(0, min_val=0, max_val=MAX_DISTANCE_MM, color=DARK)
screen.value("----", unit="mm")
Comment on lines +54 to +67
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen.gauge()'s docstring notes it should be called before title() so the title text layers on top of the arc. Here screen.title("RADAR") is drawn first, then screen.gauge(...), which can cause the gauge arc to overwrite the title depending on geometry. Please swap the call order (draw gauge first, then title/value/subtitle).

Copilot uses AI. Check for mistakes.
screen.subtitle("Out of range")
else:
display.text("{} mm".format(distance), TEXT_VAL_X, TEXT_VAL_Y, 15)

# Send the buffer to the physical screen
display.show()
screen.gauge(proximity, min_val=0, max_val=MAX_DISTANCE_MM, color=color)
screen.value(str(distance), unit="mm")
screen.subtitle("Distance")
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gauge min/max labels currently reflect proximity (0..1000) because proximity is passed as the gauge value, but the center value() and subtitle are labeled as distance in mm. This makes the on-screen gauge labels misleading (0 means “far”, not “0 mm”). Consider either (a) passing distance to gauge() and inverting the range (so the arc grows as distance decreases), or (b) updating the labels/subtitle to explicitly indicate proximity rather than distance.

Suggested change
screen.subtitle("Distance")
screen.subtitle("Distance (arc=proximity)")

Copilot uses AI. Check for mistakes.

screen.show()
sleep_ms(50)

except KeyboardInterrupt:
pass

finally:
# Clear the screen on exit or error
display.fill(0)
display.show()
screen.clear()
screen.show()
Loading