From 7a0a89f1df619987169678b4ab52c7b433b7a31f Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 28 May 2025 13:01:57 -0500 Subject: [PATCH] catch the CRC check error and just retry --- examples/opt4048_oneshot.py | 18 +++++++++--------- examples/opt4048_simpletest.py | 12 ++++++++---- examples/opt4048_webserial.py | 20 ++++++++++++-------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/examples/opt4048_oneshot.py b/examples/opt4048_oneshot.py index da310d6..47906f3 100644 --- a/examples/opt4048_oneshot.py +++ b/examples/opt4048_oneshot.py @@ -34,16 +34,16 @@ # ok we finished the reading! try: CIEx, CIEy, lux = sensor.cie - except RuntimeError: - print("Error reading sensor data") - print("\nCIE Coordinates:") - print(f"CIE x:{CIEx}, y:{CIEy}, lux: {lux}", end=" ") + print("\nCIE Coordinates:") + print(f"CIE x:{CIEx}, y:{CIEy}, lux: {lux}", end=" ") - # Calculate and display color temperature - color_temp = sensor.calculate_color_temperature(CIEx, CIEy) - print(f"Color Temperature: {color_temp} K") - print(f"Time since last read: {time.monotonic() - timestamp} sec") - timestamp = time.monotonic() + # Calculate and display color temperature + color_temp = sensor.calculate_color_temperature(CIEx, CIEy) + print(f"Color Temperature: {color_temp} K") + print(f"Time since last read: {time.monotonic() - timestamp} sec") + timestamp = time.monotonic() + except RuntimeError: + print("Error reading sensor data") sensor.mode = Mode.AUTO_ONESHOT diff --git a/examples/opt4048_simpletest.py b/examples/opt4048_simpletest.py index c9b345a..d8b7903 100644 --- a/examples/opt4048_simpletest.py +++ b/examples/opt4048_simpletest.py @@ -22,7 +22,11 @@ sensor.conversion_time = ConversionTime.TIME_100MS sensor.mode = Mode.CONTINUOUS while True: - x, y, lux = sensor.cie - print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ") - print(f"K: {sensor.calculate_color_temperature(x,y)}") - time.sleep(1) + try: + x, y, lux = sensor.cie + print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ") + print(f"K: {sensor.calculate_color_temperature(x,y)}") + time.sleep(1) + except RuntimeError: + # CRC check failed while reading data + pass diff --git a/examples/opt4048_webserial.py b/examples/opt4048_webserial.py index 841e0b7..a1ba98f 100644 --- a/examples/opt4048_webserial.py +++ b/examples/opt4048_webserial.py @@ -31,11 +31,15 @@ last_read_time = 0 while True: if time.monotonic() > last_read_time + READ_INTERVAL: - last_read_time = time.monotonic() - x, y, lux = sensor.cie - print("---CIE Data---") - print(f"CIE x: {x}") - print(f"CIE y: {y}") - print(f"Lux: {lux}") - print(f"Color Temperature: {sensor.calculate_color_temperature(x,y)} K") - print("-------------") + try: + last_read_time = time.monotonic() + x, y, lux = sensor.cie + print("---CIE Data---") + print(f"CIE x: {x}") + print(f"CIE y: {y}") + print(f"Lux: {lux}") + print(f"Color Temperature: {sensor.calculate_color_temperature(x,y)} K") + print("-------------") + except RuntimeError: + # CRC check failed while reading data + pass