Skip to content

Commit 4ba6144

Browse files
committed
Moved 2's complement calculation to threshold setters
Threshold properties changed to 12-bit signed integers and conversion to unsigned 16-bit numbers moved to threshold setter functions.
1 parent f74e4f9 commit 4ba6144

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

adafruit_ads1x15/ads1x15.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def __init__(
9696
data_rate: Optional[int] = None,
9797
mode: int = Mode.SINGLE,
9898
comparator_queue_length: int = 0,
99-
comparator_low_threshold: int = 0x8000,
100-
comparator_high_threshold: int = 0x7FF0,
99+
comparator_low_threshold: int = -2048,
100+
comparator_high_threshold: int = 2047,
101101
address: int = _ADS1X15_DEFAULT_ADDRESS,
102102
):
103103
# pylint: disable=too-many-arguments
@@ -187,17 +187,24 @@ def comparator_low_threshold(self) -> int:
187187

188188
@comparator_low_threshold.setter
189189
def comparator_low_threshold(self, comparator_low_threshold: int) -> None:
190-
"""Sets 12-bit threshold in 16-bit register in unsigned format."""
191-
if comparator_low_threshold < 0 or comparator_low_threshold > 65535:
190+
"""Sets Comparator low threshold value"""
191+
if comparator_low_threshold < -2048 or comparator_low_threshold > 2047:
192192
raise ValueError(
193-
"Comparator Low Threshold must be unsigned 16-bit integer between 0 and 65535"
193+
"Comparator Low Threshold must be an integer between -2048 and 2047"
194194
)
195195
self._comparator_low_threshold = comparator_low_threshold
196196

197+
"""Convert integer value to 12-bit twos complement and bit shift"""
198+
if comparator_low_threshold < 0:
199+
tempval = 4096 + comparator_low_threshold
200+
else:
201+
tempval = comparator_low_threshold
202+
tempval <<= 4
203+
197204
"""Write value to chip"""
198205
self.buf[0] = _ADS1X15_POINTER_LO_THRES
199-
self.buf[1] = (self._comparator_low_threshold >> 8) & 0xFF
200-
self.buf[2] = self._comparator_low_threshold & 0xFF
206+
self.buf[1] = (tempval >> 8) & 0xFF
207+
self.buf[2] = tempval & 0xFF
201208
with self.i2c_device as i2c:
202209
i2c.write(self.buf)
203210

@@ -208,17 +215,24 @@ def comparator_high_threshold(self) -> int:
208215

209216
@comparator_high_threshold.setter
210217
def comparator_high_threshold(self, comparator_high_threshold: int) -> None:
211-
"""Sets 12-bit threshold in 16-bit register in unsigned format."""
212-
if comparator_high_threshold < 0 or comparator_high_threshold > 65535:
218+
"""Sets Comparator high threshold value"""
219+
if comparator_high_threshold < -2048 or comparator_high_threshold > 2047:
213220
raise ValueError(
214-
"Comparator High Threshold must be unsigned 16-bit integer between 0 and 65535"
221+
"Comparator High Threshold must be an integer between -2048 and 2047"
215222
)
216223
self._comparator_high_threshold = comparator_high_threshold
217224

225+
"""Convert integer value to 12-bit twos complement and bit shift"""
226+
if comparator_high_threshold < 0:
227+
tempval = 4096 + comparator_high_threshold
228+
else:
229+
tempval = comparator_high_threshold
230+
tempval <<= 4
231+
218232
"""Write value to chip"""
219233
self.buf[0] = _ADS1X15_POINTER_HI_THRES
220-
self.buf[1] = (self._comparator_high_threshold >> 8) & 0xFF
221-
self.buf[2] = self._comparator_high_threshold & 0xFF
234+
self.buf[1] = (tempval >> 8) & 0xFF
235+
self.buf[2] = tempval & 0xFF
222236
with self.i2c_device as i2c:
223237
i2c.write(self.buf)
224238

adafruit_ads1x15/analog_in.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ def voltage(self) -> float:
6666
return volts
6767

6868
def convert_to_value(self, volts: float) -> int:
69-
"""Calculates integer for threshold registers from voltage level input"""
70-
value = round((volts * 32767) / _ADS1X15_PGA_RANGE[self._ads.gain])
71-
if value < 0:
72-
value = 65536 + value
69+
"""Calculates 12-bit integer for threshold registers from voltage level input"""
70+
value = round(volts * 2047 / _ADS1X15_PGA_RANGE[self._ads.gain])
7371
return value
7472

7573
def convert_to_voltage(self, value: int) -> float:
76-
"""Calculates integer for threshold registers from voltage level input"""
74+
"""Calculates voltage from 16-bit ADC reading"""
7775
volts = value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
7876
if volts > _ADS1X15_PGA_RANGE[self._ads.gain]:
7977
volts = _ADS1X15_PGA_RANGE[self._ads.gain] - volts

0 commit comments

Comments
 (0)