Skip to content

Fast channel reads via caching last channel - part deux #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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