From 5ca81ee53fe296402f28758dcc778a484cc3ba32 Mon Sep 17 00:00:00 2001 From: scirelli Date: Fri, 2 Sep 2022 13:41:40 -0400 Subject: [PATCH] Addressing PR comments --- .gitignore | 2 +- adafruit_ads1x15/ads1015.py | 6 +++--- adafruit_ads1x15/ads1115.py | 6 +++--- adafruit_ads1x15/ads1x15.py | 23 ++++++++++++++--------- adafruit_ads1x15/analog_in.py | 9 ++++----- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 40be310..bf22dad 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,4 @@ _build .idea .vscode *~ -.venv +.env diff --git a/adafruit_ads1x15/ads1015.py b/adafruit_ads1x15/ads1015.py index 933795f..e03dfd0 100644 --- a/adafruit_ads1x15/ads1015.py +++ b/adafruit_ads1x15/ads1015.py @@ -41,19 +41,19 @@ class ADS1015(ADS1x15): """Class for the ADS1015 12 bit ADC.""" @property - def bits(self): + def bits(self) -> int: """The ADC bit resolution.""" return 12 @property - def rates(self): + def rates(self) -> list[int]: """Possible data rate settings.""" r = list(_ADS1015_CONFIG_DR.keys()) r.sort() return r @property - def rate_config(self): + def rate_config(self) -> dict[int, int]: """Rate configuration masks.""" return _ADS1015_CONFIG_DR diff --git a/adafruit_ads1x15/ads1115.py b/adafruit_ads1x15/ads1115.py index 3d9288d..b9f9243 100644 --- a/adafruit_ads1x15/ads1115.py +++ b/adafruit_ads1x15/ads1115.py @@ -42,19 +42,19 @@ class ADS1115(ADS1x15): """Class for the ADS1115 16 bit ADC.""" @property - def bits(self): + def bits(self) -> int: """The ADC bit resolution.""" return 16 @property - def rates(self): + def rates(self) -> list[int]: """Possible data rate settings.""" r = list(_ADS1115_CONFIG_DR.keys()) r.sort() return r @property - def rate_config(self): + def rate_config(self) -> dict[int, int]: """Rate configuration masks.""" return _ADS1115_CONFIG_DR diff --git a/adafruit_ads1x15/ads1x15.py b/adafruit_ads1x15/ads1x15.py index 1b21a2d..b96f68d 100644 --- a/adafruit_ads1x15/ads1x15.py +++ b/adafruit_ads1x15/ads1x15.py @@ -86,53 +86,58 @@ def __init__( self.i2c_device = I2CDevice(i2c, address) @property - def data_rate(self): + def bits(self) -> int: + """The ADC bit resolution.""" + raise NotImplementedError("Subclass must implement bits property.") + + @property + def data_rate(self) -> int: """The data rate for ADC conversion in samples per second.""" return self._data_rate @data_rate.setter - def data_rate(self, rate: int): + def data_rate(self, rate: int) -> None: possible_rates = self.rates if rate not in possible_rates: raise ValueError("Data rate must be one of: {}".format(possible_rates)) self._data_rate = rate @property - def rates(self): + def rates(self) -> list[int]: """Possible data rate settings.""" raise NotImplementedError("Subclass must implement rates property.") @property - def rate_config(self): + def rate_config(self) -> dict[int, int]: """Rate configuration masks.""" raise NotImplementedError("Subclass must implement rate_config property.") @property - def gain(self): + def gain(self) -> float: """The ADC gain.""" return self._gain @gain.setter - def gain(self, gain: float): + def gain(self, gain: float) -> None: possible_gains = self.gains if gain not in possible_gains: raise ValueError("Gain must be one of: {}".format(possible_gains)) self._gain = gain @property - def gains(self): + def gains(self) -> list[float]: """Possible gain settings.""" g = list(_ADS1X15_CONFIG_GAIN.keys()) g.sort() return g @property - def mode(self): + def mode(self) -> int: """The ADC conversion mode.""" return self._mode @mode.setter - def mode(self, mode: int): + def mode(self, mode: int) -> None: if mode not in (Mode.CONTINUOUS, Mode.SINGLE): raise ValueError("Unsupported mode.") self._mode = mode diff --git a/adafruit_ads1x15/analog_in.py b/adafruit_ads1x15/analog_in.py index c79ea97..310c092 100644 --- a/adafruit_ads1x15/analog_in.py +++ b/adafruit_ads1x15/analog_in.py @@ -12,8 +12,7 @@ """ try: - from typing import Optional, cast - + from typing import Optional from .ads1x15 import ADS1x15 except ImportError: pass @@ -38,7 +37,7 @@ def __init__( self._negative_pin = negative_pin self.is_differential = False if negative_pin is not None: - pins = (self._pin_setting, cast(int, self._negative_pin)) + pins = (self._pin_setting, self._negative_pin) if pins not in _ADS1X15_DIFF_CHANNELS: raise ValueError( "Differential channels must be one of: {}".format( @@ -49,14 +48,14 @@ def __init__( self.is_differential = True @property - def value(self): + def value(self) -> int: """Returns the value of an ADC pin as an integer.""" return self._ads.read( self._pin_setting, is_differential=self.is_differential ) << (16 - self._ads.bits) @property - def voltage(self): + def voltage(self) -> float: """Returns the voltage from the ADC pin as a floating point value.""" volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767 return volts