Skip to content

Commit 40bd717

Browse files
authored
Merge pull request #1 from FoamyGuy/use_properties
properties instead of functions
2 parents 93230fa + 18429ea commit 40bd717

7 files changed

+96
-94
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Usage Example
109109
sensor.conversion_time = ConversionTime.TIME_100MS
110110
sensor.mode = Mode.CONTINUOUS
111111
while True:
112-
x, y, lux = sensor.get_cie()
112+
x, y, lux = sensor.cie
113113
print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ")
114114
print(f"K: {sensor.calculate_color_temperature(x,y)}")
115115
time.sleep(1)

adafruit_opt4048.py

Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,92 @@ def threshold_channel(self, value):
582582
raise ValueError("Threshold channel must be an integer between 0 and 3")
583583
self._threshold_channel = value
584584

585-
def get_channels_raw(self):
585+
@property
586+
def threshold_low(self):
587+
"""Get the current low threshold value.
588+
589+
Returns the current low threshold value as a 32-bit integer.
590+
This value determines when a low threshold interrupt is generated
591+
when interrupt_direction is False.
592+
"""
593+
# Read the exponent and mantissa from the threshold low register
594+
exponent = self._threshold_low_exponent
595+
mantissa = self._threshold_low_mantissa
596+
print(f"exponent: {exponent} mantissa: {mantissa}")
597+
# Calculate ADC code value by applying the exponent as a bit shift
598+
# ADD 8 to the exponent as per datasheet equations 12-13
599+
return mantissa << (8 + exponent)
600+
601+
@threshold_low.setter
602+
def threshold_low(self, value):
603+
"""Set the low threshold value for interrupt generation.
604+
605+
:param int value: The low threshold value as a 32-bit integer
606+
"""
607+
# Find the appropriate exponent and mantissa values that represent the threshold
608+
exponent = 0
609+
mantissa = value
610+
611+
# The mantissa needs to fit in 12 bits, so we start by shifting right
612+
# to determine how many shifts we need (which gives us the exponent)
613+
# Note that the threshold registers already have 8 added to exponent
614+
# internally so we first subtract 8 from our target exponent
615+
if mantissa > 0xFFF: # If value won't fit in 12 bits
616+
while mantissa > 0xFFF and exponent < 15:
617+
mantissa >>= 1
618+
exponent += 1
619+
if mantissa > 0xFFF: # If still won't fit with max exponent, clamp
620+
mantissa = 0xFFF
621+
exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally
622+
623+
# Write the exponent and mantissa to the register
624+
self._threshold_low_exponent = exponent
625+
self._threshold_low_mantissa = mantissa
626+
627+
@property
628+
def threshold_high(self):
629+
"""Get the current high threshold value.
630+
631+
Returns the current high threshold value as a 32-bit integer.
632+
This value determines when a high threshold interrupt is generated
633+
when interrupt_direction is True.
634+
"""
635+
# Read the exponent and mantissa from the threshold high register
636+
exponent = self._threshold_high_exponent
637+
mantissa = self._threshold_high_mantissa
638+
639+
# Calculate ADC code value by applying the exponent as a bit shift
640+
# ADD 8 to the exponent as per datasheet equations 10-11
641+
return mantissa << (8 + exponent)
642+
643+
@threshold_high.setter
644+
def threshold_high(self, value):
645+
"""Set the high threshold value for interrupt generation.
646+
647+
:param int value: The high threshold value as a 32-bit integer
648+
"""
649+
# Find the appropriate exponent and mantissa values that represent the threshold
650+
exponent = 0
651+
mantissa = value
652+
653+
# The mantissa needs to fit in 12 bits, so we start by shifting right
654+
# to determine how many shifts we need (which gives us the exponent)
655+
# Note that the threshold registers already have 8 added to exponent
656+
# internally so we first subtract 8 from our target exponent
657+
if mantissa > 0xFFF: # If value won't fit in 12 bits
658+
while mantissa > 0xFFF and exponent < 15:
659+
mantissa >>= 1
660+
exponent += 1
661+
if mantissa > 0xFFF: # If still won't fit with max exponent, clamp
662+
mantissa = 0xFFF
663+
exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally
664+
665+
# Write the exponent and mantissa to the register
666+
self._threshold_high_exponent = exponent
667+
self._threshold_high_mantissa = mantissa
668+
669+
@property
670+
def all_channels(self):
586671
"""Read all four channels, verify CRC, and return raw ADC code values.
587672
588673
Reads registers for channels 0-3 in one burst, checks the CRC bits for each,
@@ -681,90 +766,7 @@ def get_channels_raw(self):
681766
return tuple(channels)
682767

683768
@property
684-
def threshold_low(self):
685-
"""Get the current low threshold value.
686-
687-
Returns the current low threshold value as a 32-bit integer.
688-
This value determines when a low threshold interrupt is generated
689-
when interrupt_direction is False.
690-
"""
691-
# Read the exponent and mantissa from the threshold low register
692-
exponent = self._threshold_low_exponent
693-
mantissa = self._threshold_low_mantissa
694-
print(f"exponent: {exponent} mantissa: {mantissa}")
695-
# Calculate ADC code value by applying the exponent as a bit shift
696-
# ADD 8 to the exponent as per datasheet equations 12-13
697-
return mantissa << (8 + exponent)
698-
699-
@threshold_low.setter
700-
def threshold_low(self, value):
701-
"""Set the low threshold value for interrupt generation.
702-
703-
:param int value: The low threshold value as a 32-bit integer
704-
"""
705-
# Find the appropriate exponent and mantissa values that represent the threshold
706-
exponent = 0
707-
mantissa = value
708-
709-
# The mantissa needs to fit in 12 bits, so we start by shifting right
710-
# to determine how many shifts we need (which gives us the exponent)
711-
# Note that the threshold registers already have 8 added to exponent
712-
# internally so we first subtract 8 from our target exponent
713-
if mantissa > 0xFFF: # If value won't fit in 12 bits
714-
while mantissa > 0xFFF and exponent < 15:
715-
mantissa >>= 1
716-
exponent += 1
717-
if mantissa > 0xFFF: # If still won't fit with max exponent, clamp
718-
mantissa = 0xFFF
719-
exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally
720-
721-
# Write the exponent and mantissa to the register
722-
self._threshold_low_exponent = exponent
723-
self._threshold_low_mantissa = mantissa
724-
725-
@property
726-
def threshold_high(self):
727-
"""Get the current high threshold value.
728-
729-
Returns the current high threshold value as a 32-bit integer.
730-
This value determines when a high threshold interrupt is generated
731-
when interrupt_direction is True.
732-
"""
733-
# Read the exponent and mantissa from the threshold high register
734-
exponent = self._threshold_high_exponent
735-
mantissa = self._threshold_high_mantissa
736-
737-
# Calculate ADC code value by applying the exponent as a bit shift
738-
# ADD 8 to the exponent as per datasheet equations 10-11
739-
return mantissa << (8 + exponent)
740-
741-
@threshold_high.setter
742-
def threshold_high(self, value):
743-
"""Set the high threshold value for interrupt generation.
744-
745-
:param int value: The high threshold value as a 32-bit integer
746-
"""
747-
# Find the appropriate exponent and mantissa values that represent the threshold
748-
exponent = 0
749-
mantissa = value
750-
751-
# The mantissa needs to fit in 12 bits, so we start by shifting right
752-
# to determine how many shifts we need (which gives us the exponent)
753-
# Note that the threshold registers already have 8 added to exponent
754-
# internally so we first subtract 8 from our target exponent
755-
if mantissa > 0xFFF: # If value won't fit in 12 bits
756-
while mantissa > 0xFFF and exponent < 15:
757-
mantissa >>= 1
758-
exponent += 1
759-
if mantissa > 0xFFF: # If still won't fit with max exponent, clamp
760-
mantissa = 0xFFF
761-
exponent = 15 - 8 # Max exponent (15) minus the 8 that's added internally
762-
763-
# Write the exponent and mantissa to the register
764-
self._threshold_high_exponent = exponent
765-
self._threshold_high_mantissa = mantissa
766-
767-
def get_cie(self):
769+
def cie(self):
768770
"""Calculate CIE chromaticity coordinates and lux from raw sensor values.
769771
770772
Reads all four channels and calculates CIE x and y chromaticity coordinates
@@ -773,8 +775,8 @@ def get_cie(self):
773775
:return: Tuple of CIE x, CIE y, and lux values
774776
:rtype: Tuple[float, float, float]
775777
"""
776-
# Read all four channels using get_channels_raw
777-
ch0, ch1, ch2, ch3 = self.get_channels_raw()
778+
# Read all four channels
779+
ch0, ch1, ch2, ch3 = self.all_channels
778780

779781
# Matrix multiplication coefficients (from datasheet)
780782
m0x = 2.34892992e-04

examples/opt4048_fulltest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
while True:
121121
try:
122122
# Read all four channels from the sensor (raw ADC values)
123-
x, y, z, w = sensor.get_channels_raw()
123+
x, y, z, w = sensor.all_channels
124124

125125
print("Channel readings (raw values):")
126126
print(f"X (CH0): {x}")
@@ -129,7 +129,7 @@
129129
print(f"W (CH3): {w}")
130130

131131
# Calculate and display CIE chromaticity coordinates and lux
132-
CIEx, CIEy, lux = sensor.get_cie()
132+
CIEx, CIEy, lux = sensor.cie
133133
print("\nCIE Coordinates:")
134134
print(f"CIE x: {CIEx}")
135135
print(f"CIE y: {CIEy}")

examples/opt4048_interruptpin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
try:
3737
if pin_counter.count > 0:
3838
pin_counter.reset()
39-
x, y, lux = sensor.get_cie()
39+
x, y, lux = sensor.cie
4040
print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ")
4141
print(f"K: {sensor.calculate_color_temperature(x, y)}", end=" ")
4242
print(f"Read Delay: {time.monotonic() - last_read_time} sec")

examples/opt4048_oneshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
if sensor.mode == Mode.POWERDOWN:
3434
# ok we finished the reading!
3535
try:
36-
CIEx, CIEy, lux = sensor.get_cie()
36+
CIEx, CIEy, lux = sensor.cie
3737
except RuntimeError:
3838
print("Error reading sensor data")
3939

examples/opt4048_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
sensor.conversion_time = ConversionTime.TIME_100MS
2323
sensor.mode = Mode.CONTINUOUS
2424
while True:
25-
x, y, lux = sensor.get_cie()
25+
x, y, lux = sensor.cie
2626
print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ")
2727
print(f"K: {sensor.calculate_color_temperature(x,y)}")
2828
time.sleep(1)

examples/opt4048_webserial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
while True:
3333
if time.monotonic() > last_read_time + READ_INTERVAL:
3434
last_read_time = time.monotonic()
35-
x, y, lux = sensor.get_cie()
35+
x, y, lux = sensor.cie
3636
print("---CIE Data---")
3737
print(f"CIE x: {x}")
3838
print(f"CIE y: {y}")

0 commit comments

Comments
 (0)