33
33
_ADS1X15_DEFAULT_ADDRESS = const (0x48 )
34
34
_ADS1X15_POINTER_CONVERSION = const (0x00 )
35
35
_ADS1X15_POINTER_CONFIG = const (0x01 )
36
+ _ADS1X15_POINTER_LO_THRES = const (0x02 )
37
+ _ADS1X15_POINTER_HI_THRES = const (0x03 )
38
+
36
39
_ADS1X15_CONFIG_OS_SINGLE = const (0x8000 )
37
40
_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
+ }
39
47
_ADS1X15_CONFIG_GAIN = {
40
48
2 / 3 : 0x0000 ,
41
49
1 : 0x0200 ,
@@ -66,15 +74,28 @@ class ADS1x15:
66
74
:param int data_rate: The data rate for ADC conversion in samples per second.
67
75
Default value depends on the device.
68
76
: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.
69
86
:param int address: The I2C address of the device.
70
87
"""
71
88
89
+ # pylint: disable=too-many-instance-attributes
72
90
def __init__ (
73
91
self ,
74
92
i2c : I2C ,
75
93
gain : float = 1 ,
76
94
data_rate : Optional [int ] = None ,
77
95
mode : int = Mode .SINGLE ,
96
+ comparator_queue_length : int = 0 ,
97
+ comparator_low_threshold : int = - 32768 ,
98
+ comparator_high_threshold : int = 32767 ,
78
99
address : int = _ADS1X15_DEFAULT_ADDRESS ,
79
100
):
80
101
# pylint: disable=too-many-arguments
@@ -83,7 +104,10 @@ def __init__(
83
104
self .gain = gain
84
105
self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
85
106
self .mode = mode
107
+ self .comparator_queue_length = comparator_queue_length
86
108
self .i2c_device = I2CDevice (i2c , address )
109
+ self .comparator_low_threshold = comparator_low_threshold
110
+ self .comparator_high_threshold = comparator_high_threshold
87
111
88
112
@property
89
113
def bits (self ) -> int :
@@ -131,6 +155,67 @@ def gains(self) -> List[float]:
131
155
g .sort ()
132
156
return g
133
157
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
+
134
219
@property
135
220
def mode (self ) -> int :
136
221
"""The ADC conversion mode."""
@@ -183,7 +268,7 @@ def _read(self, pin: Pin) -> int:
183
268
config |= _ADS1X15_CONFIG_GAIN [self .gain ]
184
269
config |= self .mode
185
270
config |= self .rate_config [self .data_rate ]
186
- config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
271
+ config |= _ADS1X15_CONFIG_COMP_QUEUE [ self . comparator_queue_length ]
187
272
self ._write_register (_ADS1X15_POINTER_CONFIG , config )
188
273
189
274
# Wait for conversion to complete
0 commit comments