Skip to content

Commit 7d98fde

Browse files
committed
Addressed PR comments
Added threshold properties, changed to "comparator_queue_length" property name, added "convert to voltage" and "convert to value" functions
1 parent c84a39d commit 7d98fde

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

adafruit_ads1x15/ads1x15.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

adafruit_ads1x15/analog_in.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,19 @@ def value(self) -> int:
6262
@property
6363
def voltage(self) -> float:
6464
"""Returns the voltage from the ADC pin as a floating point value."""
65-
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
65+
volts = self.convert_to_voltage(self.value)
6666
return volts
6767

68-
def ADC_value(self, volts: float) -> int:
68+
def convert_to_value(self, volts: float) -> int:
6969
"""Calculates integer for threshold registers from voltage level input"""
70-
value = int((volts * 32767) / _ADS1X15_PGA_RANGE[self._ads.gain])
70+
value = round((volts * 32767) / _ADS1X15_PGA_RANGE[self._ads.gain])
71+
if value < 0:
72+
value = 65536 + value
7173
return value
74+
75+
def convert_to_voltage(self, value: int) -> float:
76+
"""Calculates integer for threshold registers from voltage level input"""
77+
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
78+
if volts > _ADS1X15_PGA_RANGE[self._ads.gain]:
79+
volts = _ADS1X15_PGA_RANGE[self._ads.gain] - volts
80+
return volts

examples/ads1x15_comparator_example.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
int_pin = countio.Counter(board.GP9, edge=countio.Edge.RISE)
2727

2828
# Set comparator to assert after 1 ADC conversion
29-
ads.compqueue = 1
29+
ads.comparator_queue_length = 1
30+
3031
# Set comparator low threshold to 2V
31-
ads.write_comparator_low_threshold(chan.ADC_value(2))
32+
ads.comparator_low_thres = chan.convert_to_value(2.000)
3233
# Set comparator high threshold to 2.002V. High threshold must be above low threshold
33-
ads.write_comparator_high_threshold(chan.ADC_value(2.002))
34+
ads.comparator_high_thres = chan.convert_to_value(2.002)
35+
# Write comparator values to the chip registers
36+
ads.write_comparator_thresholds()
3437

3538
count = 0
3639
while True:

0 commit comments

Comments
 (0)