@@ -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
0 commit comments