Skip to content

Add Missing Type Annotations #7

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 2 commits into from
Jan 20, 2023
Merged
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
35 changes: 17 additions & 18 deletions adafruit_pcf8574.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

try:
# This is only needed for typing
import busio # pylint: disable=unused-import
from typing import Optional
from busio import I2C
except ImportError:
pass

Expand All @@ -51,36 +52,34 @@ class PCF8574:
:param int address: The I2C device address. Default is :const:`0x20`
"""

def __init__(
self, i2c_bus: busio.I2C, address: int = PCF8574_I2CADDR_DEFAULT
) -> None:
def __init__(self, i2c_bus: I2C, address: int = PCF8574_I2CADDR_DEFAULT) -> None:
self.i2c_device = I2CDevice(i2c_bus, address)
self._writebuf = bytearray(1)
self._writebuf[0] = 0
self._readbuf = bytearray(1)
self._readbuf[0] = 0

def get_pin(self, pin):
def get_pin(self, pin: int) -> "DigitalInOut":
"""Convenience function to create an instance of the DigitalInOut class
pointing at the specified pin of this PCF8574 device.
:param int pin: pin to use for digital IO, 0 to 7
"""
assert 0 <= pin <= 7
return DigitalInOut(pin, self)

def write_gpio(self, val):
def write_gpio(self, val: int) -> None:
"""Write a full 8-bit value to the GPIO register"""
self._writebuf[0] = val & 0xFF
with self.i2c_device as i2c:
i2c.write(self._writebuf)

def read_gpio(self):
def read_gpio(self) -> int:
"""Read the full 8-bits of data from the GPIO register"""
with self.i2c_device as i2c:
i2c.readinto(self._readbuf)
return self._readbuf[0]

def write_pin(self, pin, val):
def write_pin(self, pin: int, val: bool) -> None:
"""Set a single GPIO pin high/pulled-up or driven low"""
if val:
# turn on the pullup (write high)
Expand All @@ -89,7 +88,7 @@ def write_pin(self, pin, val):
# turn on the transistor (write low)
self.write_gpio(self._writebuf[0] & ~(1 << pin))

def read_pin(self, pin):
def read_pin(self, pin: int) -> bool:
"""Read a single GPIO pin as high/pulled-up or driven low"""
return (self.read_gpio() >> pin) & 0x1

Expand All @@ -114,7 +113,7 @@ class DigitalInOut:
configurations.
"""

def __init__(self, pin_number, pcf):
def __init__(self, pin_number: int, pcf: PCF8574) -> None:
"""Specify the pin number of the PCF8574 0..7, and instance."""
self._pin = pin_number
self._pcf = pcf
Expand All @@ -127,14 +126,14 @@ def __init__(self, pin_number, pcf):
# is unused by this class). Do not remove them, instead turn off pylint
# in this case.
# pylint: disable=unused-argument
def switch_to_output(self, value=False, **kwargs):
def switch_to_output(self, value: bool = False, **kwargs) -> None:
"""Switch the pin state to a digital output with the provided starting
value (True/False for high or low, default is False/low).
"""
self.direction = digitalio.Direction.OUTPUT
self.value = value

def switch_to_input(self, pull=None, **kwargs):
def switch_to_input(self, pull: Optional[digitalio.Pull] = None, **kwargs) -> None:
"""Switch the pin state to a digital input which is the same as
setting the light pullup on. Note that true tri-state or
pull-down resistors are NOT supported!
Expand All @@ -145,26 +144,26 @@ def switch_to_input(self, pull=None, **kwargs):
# pylint: enable=unused-argument

@property
def value(self):
def value(self) -> bool:
"""The value of the pin, either True for high/pulled-up or False for
low.
"""
return self._pcf.read_pin(self._pin)

@value.setter
def value(self, val):
def value(self, val: bool) -> None:
self._pcf.write_pin(self._pin, val)

@property
def direction(self):
def direction(self) -> digitalio.Direction:
"""
Setting a pin to OUTPUT drives it low, setting it to
an INPUT enables the light pullup.
"""
return self._dir

@direction.setter
def direction(self, val):
def direction(self, val: digitalio.Direction) -> None:
if val == digitalio.Direction.INPUT:
# for inputs, turn on the pullup (write high)
self._pcf.write_pin(self._pin, True)
Expand All @@ -177,14 +176,14 @@ def direction(self, val):
raise ValueError("Expected INPUT or OUTPUT direction!")

@property
def pull(self):
def pull(self) -> digitalio.Pull.UP:
"""
Pull-up is always activated so always return the same thing
"""
return digitalio.Pull.UP

@pull.setter
def pull(self, val):
def pull(self, val: digitalio.Pull.UP) -> None:
if val is digitalio.Pull.UP:
# for inputs, turn on the pullup (write high)
self._pcf.write_pin(self._pin, True)
Expand Down