|
13 | 13 |
|
14 | 14 | MAX_DISTANCE_MM = 1000 |
15 | 15 |
|
| 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 | + |
16 | 27 | i2c = I2C(1) |
17 | 28 | tof = VL53L1X(i2c) |
18 | 29 |
|
|
30 | 41 | # Clear the display frame buffer |
31 | 42 | display.fill(0) |
32 | 43 |
|
33 | | - # Clamp distance to maximum range to avoid negative ratios |
| 44 | + # Clamp distance to maximum range and compute integer proximity |
34 | 45 | clamped_dist = max(0, min(distance, MAX_DISTANCE_MM)) |
| 46 | + proximity = MAX_DISTANCE_MM - clamped_dist |
35 | 47 |
|
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 linearly to a maximum width of 80 pixels and brightness (15 levels) using integer math |
| 49 | + bar_width = (80 * proximity) // MAX_DISTANCE_MM |
| 50 | + brightness = (15 * proximity) // MAX_DISTANCE_MM |
42 | 51 |
|
43 | 52 | # If the object is within the detection range, ensure minimum visibility |
44 | | - if distance < MAX_DISTANCE_MM: |
| 53 | + if distance <= MAX_DISTANCE_MM: |
45 | 54 | bar_width = max(1, bar_width) |
46 | 55 | brightness = max(1, brightness) |
47 | 56 |
|
48 | 57 | # 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) |
50 | 59 | # 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) |
52 | 61 |
|
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) |
56 | 67 |
|
57 | 68 | # Send the buffer to the physical screen |
58 | 69 | display.show() |
|
0 commit comments