Skip to content

Commit f7fd261

Browse files
committed
Removed 2s Complement calculations
Conversion calculations handled with signed integers, removing 2s complement. Removed subclass comparator threshold defaults since both chips have same default values.
1 parent 657b768 commit f7fd261

File tree

4 files changed

+28
-74
lines changed

4 files changed

+28
-74
lines changed

adafruit_ads1x15/ads1015.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ def _data_rate_default(self) -> Literal[1600]:
6868
"""Default data rate setting is 1600 samples per second"""
6969
return 1600
7070

71-
def _comp_low_thres_default(self) -> Literal[0x8000]:
72-
"""Value is 12-bit, 2's complement stored in 16-bit register where 4 LSBs are 0.
73-
Defaults to 0x8000 as 16-bit hex (-2048 as 12-bit decimal)."""
74-
return 0x8000
75-
76-
def _comp_high_thres_default(self) -> Literal[0x7FF0]:
77-
"""Value is 12-bit, 2's complement stored in 16-bit register where 4 LSBs are 0.
78-
Defaults to 0x7FF0 as 16-bit hex (2047 as 12-bit decimal)."""
79-
return 0x7FF0
80-
8171
def _conversion_value(self, raw_adc: int) -> int:
8272
value = struct.unpack(">h", raw_adc.to_bytes(2, "big"))[0]
8373
return value

adafruit_ads1x15/ads1115.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,6 @@ def _data_rate_default(self) -> Literal[128]:
6969
"""Default data rate setting is 128 samples per second"""
7070
return 128
7171

72-
def _comp_low_thres_default(self) -> Literal[0x8000]:
73-
"""Value is 16-bit, 2's complement.
74-
Defaults to 0x8000 as 16-bit hex (-32768 16-bit decimal)."""
75-
return 0x8000
76-
77-
def _comp_high_thres_default(self) -> Literal[0x7FFF]:
78-
"""Value is 16-bit, 2's complement.
79-
Defaults to 0x7FFF as 16-bit hex (32767 16-bit decimal)."""
80-
return 0x7FFF
81-
8272
def _conversion_value(self, raw_adc: int) -> int:
8373
value = struct.unpack(">h", raw_adc.to_bytes(2, "big"))[0]
8474
return value

