@@ -74,6 +74,9 @@ class ADS1x15:
7474 :param int data_rate: The data rate for ADC conversion in samples per second.
7575 Default value depends on the device.
7676 :param Mode mode: The conversion mode, defaults to `Mode.SINGLE`.
77+ :param int comparator_queue_length: The number of successive conversions exceeding the comparator threshold before asserting ALERT/RDY pin, defaults to 0 (comparator function disabled).
78+ :param int comparator_low_thres: Voltage limit under which comparator de-asserts ALERT/RDY pin. Must be lower than high threshold to use comparator function. Defaults to 0x800.
79+ :param int comparator_high_thres: Voltage limit over which comparator asserts ALERT/RDY pin. Must be higher than low threshold to use comparator function. Defaults to 0x7FF.
7780 :param int address: The I2C address of the device.
7881 """
7982
@@ -83,7 +86,9 @@ def __init__(
8386 gain : float = 1 ,
8487 data_rate : Optional [int ] = None ,
8588 mode : int = Mode .SINGLE ,
86- compqueue : int = 0 ,
89+ comparator_queue_length : int = 0 ,
90+ comparator_low_thres : int = 0x8000 ,
91+ comparator_high_thres : int = 0x7FF0 ,
8792 address : int = _ADS1X15_DEFAULT_ADDRESS ,
8893 ):
8994 # pylint: disable=too-many-arguments
@@ -92,7 +97,9 @@ def __init__(
9297 self .gain = gain
9398 self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
9499 self .mode = mode
95- self .compqueue = compqueue
100+ self .comparator_queue_length = comparator_queue_length
101+ self .comparator_low_thres : comparator_low_thres
102+ self .comparator_high_thres : comparator_high_thres
96103 self .i2c_device = I2CDevice (i2c , address )
97104
98105 @property
@@ -142,24 +149,48 @@ def gains(self) -> List[float]:
142149 return g
143150
144151 @property
145- def compqueue (self ) -> int :
152+ def comparator_queue_length (self ) -> int :
146153 """The ADC Comparator Queue."""
147- return self ._compqueue
154+ return self ._comparator_queue_length
148155
149- @compqueue .setter
150- def compqueue (self , compqueue : int ) -> None :
151- possible_compqueues = self .compqueues
152- if compqueue not in possible_compqueues :
153- raise ValueError ("Comparator Queue must be one of: {}" .format (possible_compqueues ))
154- self ._compqueue = compqueue
156+ @comparator_queue_length .setter
157+ def comparator_queue_length (self , comparator_queue_length : int ) -> None :
158+ possible_comparator_queue_lengths = self .comparator_queue_lengths
159+ if comparator_queue_length not in possible_comparator_queue_lengths :
160+ raise ValueError ("Comparator Queue must be one of: {}" .format (possible_comparator_queue_lengths ))
161+ self ._comparator_queue_length = comparator_queue_length
155162
156163 @property
157- def compqueues (self ) -> List [int ]:
158- """Possible gain settings."""
164+ def comparator_queue_lengths (self ) -> List [int ]:
165+ """Possible comparator queue length settings."""
159166 g = list (_ADS1X15_CONFIG_COMP_QUEUE .keys ())
160167 g .sort ()
161168 return g
162169
170+ @property
171+ def comparator_low_thres (self ) -> int :
172+ """The ADC Comparator Lower Limit Threshold."""
173+ return self ._comparator_low_thres
174+
175+ @comparator_low_thres .setter
176+ def comparator_low_thres (self , comparator_low_thres : int ) -> None :
177+ """Sets 12-bit threshold in 16-bit register in unsigned format."""
178+ if comparator_low_thres < 0 or comparator_low_thres > 65535 :
179+ raise ValueError ("Comparator Low Threshold must be unsigned 16-bit integer between 0 and 65535" )
180+ self ._comparator_low_thres = comparator_low_thres
181+
182+ @property
183+ def comparator_high_thres (self ) -> int :
184+ """The ADC Comparator Higher Limit Threshold."""
185+ return self ._comparator_high_thres
186+
187+ @comparator_high_thres .setter
188+ def comparator_high_thres (self , comparator_high_thres : int ) -> None :
189+ """Sets 12-bit threshold in 16-bit register in unsigned format."""
190+ if comparator_high_thres < 0 or comparator_high_thres > 65535 :
191+ raise ValueError ("Comparator High Threshold must be unsigned 16-bit integer between 0 and 65535" )
192+ self ._comparator_high_thres = comparator_high_thres
193+
163194 @property
164195 def mode (self ) -> int :
165196 """The ADC conversion mode."""
@@ -212,7 +243,7 @@ def _read(self, pin: Pin) -> int:
212243 config |= _ADS1X15_CONFIG_GAIN [self .gain ]
213244 config |= self .mode
214245 config |= self .rate_config [self .data_rate ]
215- config |= _ADS1X15_CONFIG_COMP_QUEUE [self .compqueue ]
246+ config |= _ADS1X15_CONFIG_COMP_QUEUE [self .comparator_queue_length ]
216247 self ._write_register (_ADS1X15_POINTER_CONFIG , config )
217248
218249 # Wait for conversion to complete
@@ -251,19 +282,17 @@ def _write_register(self, reg: int, value: int):
251282 with self .i2c_device as i2c :
252283 i2c .write (self .buf )
253284
254- def write_comparator_low_threshold (self , value : int ):
255- """Write 16 bit value to Comparator Low Threshold register ."""
285+ def write_comparator_thresholds (self ):
286+ """Write 16 bit values to Comparator Low and High Threshold registers ."""
256287 self .buf [0 ] = _ADS1X15_POINTER_LO_THRES
257- self .buf [1 ] = (value >> 8 ) & 0xFF
258- self .buf [2 ] = value & 0xFF
288+ self .buf [1 ] = (self . _comparator_low_thres >> 8 ) & 0xFF
289+ self .buf [2 ] = self . _comparator_low_thres & 0xFF
259290 with self .i2c_device as i2c :
260291 i2c .write (self .buf )
261292
262- def write_comparator_high_threshold (self , value : int ):
263- """Write 16 bit value to Comparator High Threshold register."""
264293 self .buf [0 ] = _ADS1X15_POINTER_HI_THRES
265- self .buf [1 ] = (value >> 8 ) & 0xFF
266- self .buf [2 ] = value & 0xFF
294+ self .buf [1 ] = (self . _comparator_high_thres >> 8 ) & 0xFF
295+ self .buf [2 ] = self . _comparator_high_thres & 0xFF
267296 with self .i2c_device as i2c :
268297 i2c .write (self .buf )
269298
0 commit comments