Skip to content

Commit aeaaf4b

Browse files
committed
track last pin for fast reading
1 parent 9e503e7 commit aeaaf4b

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-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+
fast = False
156+
config = _ADS1X15_CONFIG_OS_SINGLE
157+
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
158+
config |= _ADS1X15_CONFIG_GAIN[self.gain]
159+
config |= self.mode
160+
config |= self.rate_config[self.data_rate]
161+
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
162+
self._write_register(_ADS1X15_POINTER_CONFIG, config)
159163

160164
if self.mode == Mode.SINGLE:
161165
while not self._conversion_complete():
162166
pass
163167

164-
return self.get_last_result()
168+
self._last_pin_read = pin
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]

0 commit comments

Comments
 (0)