Skip to content

Commit 36e3a21

Browse files
committed
Add timeout to distance sensor reading
1 parent 9452baf commit 36e3a21

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/modulino/distance.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from time import sleep_ms, ticks_ms, ticks_diff
12
from .modulino import Modulino
23
from .lib.vl53l4cd import VL53L4CD
34

@@ -25,16 +26,19 @@ def __init__(self, i2c_bus = None, address: int | None = None) -> None:
2526
self.sensor.start_ranging()
2627

2728
@property
28-
def _distance_raw(self) -> int | None:
29+
def _distance_raw(self, timeout = 1000) -> int | None:
2930
"""
3031
Reads the raw distance value from the sensor and clears the interrupt.
3132
3233
Returns:
3334
int: The distance in centimeters.
3435
"""
35-
try:
36+
try:
37+
start = ticks_ms()
3638
while not self.sensor.data_ready:
37-
pass
39+
if ticks_diff(ticks_ms(), start) > timeout:
40+
raise OSError("Timeout waiting for sensor data")
41+
sleep_ms(1)
3842
self.sensor.clear_interrupt()
3943
sensor_value = self.sensor.distance
4044
return sensor_value

0 commit comments

Comments
 (0)