Skip to content

Commit 8845b0f

Browse files
committed
Added functions for controlling additional comparator settings
Added classes and setter/getter functions for comparator mode, latch, and polarity settings. Also added a read_config function to read the configuration register of the ads1x15 chip.
1 parent bf390e7 commit 8845b0f

File tree

4 files changed

+133
-5
lines changed

4 files changed

+133
-5
lines changed

adafruit_ads1x15/ads1015.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
pass
2121

2222
# pylint: disable=unused-import
23-
from .ads1x15 import ADS1x15, Mode
23+
from .ads1x15 import ADS1x15
2424

2525
# Data sample rates
2626
_ADS1015_CONFIG_DR = {

adafruit_ads1x15/ads1115.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
pass
2121

2222
# pylint: disable=unused-import
23-
from .ads1x15 import ADS1x15, Mode
23+
from .ads1x15 import ADS1x15
2424

2525
# Data sample rates
2626
_ADS1115_CONFIG_DR = {

adafruit_ads1x15/ads1x15.py

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,42 @@ class Mode:
6666
"""Single-Shot Mode"""
6767

6868

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+
69105
class ADS1x15:
70106
"""Base functionality for ADS1x15 analog to digital converters.
71107
@@ -79,10 +115,17 @@ class ADS1x15:
79115
Defaults to 0 (comparator function disabled).
80116
:param int comparator_low_threshold: Voltage limit under which comparator de-asserts
81117
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
83119
:param int comparator_high_threshold: Voltage limit over which comparator asserts
84120
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'
86129
:param int address: The I2C address of the device.
87130
"""
88131

@@ -96,6 +139,9 @@ def __init__(
96139
comparator_queue_length: int = 0,
97140
comparator_low_threshold: int = -32768,
98141
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,
99145
address: int = _ADS1X15_DEFAULT_ADDRESS,
100146
):
101147
# pylint: disable=too-many-arguments
@@ -108,6 +154,9 @@ def __init__(
108154
self.i2c_device = I2CDevice(i2c, address)
109155
self.comparator_low_threshold = comparator_low_threshold
110156
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
111160

112161
@property
113162
def bits(self) -> int:
@@ -227,6 +276,39 @@ def mode(self, mode: int) -> None:
227276
raise ValueError("Unsupported mode.")
228277
self._mode = mode
229278

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+
230312
def read(self, pin: Pin, is_differential: bool = False) -> int:
231313
"""I2C Interface for ADS1x15-based ADCs reads.
232314
@@ -268,6 +350,9 @@ def _read(self, pin: Pin) -> int:
268350
config |= _ADS1X15_CONFIG_GAIN[self.gain]
269351
config |= self.mode
270352
config |= self.rate_config[self.data_rate]
353+
config |= self.comparator_mode
354+
config |= self.comparator_polarity
355+
config |= self.comparator_latch
271356
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.comparator_queue_length]
272357
self._write_register(_ADS1X15_POINTER_CONFIG, config)
273358

@@ -317,3 +402,35 @@ def _read_register(self, reg: int, fast: bool = False) -> int:
317402
else:
318403
i2c.write_then_readinto(bytearray([reg]), self.buf, in_end=2)
319404
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+
)

examples/ads1x15_comparator_example.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import adafruit_ads1x15.ads1015 as ADS
1010

1111
# import adafruit_ads1x15.ads1115 as ADS
12+
from adafruit_ads1x15.ads1x15 import Mode, Comp_Mode, Comp_Polarity, Comp_Latch
1213
from adafruit_ads1x15.analog_in import AnalogIn
1314

1415
# Create the I2C bus
@@ -26,9 +27,19 @@
2627
# Create Interrupt-driven input to track comparator changes
2728
int_pin = countio.Counter(board.GP9, edge=countio.Edge.RISE)
2829

30+
# Set ADC to continuously read new data
31+
ads.mode = Mode.CONTINUOUS
2932
# Set comparator to assert after 1 ADC conversion
3033
ads.comparator_queue_length = 1
31-
34+
# Set comparator to use traditional threshold instead of window
35+
ads.comparator_mode = Comp_Mode.TRADITIONAL
36+
# Set comparator output to de-assert if readings no longer above threshold
37+
ads.comparator_latch = Comp_Latch.NONLATCHING
38+
# Set comparator output to logic LOW when asserted
39+
ads.comparator_polarity = Comp_Polarity.ACTIVE_LOW
40+
41+
# Gain should be explicitly set to ensure threshold values are calculated correctly
42+
ads.gain = 1
3243
# Set comparator low threshold to 2V
3344
ads.comparator_low_threshold = chan.convert_to_value(2.000)
3445
# Set comparator high threshold to 2.002V. High threshold must be above low threshold

0 commit comments

Comments
 (0)