3333_ADS1X15_DEFAULT_ADDRESS = const (0x48 )
3434_ADS1X15_POINTER_CONVERSION = const (0x00 )
3535_ADS1X15_POINTER_CONFIG = const (0x01 )
36+ _ADS1X15_POINTER_LO_THRES = const (0x02 )
37+ _ADS1X15_POINTER_HI_THRES = const (0x03 )
38+
3639_ADS1X15_CONFIG_OS_SINGLE = const (0x8000 )
3740_ADS1X15_CONFIG_MUX_OFFSET = const (12 )
38- _ADS1X15_CONFIG_COMP_QUE_DISABLE = const (0x0003 )
41+ _ADS1X15_CONFIG_COMP_QUEUE = {
42+ 0 : 0x0003 ,
43+ 1 : 0x0000 ,
44+ 2 : 0x0001 ,
45+ 4 : 0x0002 ,
46+ }
3947_ADS1X15_CONFIG_GAIN = {
4048 2 / 3 : 0x0000 ,
4149 1 : 0x0200 ,
@@ -66,15 +74,28 @@ class ADS1x15:
6674 :param int data_rate: The data rate for ADC conversion in samples per second.
6775 Default value depends on the device.
6876 :param Mode mode: The conversion mode, defaults to `Mode.SINGLE`.
77+ :param int comparator_queue_length: The number of successive conversions exceeding
78+ the comparator threshold before asserting ALERT/RDY pin.
79+ Defaults to 0 (comparator function disabled).
80+ :param int comparator_low_threshold: Voltage limit under which comparator de-asserts
81+ ALERT/RDY pin. Must be lower than high threshold to use comparator
82+ function. See subclass for value range and default.
83+ :param int comparator_high_threshold: Voltage limit over which comparator asserts
84+ ALERT/RDY pin. Must be higher than low threshold to use comparator
85+ function. See subclass for value range and default.
6986 :param int address: The I2C address of the device.
7087 """
7188
89+ # pylint: disable=too-many-instance-attributes
7290 def __init__ (
7391 self ,
7492 i2c : I2C ,
7593 gain : float = 1 ,
7694 data_rate : Optional [int ] = None ,
7795 mode : int = Mode .SINGLE ,
96+ comparator_queue_length : int = 0 ,
97+ comparator_low_threshold : int = - 32768 ,
98+ comparator_high_threshold : int = 32767 ,
7899 address : int = _ADS1X15_DEFAULT_ADDRESS ,
79100 ):
80101 # pylint: disable=too-many-arguments
@@ -83,7 +104,10 @@ def __init__(
83104 self .gain = gain
84105 self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
85106 self .mode = mode
107+ self .comparator_queue_length = comparator_queue_length
86108 self .i2c_device = I2CDevice (i2c , address )
109+ self .comparator_low_threshold = comparator_low_threshold
110+ self .comparator_high_threshold = comparator_high_threshold
87111
88112 @property
89113 def bits (self ) -> int :
@@ -131,6 +155,67 @@ def gains(self) -> List[float]:
131155 g .sort ()
132156 return g
133157
158+ @property
159+ def comparator_queue_length (self ) -> int :
160+ """The ADC comparator queue length."""
161+ return self ._comparator_queue_length
162+
163+ @comparator_queue_length .setter
164+ def comparator_queue_length (self , comparator_queue_length : int ) -> None :
165+ possible_comp_queue_lengths = self .comparator_queue_lengths
166+ if comparator_queue_length not in possible_comp_queue_lengths :
167+ raise ValueError (
168+ "Comparator Queue must be one of: {}" .format (
169+ possible_comp_queue_lengths
170+ )
171+ )
172+ self ._comparator_queue_length = comparator_queue_length
173+
174+ @property
175+ def comparator_queue_lengths (self ) -> List [int ]:
176+ """Possible comparator queue length settings."""
177+ g = list (_ADS1X15_CONFIG_COMP_QUEUE .keys ())
178+ g .sort ()
179+ return g
180+
181+ @property
182+ def comparator_low_threshold (self ) -> int :
183+ """The ADC Comparator Lower Limit Threshold."""
184+ return self ._comparator_low_threshold
185+
186+ @property
187+ def comparator_high_threshold (self ) -> int :
188+ """The ADC Comparator Higher Limit Threshold."""
189+ return self ._comparator_high_threshold
190+
191+ @comparator_low_threshold .setter
192+ def comparator_low_threshold (self , value : int ) -> None :
193+ """Set comparator low threshold value for ADS1x15 ADC
194+
195+ :param int value: 16-bit signed integer to write to register
196+ """
197+ if value < - 32768 or value > 32767 :
198+ raise ValueError (
199+ "Comparator Threshold value must be between -32768 and 32767"
200+ )
201+
202+ self ._comparator_low_threshold = value
203+ self ._write_register (_ADS1X15_POINTER_LO_THRES , self .comparator_low_threshold )
204+
205+ @comparator_high_threshold .setter
206+ def comparator_high_threshold (self , value : int ) -> None :
207+ """Set comparator high threshold value for ADS1x15 ADC
208+
209+ :param int value: 16-bit signed integer to write to register
210+ """
211+ if value < - 32768 or value > 32767 :
212+ raise ValueError (
213+ "Comparator Threshold value must be between -32768 and 32767"
214+ )
215+
216+ self ._comparator_high_threshold = value
217+ self ._write_register (_ADS1X15_POINTER_HI_THRES , self .comparator_high_threshold )
218+
134219 @property
135220 def mode (self ) -> int :
136221 """The ADC conversion mode."""
@@ -183,7 +268,7 @@ def _read(self, pin: Pin) -> int:
183268 config |= _ADS1X15_CONFIG_GAIN [self .gain ]
184269 config |= self .mode
185270 config |= self .rate_config [self .data_rate ]
186- config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
271+ config |= _ADS1X15_CONFIG_COMP_QUEUE [ self . comparator_queue_length ]
187272 self ._write_register (_ADS1X15_POINTER_CONFIG , config )
188273
189274 # Wait for conversion to complete
0 commit comments