From 664f24f654d2171e19c04610d86a73d4f5a55f05 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sun, 24 Oct 2021 20:54:53 -0400 Subject: [PATCH 1/6] Add typing --- adafruit_ads1x15/ads1015.py | 4 ++-- adafruit_ads1x15/ads1115.py | 4 ++-- adafruit_ads1x15/ads1x15.py | 39 +++++++++++++++++++++-------------- adafruit_ads1x15/analog_in.py | 8 ++++++- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/adafruit_ads1x15/ads1015.py b/adafruit_ads1x15/ads1015.py index d03d2bd..390a4a6 100644 --- a/adafruit_ads1x15/ads1015.py +++ b/adafruit_ads1x15/ads1015.py @@ -53,10 +53,10 @@ def rate_config(self): """Rate configuration masks.""" return _ADS1015_CONFIG_DR - def _data_rate_default(self): + def _data_rate_default(self) -> int: return 1600 - def _conversion_value(self, raw_adc): + def _conversion_value(self, raw_adc: int) -> int: raw_adc = raw_adc.to_bytes(2, "big") value = struct.unpack(">h", raw_adc)[0] return value >> 4 diff --git a/adafruit_ads1x15/ads1115.py b/adafruit_ads1x15/ads1115.py index df4509a..bd53f82 100644 --- a/adafruit_ads1x15/ads1115.py +++ b/adafruit_ads1x15/ads1115.py @@ -54,10 +54,10 @@ def rate_config(self): """Rate configuration masks.""" return _ADS1115_CONFIG_DR - def _data_rate_default(self): + def _data_rate_default(self) -> int: return 128 - def _conversion_value(self, raw_adc): + def _conversion_value(self, raw_adc: int) -> int: raw_adc = raw_adc.to_bytes(2, "big") value = struct.unpack(">h", raw_adc)[0] return value diff --git a/adafruit_ads1x15/ads1x15.py b/adafruit_ads1x15/ads1x15.py index b773cb7..ce84d93 100644 --- a/adafruit_ads1x15/ads1x15.py +++ b/adafruit_ads1x15/ads1x15.py @@ -17,6 +17,13 @@ import time from micropython import const from adafruit_bus_device.i2c_device import I2CDevice +from busio import I2C +from microcontroller import Pin + +try: + from typing import Optional +except ImportError: + pass _ADS1X15_DEFAULT_ADDRESS = const(0x48) _ADS1X15_POINTER_CONVERSION = const(0x00) @@ -49,11 +56,11 @@ class ADS1x15: def __init__( self, - i2c, - gain=1, - data_rate=None, - mode=Mode.SINGLE, - address=_ADS1X15_DEFAULT_ADDRESS, + i2c: I2C, + gain: float = 1, + data_rate: Optional[float] = None, + mode: Mode = Mode.SINGLE, + address: int = _ADS1X15_DEFAULT_ADDRESS, ): # pylint: disable=too-many-arguments self._last_pin_read = None @@ -70,7 +77,7 @@ def data_rate(self): return self._data_rate @data_rate.setter - def data_rate(self, rate): + def data_rate(self, rate: float): possible_rates = self.rates if rate not in possible_rates: raise ValueError("Data rate must be one of: {}".format(possible_rates)) @@ -92,7 +99,7 @@ def gain(self): return self._gain @gain.setter - def gain(self, gain): + def gain(self, gain: float): possible_gains = self.gains if gain not in possible_gains: raise ValueError("Gain must be one of: {}".format(possible_gains)) @@ -111,12 +118,12 @@ def mode(self): return self._mode @mode.setter - def mode(self, mode): + def mode(self, mode: Mode): if mode not in (Mode.CONTINUOUS, Mode.SINGLE): raise ValueError("Unsupported mode.") self._mode = mode - def read(self, pin, is_differential=False): + def read(self, pin: Pin, is_differential: bool = False) -> int: """I2C Interface for ADS1x15-based ADCs reads. params: @@ -126,19 +133,19 @@ def read(self, pin, is_differential=False): pin = pin if is_differential else pin + 0x04 return self._read(pin) - def _data_rate_default(self): + def _data_rate_default(self) -> int: """Retrieve the default data rate for this ADC (in samples per second). Should be implemented by subclasses. """ raise NotImplementedError("Subclasses must implement _data_rate_default!") - def _conversion_value(self, raw_adc): + def _conversion_value(self, raw_adc: int) -> int: """Subclasses should override this function that takes the 16 raw ADC values of a conversion result and returns a signed integer value. """ raise NotImplementedError("Subclass must implement _conversion_value function!") - def _read(self, pin): + def _read(self, pin: Pin) -> int: """Perform an ADC read. Returns the signed integer result of the read.""" # Immediately return conversion register result if in CONTINUOUS mode # and pin has not changed @@ -174,14 +181,14 @@ def _read(self, pin): return self._conversion_value(self.get_last_result(False)) - def _conversion_complete(self): + def _conversion_complete(self) -> int: """Return status of ADC conversion.""" # OS is bit 15 # OS = 0: Device is currently performing a conversion # OS = 1: Device is not currently performing a conversion return self._read_register(_ADS1X15_POINTER_CONFIG) & 0x8000 - def get_last_result(self, fast=False): + def get_last_result(self, fast: bool = False) -> int: """Read the last conversion result when in continuous conversion mode. Will return a signed integer value. If fast is True, the register pointer is not updated as part of the read. This reduces I2C traffic @@ -189,7 +196,7 @@ def get_last_result(self, fast=False): """ return self._read_register(_ADS1X15_POINTER_CONVERSION, fast) - def _write_register(self, reg, value): + def _write_register(self, reg: int, value: int): """Write 16 bit value to register.""" self.buf[0] = reg self.buf[1] = (value >> 8) & 0xFF @@ -197,7 +204,7 @@ def _write_register(self, reg, value): with self.i2c_device as i2c: i2c.write(self.buf) - def _read_register(self, reg, fast=False): + def _read_register(self, reg: int, fast: bool = False) -> int: """Read 16 bit register value. If fast is True, the pointer register is not updated. """ diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index ef0732c..da67228 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -11,6 +11,12 @@ * Author(s): Carter Nelson, adapted from MCP3xxx original by Brent Rubell """ +from .ads1x15 import ADS1x15 +try: + from typing import Optional +except ImportError: + pass + _ADS1X15_DIFF_CHANNELS = {(0, 1): 0, (0, 3): 1, (1, 3): 2, (2, 3): 3} _ADS1X15_PGA_RANGE = {2 / 3: 6.144, 1: 4.096, 2: 2.048, 4: 1.024, 8: 0.512, 16: 0.256} @@ -18,7 +24,7 @@ class AnalogIn: """AnalogIn Mock Implementation for ADC Reads.""" - def __init__(self, ads, positive_pin, negative_pin=None): + def __init__(self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None): """AnalogIn :param ads: The ads object. From 310fde3d8cc2a2b3418b374ce4841511c1883d63 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Mon, 25 Oct 2021 22:17:54 -0400 Subject: [PATCH 2/6] Address issues from pre-commit Changed CRLF to LF --- adafruit_ads1x15/analog_in.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index da67228..040770c 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -12,6 +12,7 @@ """ from .ads1x15 import ADS1x15 + try: from typing import Optional except ImportError: @@ -24,7 +25,9 @@ class AnalogIn: """AnalogIn Mock Implementation for ADC Reads.""" - def __init__(self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None): + def __init__( + self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None + ): """AnalogIn :param ads: The ads object. From f05560081eed0cb7519506304349dbf95f3ef6a0 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 27 Oct 2021 23:06:31 -0400 Subject: [PATCH 3/6] Made changes per review comments --- adafruit_ads1x15/ads1x15.py | 8 ++++---- adafruit_ads1x15/analog_in.py | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/adafruit_ads1x15/ads1x15.py b/adafruit_ads1x15/ads1x15.py index ce84d93..c8c8b7c 100644 --- a/adafruit_ads1x15/ads1x15.py +++ b/adafruit_ads1x15/ads1x15.py @@ -17,11 +17,11 @@ import time from micropython import const from adafruit_bus_device.i2c_device import I2CDevice -from busio import I2C -from microcontroller import Pin try: from typing import Optional + from busio import I2C + from microcontroller import Pin except ImportError: pass @@ -58,7 +58,7 @@ def __init__( self, i2c: I2C, gain: float = 1, - data_rate: Optional[float] = None, + data_rate: Optional[int] = None, mode: Mode = Mode.SINGLE, address: int = _ADS1X15_DEFAULT_ADDRESS, ): @@ -77,7 +77,7 @@ def data_rate(self): return self._data_rate @data_rate.setter - def data_rate(self, rate: float): + def data_rate(self, rate: int): possible_rates = self.rates if rate not in possible_rates: raise ValueError("Data rate must be one of: {}".format(possible_rates)) diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index 040770c..c3cd31f 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -11,10 +11,9 @@ * Author(s): Carter Nelson, adapted from MCP3xxx original by Brent Rubell """ -from .ads1x15 import ADS1x15 - try: from typing import Optional + from .ads1x15 import ADS1x15 except ImportError: pass From 3aa42618404834d6e82c893d77bfb03761a5b787 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 27 Oct 2021 23:07:47 -0400 Subject: [PATCH 4/6] Change CRLF to LF --- adafruit_ads1x15/analog_in.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index c3cd31f..da67228 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -11,9 +11,9 @@ * Author(s): Carter Nelson, adapted from MCP3xxx original by Brent Rubell """ +from .ads1x15 import ADS1x15 try: from typing import Optional - from .ads1x15 import ADS1x15 except ImportError: pass @@ -24,9 +24,7 @@ class AnalogIn: """AnalogIn Mock Implementation for ADC Reads.""" - def __init__( - self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None - ): + def __init__(self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None): """AnalogIn :param ads: The ads object. From 15f0379ba90795264da2a729a472843501f00c03 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 27 Oct 2021 23:08:41 -0400 Subject: [PATCH 5/6] Reformatting by black --- adafruit_ads1x15/analog_in.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index da67228..040770c 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -12,6 +12,7 @@ """ from .ads1x15 import ADS1x15 + try: from typing import Optional except ImportError: @@ -24,7 +25,9 @@ class AnalogIn: """AnalogIn Mock Implementation for ADC Reads.""" - def __init__(self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None): + def __init__( + self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None + ): """AnalogIn :param ads: The ads object. From f74dda9f2f5b6392731bdf43149828755a276f31 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Thu, 28 Oct 2021 10:14:47 -0400 Subject: [PATCH 6/6] Move ADS1x15 import into try block --- adafruit_ads1x15/analog_in.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index 040770c..c3cd31f 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -11,10 +11,9 @@ * Author(s): Carter Nelson, adapted from MCP3xxx original by Brent Rubell """ -from .ads1x15 import ADS1x15 - try: from typing import Optional + from .ads1x15 import ADS1x15 except ImportError: pass