From 23be8dd884bc404f50a2feecd67d1131596aff7c Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 3 Jun 2019 17:30:02 -0700 Subject: [PATCH 1/2] tweak logic for single-shot, fix voltage --- adafruit_ads1x15/ads1x15.py | 15 +++++++-------- adafruit_ads1x15/analog_in.py | 3 +-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/adafruit_ads1x15/ads1x15.py b/adafruit_ads1x15/ads1x15.py index 82c1d34..e51194f 100644 --- a/adafruit_ads1x15/ads1x15.py +++ b/adafruit_ads1x15/ads1x15.py @@ -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] @@ -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.""" @@ -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) diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index 055d759..1d37097 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -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 From 1c4f21c3a6c5aa49bfc5c11d0e18db0d950696e4 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 3 Jun 2019 17:46:44 -0700 Subject: [PATCH 2/2] lint space --- adafruit_ads1x15/ads1x15.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ads1x15/ads1x15.py b/adafruit_ads1x15/ads1x15.py index e51194f..0f31f53 100644 --- a/adafruit_ads1x15/ads1x15.py +++ b/adafruit_ads1x15/ads1x15.py @@ -151,7 +151,7 @@ def _conversion_value(self, raw_adc): def _read(self, pin): """Perform an ADC read. Returns the signed integer result of the read.""" if self.mode == Mode.CONTINUOUS and self._last_pin_read == pin: - return self._conversion_value(self.get_last_result(True)) + return self._conversion_value(self.get_last_result(True)) else: self._last_pin_read = pin config = _ADS1X15_CONFIG_OS_SINGLE