Skip to content

Commit c9846fc

Browse files
authored
Merge pull request #3 from FoamyGuy/catch_crc_check_error
catch the CRC check error and just retry
2 parents 2e76865 + 7a0a89f commit c9846fc

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

examples/opt4048_oneshot.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
# ok we finished the reading!
3535
try:
3636
CIEx, CIEy, lux = sensor.cie
37-
except RuntimeError:
38-
print("Error reading sensor data")
3937

40-
print("\nCIE Coordinates:")
41-
print(f"CIE x:{CIEx}, y:{CIEy}, lux: {lux}", end=" ")
38+
print("\nCIE Coordinates:")
39+
print(f"CIE x:{CIEx}, y:{CIEy}, lux: {lux}", end=" ")
4240

43-
# Calculate and display color temperature
44-
color_temp = sensor.calculate_color_temperature(CIEx, CIEy)
45-
print(f"Color Temperature: {color_temp} K")
46-
print(f"Time since last read: {time.monotonic() - timestamp} sec")
47-
timestamp = time.monotonic()
41+
# Calculate and display color temperature
42+
color_temp = sensor.calculate_color_temperature(CIEx, CIEy)
43+
print(f"Color Temperature: {color_temp} K")
44+
print(f"Time since last read: {time.monotonic() - timestamp} sec")
45+
timestamp = time.monotonic()
46+
except RuntimeError:
47+
print("Error reading sensor data")
4848

4949
sensor.mode = Mode.AUTO_ONESHOT

examples/opt4048_simpletest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
sensor.conversion_time = ConversionTime.TIME_100MS
2323
sensor.mode = Mode.CONTINUOUS
2424
while True:
25-
x, y, lux = sensor.cie
26-
print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ")
27-
print(f"K: {sensor.calculate_color_temperature(x,y)}")
28-
time.sleep(1)
25+
try:
26+
x, y, lux = sensor.cie
27+
print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ")
28+
print(f"K: {sensor.calculate_color_temperature(x,y)}")
29+
time.sleep(1)
30+
except RuntimeError:
31+
# CRC check failed while reading data
32+
pass

examples/opt4048_webserial.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@
3131
last_read_time = 0
3232
while True:
3333
if time.monotonic() > last_read_time + READ_INTERVAL:
34-
last_read_time = time.monotonic()
35-
x, y, lux = sensor.cie
36-
print("---CIE Data---")
37-
print(f"CIE x: {x}")
38-
print(f"CIE y: {y}")
39-
print(f"Lux: {lux}")
40-
print(f"Color Temperature: {sensor.calculate_color_temperature(x,y)} K")
41-
print("-------------")
34+
try:
35+
last_read_time = time.monotonic()
36+
x, y, lux = sensor.cie
37+
print("---CIE Data---")
38+
print(f"CIE x: {x}")
39+
print(f"CIE y: {y}")
40+
print(f"Lux: {lux}")
41+
print(f"Color Temperature: {sensor.calculate_color_temperature(x,y)} K")
42+
print("-------------")
43+
except RuntimeError:
44+
# CRC check failed while reading data
45+
pass

0 commit comments

Comments
 (0)