Skip to content

properties instead of functions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
176 changes: 89 additions & 87 deletions adafruit_opt4048.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/opt4048_fulltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion examples/opt4048_interruptpin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion examples/opt4048_oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion examples/opt4048_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/opt4048_webserial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down