Skip to content

Add Missing Type Annotations #8

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

Merged
merged 6 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 13 additions & 8 deletions adafruit_pcf8591/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@
* Author(s): Bryan Siepert, adpted from ADS1x15 by Carter Nelson
"""

try:
import typing # pylint: disable=unused-import
except ImportError:
pass


class AnalogIn:
"""AnalogIn Mock Implementation for ADC Reads."""

def __init__(self, pcf, pin):
def __init__(self, pcf: "PCF8591", pin: int) -> None:
"""AnalogIn

:param ads: The PCF8591 object.
Expand All @@ -42,38 +47,38 @@ def __init__(self, pcf, pin):
self._channel_number = pin

@property
def voltage(self):
def voltage(self) -> float:
"""Returns the value of an ADC channel in volts as compared to the reference voltage."""

if not self._pcf:
raise RuntimeError(
"Underlying ADC does not exist, likely due to callint `deinit`"
"Underlying ADC does not exist, likely due to calling `deinit`"
)
raw_reading = self._pcf.read(self._channel_number)
return ((raw_reading << 8) / 65535) * self._pcf.reference_voltage

@property
def value(self):
def value(self) -> int:
"""Returns the value of an ADC channel.
The value is scaled to a 16-bit integer from the native 8-bit value."""

if not self._pcf:
raise RuntimeError(
"Underlying ADC does not exist, likely due to callint `deinit`"
"Underlying ADC does not exist, likely due to calling `deinit`"
)

return self._pcf.read(self._channel_number) << 8

@property
def reference_voltage(self):
def reference_voltage(self) -> float:
"""The maximum voltage measurable (also known as the reference voltage) as a float in
Volts. Assumed to be 3.3V but can be overridden using the `PCF8591` constructor"""
if not self._pcf:
raise RuntimeError(
"Underlying ADC does not exist, likely due to callint `deinit`"
"Underlying ADC does not exist, likely due to calling `deinit`"
)
return self._pcf.reference_voltage

def deinit(self):
def deinit(self) -> None:
"""Release the reference to the PCF8591. Create a new AnalogIn to use it again."""
self._pcf = None
14 changes: 9 additions & 5 deletions adafruit_pcf8591/analog_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@

* Author(s): Bryan Siepert
"""
try:
import typing # pylint: disable=unused-import
except ImportError:
pass


class AnalogOut:
"""AnalogIn Mock Implementation for ADC Reads."""

def __init__(self, pcf, dac_pin=0):
def __init__(self, pcf: "PCF8591", dac_pin: int = 0) -> None:
"""AnalogIn

:param pcf: The pcf object.
Expand All @@ -45,24 +49,24 @@ def __init__(self, pcf, dac_pin=0):
self._pcf.dac_enabled = True

@property
def value(self):
def value(self) -> int:
"""Returns the currently set value of the DAC pin as an integer."""
return self._value

@value.setter
def value(self, new_value): # this may have to scale from 16-bit
def value(self, new_value: int) -> None: # this may have to scale from 16-bit
if new_value < 0 or new_value > 65535:
raise ValueError("value must be a 16-bit integer from 0-65535")

if not self._pcf.dac_enabled:
raise RuntimeError(
"Underlying DAC is disabled, likely due to callint `deinit`"
"Underlying DAC is disabled, likely due to calling `deinit`"
)
# underlying sensor is 8-bit, so scale accordingly
self._pcf.write(new_value >> 8)
self._value = new_value

def deinit(self):
def deinit(self) -> None:
"""Disable the underlying DAC and release the reference to the PCF8591.
Create a new AnalogOut to use it again."""
self._pcf.dac_enabled = False
Expand Down
26 changes: 19 additions & 7 deletions adafruit_pcf8591/pcf8591.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
from micropython import const
from adafruit_bus_device import i2c_device

try:
import typing # pylint: disable=unused-import
from typing_extensions import Literal
from busio import I2C
except ImportError:
pass

_PCF8591_DEFAULT_ADDR = const(0x48) # PCF8591 Default Address
_PCF8591_ENABLE_DAC = const(0x40) # control bit for having the DAC active

Expand All @@ -53,7 +60,12 @@ class PCF8591:

"""

def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3):
def __init__(
self,
i2c_bus: I2C,
address: int = _PCF8591_DEFAULT_ADDR,
reference_voltage: float = 3.3,
) -> None:
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
self._dacval = 0
self._dac_enabled = False
Expand All @@ -67,12 +79,12 @@ def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3
# user calls to `read`

@property
def reference_voltage(self):
def reference_voltage(self) -> float:
"""The voltage level that ADC signals are compared to.
An ADC value of 65535 will equal `reference_voltage`"""
return self._reference_voltage

def _half_read(self, channel):
def _half_read(self, channel: Literal[0, 1, 2, 3]) -> None:
if self._dac_enabled:
self._buffer[0] = _PCF8591_ENABLE_DAC
self._buffer[1] = self._dacval
Expand All @@ -85,7 +97,7 @@ def _half_read(self, channel):
with self.i2c_device as i2c:
i2c.write_then_readinto(self._buffer, self._buffer)

def read(self, channel):
def read(self, channel: Literal[0, 1, 2, 3]) -> None:
"""Read an analog value from one of the four ADC inputs

param: :channel The single-ended ADC channel to read from, 0 thru 3
Expand All @@ -101,17 +113,17 @@ def read(self, channel):
return unpack_from(">B", self._buffer[1:])[0]

@property
def dac_enabled(self):
def dac_enabled(self) -> bool:
"""Enables the DAC when True, or sets it to tri-state / high-Z when False"""
return self._dac_enabled

@dac_enabled.setter
def dac_enabled(self, enable_dac):
def dac_enabled(self, enable_dac: bool) -> None:

self._dac_enabled = enable_dac
self.write(self._dacval)

def write(self, value):
def write(self, value: int) -> None:
"""Writes a uint8_t value to the DAC output

param: :output The value to write: 0 is GND and 65535 is VCC
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
Adafruit-Blinka
adafruit-circuitpython-register
adafruit-circuitpython-busdevice
typing-extensions~=4.0