Merge pull request #34 from caternuson/fast_read2

Fast channel reads via caching last channel - part deux
This commit is contained in:
Limor "Ladyada" Fried 2019-06-06 12:40:19 -04:00 committed by GitHub
commit f128d33d01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 10 deletions

View file

@ -150,10 +150,10 @@ class ADS1x15(object):
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]
@ -162,11 +162,11 @@ class ADS1x15(object):
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."""
@ -195,7 +195,6 @@ class ADS1x15(object):
"""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)

View file

@ -76,6 +76,5 @@ class AnalogIn():
@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