|
2 | 2 |
|
3 | 3 | Displays a real-time distance bar on the screen. The closer the object, |
4 | 4 | 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. |
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from time import sleep_ms |
9 | 9 |
|
| 10 | +import micropython |
10 | 11 | import ssd1327 |
11 | 12 | from machine import I2C, SPI, Pin |
| 13 | +from steami_screen import DARK, GRAY, LIGHT, RED, Screen, SSD1327Display |
12 | 14 | from vl53l1x import VL53L1X |
13 | 15 |
|
| 16 | +# === Constants === |
14 | 17 | MAX_DISTANCE_MM = 1000 |
15 | 18 |
|
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 === |
30 | 20 | spi = SPI(1) |
31 | 21 | dc = Pin("DATA_COMMAND_DISPLAY") |
32 | 22 | res = Pin("RST_DISPLAY") |
33 | 23 | 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 |
34 | 48 |
|
35 | | -display = ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs) |
36 | 49 |
|
37 | 50 | try: |
38 | 51 | while True: |
39 | 52 | distance = tof.read() |
40 | 53 |
|
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() |
47 | 55 |
|
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) |
51 | 57 |
|
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) |
56 | 63 |
|
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") |
61 | 65 |
|
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") |
65 | 69 | 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") |
70 | 72 |
|
| 73 | + screen.show() |
71 | 74 | sleep_ms(50) |
72 | 75 |
|
73 | 76 | except KeyboardInterrupt: |
74 | 77 | pass |
| 78 | + |
75 | 79 | 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