adafruit_ads1x15/ads1x15.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def __init__(
9494
data_rate: Optional[int] = None,
9595
mode: int = Mode.SINGLE,
9696
comparator_queue_length: int = 0,
97-
comparator_low_threshold: Optional[int] = None,
98-
comparator_high_threshold: Optional[int] = None,
97+
comparator_low_threshold: int = -32768,
98+
comparator_high_threshold: int = 32767,
9999
address: int = _ADS1X15_DEFAULT_ADDRESS,
100100
):
101101
# pylint: disable=too-many-arguments
@@ -106,16 +106,8 @@ def __init__(
106106
self.mode = mode
107107
self.comparator_queue_length = comparator_queue_length
108108
self.i2c_device = I2CDevice(i2c, address)
109-
self.comparator_low_threshold = (
110-
self._comp_low_thres_default()
111-
if comparator_low_threshold is None
112-
else comparator_low_threshold
113-
)
114-
self.comparator_high_threshold = (
115-
self._comp_high_thres_default()
116-
if comparator_high_threshold is None
117-
else comparator_high_threshold
118-
)
109+
self.comparator_low_threshold = comparator_low_threshold
110+
self.comparator_high_threshold = comparator_high_threshold
119111

120112
@property
121113
def bits(self) -> int:
@@ -198,23 +190,35 @@ def comparator_high_threshold(self) -> int:
198190

199191
@comparator_low_threshold.setter
200192
def comparator_low_threshold(self, value: int) -> None:
201-
"""Set comparator low threshold value for ADS1015 ADC
193+
"""Set comparator low threshold value for ADS1x15 ADC
202194
203195
:param int value: 16-bit signed integer to write to register
204196
"""
205-
if value < 0 or value > 65535:
206-
raise ValueError("Comparator Threshold value must be between 0 and 65535")
197+
if value < -32768 or value > 32767:
198+
raise ValueError(
199+
"Comparator Threshold value must be between -32768 and 32767"
200+
)
201+
202+
if (self.bits == 12) & (value & 0x000F > 0):
203+
print("4 LSBs will be truncated for ADS1015 for 12-bit value")
204+
207205
self._comparator_low_threshold = value
208206
self._write_register(_ADS1X15_POINTER_LO_THRES, self.comparator_low_threshold)
209207

210208
@comparator_high_threshold.setter
211209
def comparator_high_threshold(self, value: int) -> None:
212-
"""Set comparator high threshold value for ADS1015 ADC
210+
"""Set comparator high threshold value for ADS1x15 ADC
213211
214212
:param int value: 16-bit signed integer to write to register
215213
"""
216-
if value < 0 or value > 65535:
217-
raise ValueError("Comparator Threshold value must be between 0 and 65535")
214+
if value < -32768 or value > 32767:
215+
raise ValueError(
216+
"Comparator Threshold value must be between -32768 and 32767"
217+
)
218+
219+
if (self.bits == 12) & (value & 0x000F > 0):
220+
print("4 LSBs will be truncated for ADS1015 for 12-bit value")
221+
218222
self._comparator_high_threshold = value
219223
self._write_register(_ADS1X15_POINTER_HI_THRES, self.comparator_high_threshold)
220224

@@ -244,18 +248,6 @@ def _data_rate_default(self) -> int:
244248
"""
245249
raise NotImplementedError("Subclasses must implement _data_rate_default!")
246250

247-
def _comp_low_thres_default(self) -> int:
248-
"""Retrieve the default comparator low threshold for this ADC (in 16-bit signed int).
249-
Should be implemented by subclasses.
250-
"""
251-
raise NotImplementedError("Subclasses must implement _comp_low_thres_default!")
252-
253-
def _comp_high_thres_default(self) -> int:
254-
"""Retrieve the default comparator high threshold for this ADC (in 16-bit signed int).
255-
Should be implemented by subclasses.
256-
"""
257-
raise NotImplementedError("Subclasses must implement _comp_high_thres_default!")
258-
259251
def _conversion_value(self, raw_adc: int) -> int:
260252
"""Subclasses should override this function that takes the 16 raw ADC
261253
values of a conversion result and returns a signed integer value.

adafruit_ads1x15/analog_in.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,10 @@ def voltage(self) -> float:
6464
return volts
6565

6666
def convert_to_value(self, volts: float) -> int:
67-
"""Calculates 12-bit integer for threshold registers from voltage level input"""
68-
69-
# Convert 2's complement of signed int if number is negative
70-
if volts > 0:
71-
value = round(
72-
(volts / _ADS1X15_PGA_RANGE[self._ads.gain])
73-
* ((1 << (self._ads.bits - 1)) - 1)
74-
)
75-
else:
76-
value = round(
77-
(volts / _ADS1X15_PGA_RANGE[self._ads.gain])
78-
* (1 << (self._ads.bits - 1))
79-
)
80-
value += 1 << self._ads.bits
67+
"""Calculates a standard 16-bit integer value for a given voltage"""
68+
69+
lsb = _ADS1X15_PGA_RANGE[self._ads.gain] / (1 << (self._ads.bits - 1))
70+
value = int(volts / lsb)
8171

8272
# Need to bit shift if value is only 12-bits
8373
value <<= 16 - self._ads.bits
@@ -86,18 +76,10 @@ def convert_to_value(self, volts: float) -> int:
8676
def convert_to_voltage(self, value_int: int) -> float:
8777
"""Calculates voltage from 16-bit ADC reading"""
8878

89-
if value_int & 0x8000:
90-
# Need to convert negative number through 2's complement
91-
value_int -= 0x10000
79+
lsb = _ADS1X15_PGA_RANGE[self._ads.gain] / (1 << (self._ads.bits - 1))
9280

9381
# Need to bit shift if value is only 12-bits
9482
value_int >>= 16 - self._ads.bits
95-
96-
volts = float(value_int)
97-
volts = (
98-
volts
99-
* _ADS1X15_PGA_RANGE[self._ads.gain]
100-
/ (0x7FFF >> (16 - self._ads.bits))
101-
)
83+
volts = value_int * lsb
10284

10385
return volts

0 commit comments

Comments
 (0)