Skip to content

Commit

Permalink
Merge pull request #34 from caternuson/fast_read2
Browse files Browse the repository at this point in the history
Fast channel reads via caching last channel - part deux
  • Loading branch information
ladyada authored Jun 6, 2019
2 parents 6f0cfb2 + 1c4f21c commit f128d33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
15 changes: 7 additions & 8 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def _conversion_value(self, raw_adc):

def _read(self, pin):
"""Perform an ADC read. Returns the signed integer result of the read."""
fast = True
if self._last_pin_read != pin:
if self.mode == Mode.CONTINUOUS and self._last_pin_read == pin:
return self._conversion_value(self.get_last_result(True))
else:
self._last_pin_read = pin
fast = False
config = _ADS1X15_CONFIG_OS_SINGLE
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
config |= _ADS1X15_CONFIG_GAIN[self.gain]
Expand All @@ -162,11 +162,11 @@ def _read(self, pin):
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
self._write_register(_ADS1X15_POINTER_CONFIG, config)

if self.mode == Mode.SINGLE:
while not self._conversion_complete():
pass
if self.mode == Mode.SINGLE:
while not self._conversion_complete():
pass

return self._conversion_value(self.get_last_result(fast))
return self._conversion_value(self.get_last_result(False))

def _conversion_complete(self):
"""Return status of ADC conversion."""
Expand Down Expand Up @@ -195,7 +195,6 @@ def _read_register(self, reg, fast=False):
"""Read 16 bit register value. If fast is True, the pointer register
is not updated.
"""
self.buf[0] = reg
with self.i2c_device as i2c:
if fast:
i2c.readinto(self.buf, end=2)
Expand Down
3 changes: 1 addition & 2 deletions adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,5 @@ def value(self):
@property
def voltage(self):
"""Returns the voltage from the ADC pin as a floating point value."""
raw = self.value
volts = raw * (_ADS1X15_PGA_RANGE[self._ads.gain] / (2**(self._ads.bits-1) - 1))
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
return volts

0 comments on commit f128d33

Please sign in to comment.