@@ -66,6 +66,42 @@ class Mode:
66
66
"""Single-Shot Mode"""
67
67
68
68
69
+ class Comp_Mode :
70
+ """An enum-like class representing possible ADC Comparator operating modes."""
71
+
72
+ # See datasheet "Operating Modes" section
73
+ # values here are masks for setting COMP_MODE bit in Config Register
74
+ # pylint: disable=too-few-public-methods
75
+ TRADITIONAL = 0x0000
76
+ """Traditional Compartor Mode activates above high threshold, de-activates below low"""
77
+ WINDOW = 0x0010
78
+ """Window Comparator Mode activates when reading is outside of high and low thresholds"""
79
+
80
+
81
+ class Comp_Polarity :
82
+ """An enum-like class representing possible ADC Comparator polarity modes."""
83
+
84
+ # See datasheet "Operating Modes" section
85
+ # values here are masks for setting COMP_POL bit in Config Register
86
+ # pylint: disable=too-few-public-methods
87
+ ACTIVE_LOW = 0x0000
88
+ """ALERT_RDY pin is LOW when comparator is active"""
89
+ ACTIVE_HIGH = 0x0008
90
+ """ALERT_RDY pin is HIGH when comparator is active"""
91
+
92
+
93
+ class Comp_Latch :
94
+ """An enum-like class representing possible ADC Comparator latching modes."""
95
+
96
+ # See datasheet "Operating Modes" section
97
+ # values here are masks for setting COMP_LAT bit in Config Register
98
+ # pylint: disable=too-few-public-methods
99
+ NONLATCHING = 0x0000
100
+ """ALERT_RDY pin does not latch when asserted"""
101
+ LATCHING = 0x0004
102
+ """ALERT_RDY pin remains asserted until data is read by controller"""
103
+
104
+
69
105
class ADS1x15 :
70
106
"""Base functionality for ADS1x15 analog to digital converters.
71
107
@@ -79,10 +115,17 @@ class ADS1x15:
79
115
Defaults to 0 (comparator function disabled).
80
116
:param int comparator_low_threshold: Voltage limit under which comparator de-asserts
81
117
ALERT/RDY pin. Must be lower than high threshold to use comparator
82
- function. See subclass for value range and default.
118
+ function. Range of -32768 to 32767, default -32768
83
119
:param int comparator_high_threshold: Voltage limit over which comparator asserts
84
120
ALERT/RDY pin. Must be higher than low threshold to use comparator
85
- function. See subclass for value range and default.
121
+ function. Range of -32768 to 32767, default 32767
122
+ :param Comp_Mode comparator_mode: Configures the comparator as either traditional or window.
123
+ Defaults to 'Comp_Mode.TRADITIONAL'
124
+ :param Comp_Polarity comparator_polarity: Configures the comparator output as either active
125
+ low or active high. Defaults to 'Comp_Polarity.ACTIVE_LOW'
126
+ :param Comp_Latch comparator_latch: Configures the comparator output to only stay asserted while
127
+ readings exceed threshold or latch on assertion until data is read.
128
+ Defaults to 'Comp_Latch.NONLATCHING'
86
129
:param int address: The I2C address of the device.
87
130
"""
88
131
@@ -96,6 +139,9 @@ def __init__(
96
139
comparator_queue_length : int = 0 ,
97
140
comparator_low_threshold : int = - 32768 ,
98
141
comparator_high_threshold : int = 32767 ,
142
+ comparator_mode : int = Comp_Mode .TRADITIONAL ,
143
+ comparator_polarity : int = Comp_Polarity .ACTIVE_LOW ,
144
+ comparator_latch : int = Comp_Latch .NONLATCHING ,
99
145
address : int = _ADS1X15_DEFAULT_ADDRESS ,
100
146
):
101
147
# pylint: disable=too-many-arguments
@@ -108,6 +154,9 @@ def __init__(
108
154
self .i2c_device = I2CDevice (i2c , address )
109
155
self .comparator_low_threshold = comparator_low_threshold
110
156
self .comparator_high_threshold = comparator_high_threshold
157
+ self .comparator_mode = comparator_mode
158
+ self .comparator_polarity = comparator_polarity
159
+ self .comparator_latch = comparator_latch
111
160
112
161
@property
113
162
def bits (self ) -> int :
@@ -227,6 +276,39 @@ def mode(self, mode: int) -> None:
227
276
raise ValueError ("Unsupported mode." )
228
277
self ._mode = mode
229
278
279
+ @property
280
+ def comparator_mode (self ) -> int :
281
+ """The ADC comparator mode."""
282
+ return self ._comparator_mode
283
+
284
+ @comparator_mode .setter
285
+ def comparator_mode (self , comp_mode : int ) -> None :
286
+ if comp_mode not in (Comp_Mode .TRADITIONAL , Comp_Mode .WINDOW ):
287
+ raise ValueError ("Unsupported mode." )
288
+ self ._comparator_mode = comp_mode
289
+
290
+ @property
291
+ def comparator_polarity (self ) -> int :
292
+ """The ADC comparator polarity mode."""
293
+ return self ._comparator_polarity
294
+
295
+ @comparator_polarity .setter
296
+ def comparator_polarity (self , comp_pol : int ) -> None :
297
+ if comp_pol not in (Comp_Polarity .ACTIVE_LOW , Comp_Polarity .ACTIVE_HIGH ):
298
+ raise ValueError ("Unsupported mode." )
299
+ self ._comparator_polarity = comp_pol
300
+
301
+ @property
302
+ def comparator_latch (self ) -> int :
303
+ """The ADC comparator latching mode."""
304
+ return self ._comparator_latch
305
+
306
+ @comparator_latch .setter
307
+ def comparator_latch (self , comp_latch : int ) -> None :
308
+ if comp_latch not in (Comp_Latch .NONLATCHING , Comp_Latch .LATCHING ):
309
+ raise ValueError ("Unsupported mode." )
310
+ self ._comparator_latch = comp_latch
311
+
230
312
def read (self , pin : Pin , is_differential : bool = False ) -> int :
231
313
"""I2C Interface for ADS1x15-based ADCs reads.
232
314
@@ -268,6 +350,9 @@ def _read(self, pin: Pin) -> int:
268
350
config |= _ADS1X15_CONFIG_GAIN [self .gain ]
269
351
config |= self .mode
270
352
config |= self .rate_config [self .data_rate ]
353
+ config |= self .comparator_mode
354
+ config |= self .comparator_polarity
355
+ config |= self .comparator_latch
271
356
config |= _ADS1X15_CONFIG_COMP_QUEUE [self .comparator_queue_length ]
272
357
self ._write_register (_ADS1X15_POINTER_CONFIG , config )
273
358
@@ -317,3 +402,35 @@ def _read_register(self, reg: int, fast: bool = False) -> int:
317
402
else :
318
403
i2c .write_then_readinto (bytearray ([reg ]), self .buf , in_end = 2 )
319
404
return self .buf [0 ] << 8 | self .buf [1 ]
405
+
406
+ def read_config (self ) -> None :
407
+ """Reads Config Register and sets all properties accordingly"""
408
+ config_value = self ._read_register (_ADS1X15_POINTER_CONFIG )
409
+
410
+ self .gain = next (
411
+ key
412
+ for key , value in _ADS1X15_CONFIG_GAIN .items ()
413
+ if value == (config_value & 0x0E00 )
414
+ )
415
+ self .data_rate = next (
416
+ key
417
+ for key , value in self .rate_config .items ()
418
+ if value == (config_value & 0x00E0 )
419
+ )
420
+ self .comparator_queue_length = next (
421
+ key
422
+ for key , value in _ADS1X15_CONFIG_COMP_QUEUE .items ()
423
+ if value == (config_value & 0x0003 )
424
+ )
425
+ self .mode = Mode .SINGLE if config_value & 0x0100 else Mode .CONTINUOUS
426
+ self .comparator_mode = (
427
+ Comp_Mode .WINDOW if config_value & 0x0010 else Comp_Mode .TRADITIONAL
428
+ )
429
+ self .comparator_polarity = (
430
+ Comp_Polarity .ACTIVE_HIGH
431
+ if config_value & 0x0008
432
+ else Comp_Polarity .ACTIVE_LOW
433
+ )
434
+ self .comparator_latch = (
435
+ Comp_Latch .LATCHING if config_value & 0x0004 else Comp_Latch .NONLATCHING
436
+ )
0 commit comments