Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing to library #73

Merged
merged 6 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions adafruit_ads1x15/ads1015.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions adafruit_ads1x15/ads1115.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 23 additions & 16 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
from micropython import const
from adafruit_bus_device.i2c_device import I2CDevice

try:
from typing import Optional
FoamyGuy marked this conversation as resolved.
Show resolved Hide resolved
from busio import I2C
from microcontroller import Pin
except ImportError:
pass

_ADS1X15_DEFAULT_ADDRESS = const(0x48)
_ADS1X15_POINTER_CONVERSION = const(0x00)
_ADS1X15_POINTER_CONFIG = const(0x01)
Expand Down Expand Up @@ -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[int] = None,
mode: Mode = Mode.SINGLE,
address: int = _ADS1X15_DEFAULT_ADDRESS,
):
# pylint: disable=too-many-arguments
self._last_pin_read = None
Expand All @@ -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: int):
possible_rates = self.rates
if rate not in possible_rates:
raise ValueError("Data rate must be one of: {}".format(possible_rates))
Expand All @@ -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))
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -174,30 +181,30 @@ 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
and increases possible read rate.
"""
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
self.buf[2] = value & 0xFF
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.
"""
Expand Down
10 changes: 9 additions & 1 deletion adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
* Author(s): Carter Nelson, adapted from MCP3xxx original by Brent Rubell
"""

try:
from typing import Optional
FoamyGuy marked this conversation as resolved.
Show resolved Hide resolved
from .ads1x15 import ADS1x15
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}


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.
Expand Down