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 ,
@@ -75,6 +83,7 @@ def __init__(
75
83
gain : float = 1 ,
76
84
data_rate : Optional [int ] = None ,
77
85
mode : int = Mode .SINGLE ,
86
+ compqueue : int = 0 ,
78
87
address : int = _ADS1X15_DEFAULT_ADDRESS ,
79
88
):
80
89
# pylint: disable=too-many-arguments
@@ -83,6 +92,7 @@ def __init__(
83
92
self .gain = gain
84
93
self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
85
94
self .mode = mode
95
+ self .compqueue = compqueue
86
96
self .i2c_device = I2CDevice (i2c , address )
87
97
88
98
@property
@@ -131,6 +141,25 @@ def gains(self) -> List[float]:
131
141
g .sort ()
132
142
return g
133
143
144
+ @property
145
+ def compqueue (self ) -> int :
146
+ """The ADC Comparator Queue."""
147
+ return self ._compqueue
148
+
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
155
+
156
+ @property
157
+ def compqueues (self ) -> List [int ]:
158
+ """Possible gain settings."""
159
+ g = list (_ADS1X15_CONFIG_COMP_QUEUE .keys ())
160
+ g .sort ()
161
+ return g
162
+
134
163
@property
135
164
def mode (self ) -> int :
136
165
"""The ADC conversion mode."""
@@ -183,7 +212,7 @@ def _read(self, pin: Pin) -> int:
183
212
config |= _ADS1X15_CONFIG_GAIN [self .gain ]
184
213
config |= self .mode
185
214
config |= self .rate_config [self .data_rate ]
186
- config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
215
+ config |= _ADS1X15_CONFIG_COMP_QUEUE [ self . compqueue ]
187
216
self ._write_register (_ADS1X15_POINTER_CONFIG , config )
188
217
189
218
# Wait for conversion to complete
@@ -222,6 +251,22 @@ def _write_register(self, reg: int, value: int):
222
251
with self .i2c_device as i2c :
223
252
i2c .write (self .buf )
224
253
254
+ def write_comparator_low_threshold (self , value : int ):
255
+ """Write 16 bit value to register."""
256
+ self .buf [0 ] = _ADS1X15_POINTER_LO_THRES
257
+ self .buf [1 ] = (value >> 8 ) & 0xFF
258
+ self .buf [2 ] = value & 0xFF
259
+ with self .i2c_device as i2c :
260
+ i2c .write (self .buf )
261
+
262
+ def write_comparator_high_threshold (self , value : int ):
263
+ """Write 16 bit value to register."""
264
+ self .buf [0 ] = _ADS1X15_POINTER_HI_THRES
265
+ self .buf [1 ] = (value >> 8 ) & 0xFF
266
+ self .buf [2 ] = value & 0xFF
267
+ with self .i2c_device as i2c :
268
+ i2c .write (self .buf )
269
+
225
270
def _read_register (self , reg : int , fast : bool = False ) -> int :
226
271
"""Read 16 bit register value. If fast is True, the pointer register
227
272
is not updated.
0 commit comments