Skip to content

Commit 0aa1cd7

Browse files
committed
Moved threshold writes to setters
Writing of the threshold registers moved to the setter functions, "threshold" spelled out in variables, improved readability of some comments.
1 parent 7d98fde commit 0aa1cd7

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

adafruit_ads1x15/ads1x15.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,20 @@ class ADS1x15:
7171
7272
:param ~busio.I2C i2c: The I2C bus the device is connected to.
7373
:param float gain: The ADC gain.
74+
Search logs
75+
7476
:param int data_rate: The data rate for ADC conversion in samples per second.
7577
Default value depends on the device.
7678
: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.
79+
:param int comparator_queue_length: The number of successive conversions exceeding
80+
the comparator threshold before asserting ALERT/RDY pin.
81+
Defaults to 0 (comparator function disabled).
82+
:param int comparator_low_threshold: Voltage limit under which comparator de-asserts
83+
ALERT/RDY pin. Must be lower than high threshold to use comparator
84+
function. Defaults to 0x8000.
85+
:param int comparator_high_threshold: Voltage limit over which comparator asserts
86+
ALERT/RDY pin. Must be higher than low threshold to use comparator
87+
function. Defaults to 0x7FF0.
8088
:param int address: The I2C address of the device.
8189
"""
8290

@@ -87,8 +95,8 @@ def __init__(
8795
data_rate: Optional[int] = None,
8896
mode: int = Mode.SINGLE,
8997
comparator_queue_length: int = 0,
90-
comparator_low_thres: int = 0x8000,
91-
comparator_high_thres: int = 0x7FF0,
98+
comparator_low_threshold: int = 0x8000,
99+
comparator_high_threshold: int = 0x7FF0,
92100
address: int = _ADS1X15_DEFAULT_ADDRESS,
93101
):
94102
# pylint: disable=too-many-arguments
@@ -98,8 +106,8 @@ def __init__(
98106
self.data_rate = self._data_rate_default() if data_rate is None else data_rate
99107
self.mode = mode
100108
self.comparator_queue_length = comparator_queue_length
101-
self.comparator_low_thres: comparator_low_thres
102-
self.comparator_high_thres: comparator_high_thres
109+
self.comparator_low_threshold: comparator_low_threshold
110+
self.comparator_high_threshold: comparator_high_threshold
103111
self.i2c_device = I2CDevice(i2c, address)
104112

105113
@property
@@ -150,7 +158,7 @@ def gains(self) -> List[float]:
150158

151159
@property
152160
def comparator_queue_length(self) -> int:
153-
"""The ADC Comparator Queue."""
161+
"""The ADC comparator queue length."""
154162
return self._comparator_queue_length
155163

156164
@comparator_queue_length.setter
@@ -168,28 +176,42 @@ def comparator_queue_lengths(self) -> List[int]:
168176
return g
169177

170178
@property
171-
def comparator_low_thres(self) -> int:
179+
def comparator_low_threshold(self) -> int:
172180
"""The ADC Comparator Lower Limit Threshold."""
173-
return self._comparator_low_thres
181+
return self._comparator_low_threshold
174182

175-
@comparator_low_thres.setter
176-
def comparator_low_thres(self, comparator_low_thres: int) -> None:
183+
@comparator_low_threshold.setter
184+
def comparator_low_threshold(self, comparator_low_threshold: int) -> None:
177185
"""Sets 12-bit threshold in 16-bit register in unsigned format."""
178-
if comparator_low_thres < 0 or comparator_low_thres > 65535:
186+
if comparator_low_threshold < 0 or comparator_low_threshold > 65535:
179187
raise ValueError("Comparator Low Threshold must be unsigned 16-bit integer between 0 and 65535")
180-
self._comparator_low_thres = comparator_low_thres
188+
self._comparator_low_threshold = comparator_low_threshold
189+
190+
"""Write value to chip"""
191+
self.buf[0] = _ADS1X15_POINTER_LO_THRES
192+
self.buf[1] = (self._comparator_low_threshold >> 8) & 0xFF
193+
self.buf[2] = self._comparator_low_threshold & 0xFF
194+
with self.i2c_device as i2c:
195+
i2c.write(self.buf)
181196

182197
@property
183-
def comparator_high_thres(self) -> int:
198+
def comparator_high_threshold(self) -> int:
184199
"""The ADC Comparator Higher Limit Threshold."""
185-
return self._comparator_high_thres
200+
return self._comparator_high_threshold
186201

187-
@comparator_high_thres.setter
188-
def comparator_high_thres(self, comparator_high_thres: int) -> None:
202+
@comparator_high_threshold.setter
203+
def comparator_high_threshold(self, comparator_high_threshold: int) -> None:
189204
"""Sets 12-bit threshold in 16-bit register in unsigned format."""
190-
if comparator_high_thres < 0 or comparator_high_thres > 65535:
205+
if comparator_high_threshold < 0 or comparator_high_threshold > 65535:
191206
raise ValueError("Comparator High Threshold must be unsigned 16-bit integer between 0 and 65535")
192-
self._comparator_high_thres = comparator_high_thres
207+
self._comparator_high_threshold = comparator_high_threshold
208+
209+
"""Write value to chip"""
210+
self.buf[0] = _ADS1X15_POINTER_HI_THRES
211+
self.buf[1] = (self._comparator_high_threshold >> 8) & 0xFF
212+
self.buf[2] = self._comparator_high_threshold & 0xFF
213+
with self.i2c_device as i2c:
214+
i2c.write(self.buf)
193215

194216
@property
195217
def mode(self) -> int:
@@ -282,20 +304,6 @@ def _write_register(self, reg: int, value: int):
282304
with self.i2c_device as i2c:
283305
i2c.write(self.buf)
284306

285-
def write_comparator_thresholds(self):
286-
"""Write 16 bit values to Comparator Low and High Threshold registers."""
287-
self.buf[0] = _ADS1X15_POINTER_LO_THRES
288-
self.buf[1] = (self._comparator_low_thres >> 8) & 0xFF
289-
self.buf[2] = self._comparator_low_thres & 0xFF
290-
with self.i2c_device as i2c:
291-
i2c.write(self.buf)
292-
293-
self.buf[0] = _ADS1X15_POINTER_HI_THRES
294-
self.buf[1] = (self._comparator_high_thres >> 8) & 0xFF
295-
self.buf[2] = self._comparator_high_thres & 0xFF
296-
with self.i2c_device as i2c:
297-
i2c.write(self.buf)
298-
299307
def _read_register(self, reg: int, fast: bool = False) -> int:
300308
"""Read 16 bit register value. If fast is True, the pointer register
301309
is not updated.

adafruit_ads1x15/analog_in.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def convert_to_value(self, volts: float) -> int:
7474

7575
def convert_to_voltage(self, value: int) -> float:
7676
"""Calculates integer for threshold registers from voltage level input"""
77-
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
77+
volts = value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
7878
if volts > _ADS1X15_PGA_RANGE[self._ads.gain]:
7979
volts = _ADS1X15_PGA_RANGE[self._ads.gain] - volts
8080
return volts

examples/ads1x15_comparator_example.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
ads.comparator_queue_length = 1
3030

3131
# Set comparator low threshold to 2V
32-
ads.comparator_low_thres = chan.convert_to_value(2.000)
32+
ads.comparator_low_threshold = chan.convert_to_value(2.000)
3333
# Set comparator high threshold to 2.002V. High threshold must be above low threshold
34-
ads.comparator_high_thres = chan.convert_to_value(2.002)
35-
# Write comparator values to the chip registers
36-
ads.write_comparator_thresholds()
34+
ads.comparator_high_threshold = chan.convert_to_value(2.002)
3735

3836
count = 0
3937
while True:

0 commit comments

Comments
 (0)