Skip to content

Commit f128d33

Browse files
authored
Merge pull request #34 from caternuson/fast_read2
Fast channel reads via caching last channel - part deux
2 parents 6f0cfb2 + 1c4f21c commit f128d33

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

adafruit_ads1x15/ads1x15.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ def _conversion_value(self, raw_adc):
150150

151151
def _read(self, pin):
152152
"""Perform an ADC read. Returns the signed integer result of the read."""
153-
fast = True
154-
if self._last_pin_read != pin:
153+
if self.mode == Mode.CONTINUOUS and self._last_pin_read == pin:
154+
return self._conversion_value(self.get_last_result(True))
155+
else:
155156
self._last_pin_read = pin
156-
fast = False
157157
config = _ADS1X15_CONFIG_OS_SINGLE
158158
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
159159
config |= _ADS1X15_CONFIG_GAIN[self.gain]
@@ -162,11 +162,11 @@ def _read(self, pin):
162162
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
163163
self._write_register(_ADS1X15_POINTER_CONFIG, config)
164164

165-
if self.mode == Mode.SINGLE:
166-
while not self._conversion_complete():
167-
pass
165+
if self.mode == Mode.SINGLE:
166+
while not self._conversion_complete():
167+
pass
168168

169-
return self._conversion_value(self.get_last_result(fast))
169+
return self._conversion_value(self.get_last_result(False))
170170

171171
def _conversion_complete(self):
172172
"""Return status of ADC conversion."""
@@ -195,7 +195,6 @@ def _read_register(self, reg, fast=False):
195195
"""Read 16 bit register value. If fast is True, the pointer register
196196
is not updated.
197197
"""
198-
self.buf[0] = reg
199198
with self.i2c_device as i2c:
200199
if fast:
201200
i2c.readinto(self.buf, end=2)

adafruit_ads1x15/analog_in.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,5 @@ def value(self):
7676
@property
7777
def voltage(self):
7878
"""Returns the voltage from the ADC pin as a floating point value."""
79-
raw = self.value
80-
volts = raw * (_ADS1X15_PGA_RANGE[self._ads.gain] / (2**(self._ads.bits-1) - 1))
79+
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
8180
return volts

0 commit comments

Comments
 (0)