Skip to content

Commit

Permalink
Addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
scirelli committed Sep 2, 2022
1 parent 641ba14 commit 5ca81ee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ _build
.idea
.vscode
*~
.venv
.env
6 changes: 3 additions & 3 deletions adafruit_ads1x15/ads1015.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions adafruit_ads1x15/ads1115.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 14 additions & 9 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"""

try:
from typing import Optional, cast

from typing import Optional
from .ads1x15 import ADS1x15
except ImportError:
pass
Expand All @@ -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(
Expand All @@ -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

0 comments on commit 5ca81ee

Please sign in to comment.