diff --git a/README.rst b/README.rst index 158dec4..61d9f60 100644 --- a/README.rst +++ b/README.rst @@ -109,7 +109,7 @@ Usage Example sensor.conversion_time = ConversionTime.TIME_100MS sensor.mode = Mode.CONTINUOUS while True: - x, y, lux = sensor.get_cie() + 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) diff --git a/adafruit_opt4048.py b/adafruit_opt4048.py index fa47076..7a154d0 100644 --- a/adafruit_opt4048.py +++ b/adafruit_opt4048.py @@ -582,7 +582,92 @@ def threshold_channel(self, value): raise ValueError("Threshold channel must be an integer between 0 and 3") self._threshold_channel = value - def get_channels_raw(self): + @property + def threshold_low(self): + """Get the current low threshold value. + + Returns the current low threshold value as a 32-bit integer. + This value determines when a low threshold interrupt is generated + when interrupt_direction is False. + """ + # Read the exponent and mantissa from the threshold low register + exponent = self._threshold_low_exponent + mantissa = self._threshold_low_mantissa + print(f"exponent: {exponent} mantissa: {mantissa}") + # Calculate ADC code value by applying the exponent as a bit shift + # ADD 8 to the exponent as per datasheet equations 12-13 + return mantissa << (8 + exponent) + + @threshold_low.setter + def threshold_low(self, value): + """Set the low threshold value for interrupt generation. + + :param int value: The low threshold value as a 32-bit integer + """ + # Find the appropriate exponent and mantissa values that represent the threshold + exponent = 0 + mantissa = value + + # The mantissa needs to fit in 12 bits, so we start by shifting right + # to determine how many shifts we need (which gives us the exponent) + # Note that the threshold registers already have 8 added to exponent + # internally so we first subtract 8 from our target exponent + if mantissa > 0xFFF: # If value won't fit in 12 bits + while mantissa > 0xFFF and exponent < 15: + mantissa >>= 1 + exponent += 1 + if mantissa > 0xFFF: # If still won't fit with max exponent, clamp + mantissa = 0xFFF + exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally + + # Write the exponent and mantissa to the register + self._threshold_low_exponent = exponent + self._threshold_low_mantissa = mantissa + + @property + def threshold_high(self): + """Get the current high threshold value. + + Returns the current high threshold value as a 32-bit integer. + This value determines when a high threshold interrupt is generated + when interrupt_direction is True. + """ + # Read the exponent and mantissa from the threshold high register + exponent = self._threshold_high_exponent + mantissa = self._threshold_high_mantissa + + # Calculate ADC code value by applying the exponent as a bit shift + # ADD 8 to the exponent as per datasheet equations 10-11 + return mantissa << (8 + exponent) + + @threshold_high.setter + def threshold_high(self, value): + """Set the high threshold value for interrupt generation. + + :param int value: The high threshold value as a 32-bit integer + """ + # Find the appropriate exponent and mantissa values that represent the threshold + exponent = 0 + mantissa = value + + # The mantissa needs to fit in 12 bits, so we start by shifting right + # to determine how many shifts we need (which gives us the exponent) + # Note that the threshold registers already have 8 added to exponent + # internally so we first subtract 8 from our target exponent + if mantissa > 0xFFF: # If value won't fit in 12 bits + while mantissa > 0xFFF and exponent < 15: + mantissa >>= 1 + exponent += 1 + if mantissa > 0xFFF: # If still won't fit with max exponent, clamp + mantissa = 0xFFF + exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally + + # Write the exponent and mantissa to the register + self._threshold_high_exponent = exponent + self._threshold_high_mantissa = mantissa + + @property + def all_channels(self): """Read all four channels, verify CRC, and return raw ADC code values. Reads registers for channels 0-3 in one burst, checks the CRC bits for each, @@ -681,90 +766,7 @@ def get_channels_raw(self): return tuple(channels) @property - def threshold_low(self): - """Get the current low threshold value. - - Returns the current low threshold value as a 32-bit integer. - This value determines when a low threshold interrupt is generated - when interrupt_direction is False. - """ - # Read the exponent and mantissa from the threshold low register - exponent = self._threshold_low_exponent - mantissa = self._threshold_low_mantissa - print(f"exponent: {exponent} mantissa: {mantissa}") - # Calculate ADC code value by applying the exponent as a bit shift - # ADD 8 to the exponent as per datasheet equations 12-13 - return mantissa << (8 + exponent) - - @threshold_low.setter - def threshold_low(self, value): - """Set the low threshold value for interrupt generation. - - :param int value: The low threshold value as a 32-bit integer - """ - # Find the appropriate exponent and mantissa values that represent the threshold - exponent = 0 - mantissa = value - - # The mantissa needs to fit in 12 bits, so we start by shifting right - # to determine how many shifts we need (which gives us the exponent) - # Note that the threshold registers already have 8 added to exponent - # internally so we first subtract 8 from our target exponent - if mantissa > 0xFFF: # If value won't fit in 12 bits - while mantissa > 0xFFF and exponent < 15: - mantissa >>= 1 - exponent += 1 - if mantissa > 0xFFF: # If still won't fit with max exponent, clamp - mantissa = 0xFFF - exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally - - # Write the exponent and mantissa to the register - self._threshold_low_exponent = exponent - self._threshold_low_mantissa = mantissa - - @property - def threshold_high(self): - """Get the current high threshold value. - - Returns the current high threshold value as a 32-bit integer. - This value determines when a high threshold interrupt is generated - when interrupt_direction is True. - """ - # Read the exponent and mantissa from the threshold high register - exponent = self._threshold_high_exponent - mantissa = self._threshold_high_mantissa - - # Calculate ADC code value by applying the exponent as a bit shift - # ADD 8 to the exponent as per datasheet equations 10-11 - return mantissa << (8 + exponent) - - @threshold_high.setter - def threshold_high(self, value): - """Set the high threshold value for interrupt generation. - - :param int value: The high threshold value as a 32-bit integer - """ - # Find the appropriate exponent and mantissa values that represent the threshold - exponent = 0 - mantissa = value - - # The mantissa needs to fit in 12 bits, so we start by shifting right - # to determine how many shifts we need (which gives us the exponent) - # Note that the threshold registers already have 8 added to exponent - # internally so we first subtract 8 from our target exponent - if mantissa > 0xFFF: # If value won't fit in 12 bits - while mantissa > 0xFFF and exponent < 15: - mantissa >>= 1 - exponent += 1 - if mantissa > 0xFFF: # If still won't fit with max exponent, clamp - mantissa = 0xFFF - exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally - - # Write the exponent and mantissa to the register - self._threshold_high_exponent = exponent - self._threshold_high_mantissa = mantissa - - def get_cie(self): + def cie(self): """Calculate CIE chromaticity coordinates and lux from raw sensor values. Reads all four channels and calculates CIE x and y chromaticity coordinates @@ -773,8 +775,8 @@ def get_cie(self): :return: Tuple of CIE x, CIE y, and lux values :rtype: Tuple[float, float, float] """ - # Read all four channels using get_channels_raw - ch0, ch1, ch2, ch3 = self.get_channels_raw() + # Read all four channels + ch0, ch1, ch2, ch3 = self.all_channels # Matrix multiplication coefficients (from datasheet) m0x = 2.34892992e-04 diff --git a/examples/opt4048_fulltest.py b/examples/opt4048_fulltest.py index 172c117..66bea6c 100644 --- a/examples/opt4048_fulltest.py +++ b/examples/opt4048_fulltest.py @@ -120,7 +120,7 @@ while True: try: # Read all four channels from the sensor (raw ADC values) - x, y, z, w = sensor.get_channels_raw() + x, y, z, w = sensor.all_channels print("Channel readings (raw values):") print(f"X (CH0): {x}") @@ -129,7 +129,7 @@ print(f"W (CH3): {w}") # Calculate and display CIE chromaticity coordinates and lux - CIEx, CIEy, lux = sensor.get_cie() + CIEx, CIEy, lux = sensor.cie print("\nCIE Coordinates:") print(f"CIE x: {CIEx}") print(f"CIE y: {CIEy}") diff --git a/examples/opt4048_interruptpin.py b/examples/opt4048_interruptpin.py index a2eee39..2319c50 100644 --- a/examples/opt4048_interruptpin.py +++ b/examples/opt4048_interruptpin.py @@ -36,7 +36,7 @@ try: if pin_counter.count > 0: pin_counter.reset() - x, y, lux = sensor.get_cie() + x, y, lux = sensor.cie print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ") print(f"K: {sensor.calculate_color_temperature(x, y)}", end=" ") print(f"Read Delay: {time.monotonic() - last_read_time} sec") diff --git a/examples/opt4048_oneshot.py b/examples/opt4048_oneshot.py index 926759f..da310d6 100644 --- a/examples/opt4048_oneshot.py +++ b/examples/opt4048_oneshot.py @@ -33,7 +33,7 @@ if sensor.mode == Mode.POWERDOWN: # ok we finished the reading! try: - CIEx, CIEy, lux = sensor.get_cie() + CIEx, CIEy, lux = sensor.cie except RuntimeError: print("Error reading sensor data") diff --git a/examples/opt4048_simpletest.py b/examples/opt4048_simpletest.py index df0b53e..c9b345a 100644 --- a/examples/opt4048_simpletest.py +++ b/examples/opt4048_simpletest.py @@ -22,7 +22,7 @@ sensor.conversion_time = ConversionTime.TIME_100MS sensor.mode = Mode.CONTINUOUS while True: - x, y, lux = sensor.get_cie() + 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) diff --git a/examples/opt4048_webserial.py b/examples/opt4048_webserial.py index 0f695d8..841e0b7 100644 --- a/examples/opt4048_webserial.py +++ b/examples/opt4048_webserial.py @@ -32,7 +32,7 @@ while True: if time.monotonic() > last_read_time + READ_INTERVAL: last_read_time = time.monotonic() - x, y, lux = sensor.get_cie() + x, y, lux = sensor.cie print("---CIE Data---") print(f"CIE x: {x}") print(f"CIE y: {y}")