Skip to content

Commit 6f0cfb2

Browse files
authored
Merge pull request #33 from caternuson/fast_read2
Fast channel reads via caching last channel
2 parents 9e503e7 + a3d1c3f commit 6f0cfb2

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

adafruit_ads1x15/ads1x15.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class ADS1x15(object):
6666
def __init__(self, i2c, gain=1, data_rate=None, mode=Mode.SINGLE,
6767
address=_ADS1X15_DEFAULT_ADDRESS):
6868
#pylint: disable=too-many-arguments
69+
self._last_pin_read = None
6970
self.buf = bytearray(3)
7071
self._data_rate = self._gain = self._mode = None
7172
self.gain = gain
@@ -149,19 +150,23 @@ def _conversion_value(self, raw_adc):
149150

150151
def _read(self, pin):
151152
"""Perform an ADC read. Returns the signed integer result of the read."""
152-
config = _ADS1X15_CONFIG_OS_SINGLE
153-
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
154-
config |= _ADS1X15_CONFIG_GAIN[self.gain]
155-
config |= self.mode
156-
config |= self.rate_config[self.data_rate]
157-
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
158-
self._write_register(_ADS1X15_POINTER_CONFIG, config)
153+
fast = True
154+
if self._last_pin_read != pin:
155+
self._last_pin_read = pin
156+
fast = False
157+
config = _ADS1X15_CONFIG_OS_SINGLE
158+
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
159+
config |= _ADS1X15_CONFIG_GAIN[self.gain]
160+
config |= self.mode
161+
config |= self.rate_config[self.data_rate]
162+
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
163+
self._write_register(_ADS1X15_POINTER_CONFIG, config)
159164

160165
if self.mode == Mode.SINGLE:
161166
while not self._conversion_complete():
162167
pass
163168

164-
return self.get_last_result()
169+
return self._conversion_value(self.get_last_result(fast))
165170

166171
def _conversion_complete(self):
167172
"""Return status of ADC conversion."""
@@ -170,11 +175,13 @@ def _conversion_complete(self):
170175
# OS = 1: Device is not currently performing a conversion
171176
return self._read_register(_ADS1X15_POINTER_CONFIG) & 0x8000
172177

173-
def get_last_result(self):
178+
def get_last_result(self, fast=False):
174179
"""Read the last conversion result when in continuous conversion mode.
175-
Will return a signed integer value.
180+
Will return a signed integer value. If fast is True, the register
181+
pointer is not updated as part of the read. This reduces I2C traffic
182+
and increases possible read rate.
176183
"""
177-
return self._conversion_value(self._read_register(_ADS1X15_POINTER_CONVERSION))
184+
return self._read_register(_ADS1X15_POINTER_CONVERSION, fast)
178185

179186
def _write_register(self, reg, value):
180187
"""Write 16 bit value to register."""
@@ -184,10 +191,14 @@ def _write_register(self, reg, value):
184191
with self.i2c_device as i2c:
185192
i2c.write(self.buf)
186193

187-
def _read_register(self, reg):
188-
"""Read 16 bit register value."""
194+
def _read_register(self, reg, fast=False):
195+
"""Read 16 bit register value. If fast is True, the pointer register
196+
is not updated.
197+
"""
189198
self.buf[0] = reg
190199
with self.i2c_device as i2c:
191-
i2c.write(self.buf, end=1, stop=False)
192-
i2c.readinto(self.buf, end=2)
200+
if fast:
201+
i2c.readinto(self.buf, end=2)
202+
else:
203+
i2c.write_then_readinto(bytearray([reg]), self.buf, in_end=2, stop=False)
193204
return self.buf[0] << 8 | self.buf[1]

examples/ads1x15_fast_read.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_ads1x15.ads1015 as ADS
5+
from adafruit_ads1x15.ads1x15 import Mode
6+
from adafruit_ads1x15.analog_in import AnalogIn
7+
8+
# Data collection setup
9+
RATE = 3300
10+
SAMPLES = 1000
11+
12+
# Create the I2C bus with a fast frequency
13+
i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000)
14+
15+
# Create the ADC object using the I2C bus
16+
ads = ADS.ADS1015(i2c)
17+
18+
# Create single-ended input on channel 0
19+
chan0 = AnalogIn(ads, ADS.P0)
20+
21+
# ADC Configuration
22+
ads.mode = Mode.CONTINUOUS
23+
ads.data_rate = RATE
24+
25+
data = [None]*SAMPLES
26+
27+
start = time.monotonic()
28+
29+
# Read the same channel over and over
30+
for i in range(SAMPLES):
31+
data[i] = chan0.value
32+
33+
end = time.monotonic()
34+
total_time = end - start
35+
36+
print("Time of capture: {}s".format(total_time))
37+
print("Sample rate requested={} actual={}".format(RATE, SAMPLES / total_time))

0 commit comments

Comments
 (0)