Skip to content

Commit

Permalink
Merge pull request #86 from scirelli/enhancment/add_typing
Browse files Browse the repository at this point in the history
Adding typing
  • Loading branch information
tekktrik authored Sep 7, 2022
2 parents 1d9f141 + 588051a commit fe1337c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _build

# Virtual environment-specific files
.env
.venv

# MacOS-specific files
*.DS_Store
Expand Down
18 changes: 12 additions & 6 deletions adafruit_ads1x15/ads1015.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
"""
import struct

try:
from typing import Dict, List

from typing_extensions import Literal
except ImportError:
pass

# pylint: disable=unused-import
from .ads1x15 import ADS1x15, Mode

Expand Down Expand Up @@ -41,26 +48,25 @@ class ADS1015(ADS1x15):
"""Class for the ADS1015 12 bit ADC."""

@property
def bits(self):
def bits(self) -> Literal[12]:
"""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

def _data_rate_default(self) -> int:
def _data_rate_default(self) -> Literal[1600]:
return 1600

def _conversion_value(self, raw_adc: int) -> int:
raw_adc = raw_adc.to_bytes(2, "big")
value = struct.unpack(">h", raw_adc)[0]
value = struct.unpack(">h", raw_adc.to_bytes(2, "big"))[0]
return value >> 4
18 changes: 12 additions & 6 deletions adafruit_ads1x15/ads1115.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
"""
import struct

try:
from typing import Dict, List

from typing_extensions import Literal
except ImportError:
pass

# pylint: disable=unused-import
from .ads1x15 import ADS1x15, Mode

Expand Down Expand Up @@ -42,26 +49,25 @@ class ADS1115(ADS1x15):
"""Class for the ADS1115 16 bit ADC."""

@property
def bits(self):
def bits(self) -> Literal[16]:
"""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

def _data_rate_default(self) -> int:
def _data_rate_default(self) -> Literal[128]:
return 128

def _conversion_value(self, raw_adc: int) -> int:
raw_adc = raw_adc.to_bytes(2, "big")
value = struct.unpack(">h", raw_adc)[0]
value = struct.unpack(">h", raw_adc.to_bytes(2, "big"))[0]
return value
32 changes: 19 additions & 13 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15.git"

import time
from micropython import const

from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const

try:
from typing import Optional
from typing import Dict, List, Optional

from busio import I2C
from microcontroller import Pin
except ImportError:
Expand Down Expand Up @@ -69,66 +71,70 @@ def __init__(
i2c: I2C,
gain: float = 1,
data_rate: Optional[int] = None,
mode: Mode = Mode.SINGLE,
mode: int = Mode.SINGLE,
address: int = _ADS1X15_DEFAULT_ADDRESS,
):
# pylint: disable=too-many-arguments
self._last_pin_read = None
self.buf = bytearray(3)
self._data_rate = self._gain = self._mode = None
self.gain = gain
self.data_rate = self._data_rate_default() if data_rate is None else data_rate
self.mode = mode
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: Mode):
def mode(self, mode: int) -> None:
if mode not in (Mode.CONTINUOUS, Mode.SINGLE):
raise ValueError("Unsupported mode.")
self._mode = mode
Expand Down
4 changes: 2 additions & 2 deletions adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,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
Empty file added adafruit_ads1x15/py.typed
Empty file.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

Adafruit-Blinka
adafruit-circuitpython-busdevice
typing-extensions~=4.0

0 comments on commit fe1337c

Please sign in to comment.