@@ -74,6 +74,9 @@ class ADS1x15:
74
74
:param int data_rate: The data rate for ADC conversion in samples per second.
75
75
Default value depends on the device.
76
76
: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.
77
80
:param int address: The I2C address of the device.
78
81
"""
79
82
@@ -83,7 +86,9 @@ def __init__(
83
86
gain : float = 1 ,
84
87
data_rate : Optional [int ] = None ,
85
88
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 ,
87
92
address : int = _ADS1X15_DEFAULT_ADDRESS ,
88
93
):
89
94
# pylint: disable=too-many-arguments
@@ -92,7 +97,9 @@ def __init__(
92
97
self .gain = gain
93
98
self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
94
99
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
96
103
self .i2c_device = I2CDevice (i2c , address )
97
104
98
105
@property
@@ -142,24 +149,48 @@ def gains(self) -> List[float]:
142
149
return g
143
150
144
151
@property
145
- def compqueue (self ) -> int :
152
+ def comparator_queue_length (self ) -> int :
146
153
"""The ADC Comparator Queue."""
147
- return self ._compqueue
154
+ return self ._comparator_queue_length
148
155
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
155
162
156
163
@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."""
159
166
g = list (_ADS1X15_CONFIG_COMP_QUEUE .keys ())
160
167
g .sort ()
161
168
return g
162
169
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
+
163
194
@property
164
195
def mode (self ) -> int :
165
196
"""The ADC conversion mode."""
@@ -212,7 +243,7 @@ def _read(self, pin: Pin) -> int:
212
243
config |= _ADS1X15_CONFIG_GAIN [self .gain ]
213
244
config |= self .mode
214
245
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 ]
216
247
self ._write_register (_ADS1X15_POINTER_CONFIG , config )
217
248
218
249
# Wait for conversion to complete
@@ -251,19 +282,17 @@ def _write_register(self, reg: int, value: int):
251
282
with self .i2c_device as i2c :
252
283
i2c .write (self .buf )
253
284
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 ."""
256
287
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
259
290
with self .i2c_device as i2c :
260
291
i2c .write (self .buf )
261
292
262
- def write_comparator_high_threshold (self , value : int ):
263
- """Write 16 bit value to Comparator High Threshold register."""
264
293
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
267
296
with self .i2c_device as i2c :
268
297
i2c .write (self .buf )
269
298
0 commit comments