Skip to content

Commit

Permalink
Added remaining typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Nov 12, 2021
1 parent b62cfde commit e5b89be
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 131 deletions.
24 changes: 12 additions & 12 deletions adafruit_mcp230xx/digital_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import digitalio

try:
import typing # pylint: disable=unused-import
from typing import Optional
from adafruit_mcp230xx.mcp23xxx import MCP23XXX
from digitalio import Pull, Direction
except ImportError:
Expand Down Expand Up @@ -48,7 +48,7 @@ class DigitalInOut:
configurations.
"""

def __init__(self, pin_number: int, mcp230xx: MCP23XXX):
def __init__(self, pin_number: int, mcp230xx: MCP23XXX) -> None:
"""Specify the pin number of the MCP230xx (0...7 for MCP23008, or 0...15
for MCP23017) and MCP23008 instance.
"""
Expand All @@ -60,7 +60,7 @@ def __init__(self, pin_number: int, mcp230xx: MCP23XXX):
# 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: bool = 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).
"""
Expand All @@ -69,7 +69,7 @@ def switch_to_output(self, value: bool = False, **kwargs):

def switch_to_input(
self, pull: Pull = None, invert_polarity: bool = False, **kwargs
):
) -> None:
"""Switch the pin state to a digital input with the provided starting
pull-up resistor state (optional, no pull-up by default) and input polarity. Note that
pull-down resistors are NOT supported!
Expand All @@ -81,22 +81,22 @@ def switch_to_input(
# pylint: enable=unused-argument

@property
def value(self):
def value(self) -> bool:
"""The value of the pin, either True for high or False for
low. Note you must configure as an output or input appropriately
before reading and writing this value.
"""
return _get_bit(self._mcp.gpio, self._pin)

@value.setter
def value(self, val: bool):
def value(self, val: bool) -> None:
if val:
self._mcp.gpio = _enable_bit(self._mcp.gpio, self._pin)
else:
self._mcp.gpio = _clear_bit(self._mcp.gpio, self._pin)

@property
def direction(self):
def direction(self) -> bool:
"""The direction of the pin, either True for an input or
False for an output.
"""
Expand All @@ -105,7 +105,7 @@ def direction(self):
return digitalio.Direction.OUTPUT

@direction.setter
def direction(self, val: Direction):
def direction(self, val: Direction) -> None:
if val == digitalio.Direction.INPUT:
self._mcp.iodir = _enable_bit(self._mcp.iodir, self._pin)
elif val == digitalio.Direction.OUTPUT:
Expand All @@ -114,7 +114,7 @@ def direction(self, val: Direction):
raise ValueError("Expected INPUT or OUTPUT direction!")

@property
def pull(self):
def pull(self) -> Optional[digitalio.Pull]:
"""Enable or disable internal pull-up resistors for this pin. A
value of digitalio.Pull.UP will enable a pull-up resistor, and None will
disable it. Pull-down resistors are NOT supported!
Expand All @@ -128,7 +128,7 @@ def pull(self):
return None

@pull.setter
def pull(self, val: Pull):
def pull(self, val: Pull) -> None:
try:
if val is None:
self._mcp.gppu = _clear_bit(self._mcp.gppu, self._pin)
Expand All @@ -143,7 +143,7 @@ def pull(self, val: Pull):
raise ValueError("Pull-up/pull-down resistors not supported.") from error

@property
def invert_polarity(self):
def invert_polarity(self) -> bool:
"""The polarity of the pin, either True for an Inverted or
False for an normal.
"""
Expand All @@ -152,7 +152,7 @@ def invert_polarity(self):
return False

@invert_polarity.setter
def invert_polarity(self, val: bool):
def invert_polarity(self, val: bool) -> None:
if val:
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
else:
Expand Down
16 changes: 9 additions & 7 deletions adafruit_mcp230xx/mcp23008.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class MCP23008(MCP230XX):
at the specified I2C address.
"""

def __init__(self, i2c: I2C, address: int = _MCP23008_ADDRESS, reset: bool = True):
def __init__(
self, i2c: I2C, address: int = _MCP23008_ADDRESS, reset: bool = True
) -> None:
super().__init__(i2c, address)

if reset:
Expand All @@ -53,38 +55,38 @@ def __init__(self, i2c: I2C, address: int = _MCP23008_ADDRESS, reset: bool = Tru
self._write_u8(_MCP23008_IPOL, 0x00)

@property
def gpio(self):
def gpio(self) -> int:
"""The raw GPIO output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high), assuming that
pin has been configured as an output previously.
"""
return self._read_u8(_MCP23008_GPIO)

@gpio.setter
def gpio(self, val: int):
def gpio(self, val: int) -> None:
self._write_u8(_MCP23008_GPIO, val)

@property
def iodir(self):
def iodir(self) -> int:
"""The raw IODIR direction register. Each bit represents
direction of a pin, either 1 for an input or 0 for an output mode.
"""
return self._read_u8(_MCP23008_IODIR)

@iodir.setter
def iodir(self, val: int):
def iodir(self, val: int) -> None:
self._write_u8(_MCP23008_IODIR, val)

@property
def gppu(self):
def gppu(self) -> int:
"""The raw GPPU pull-up register. Each bit represents
if a pull-up is enabled on the specified pin (1 = pull-up enabled,
0 = pull-up disabled). Note pull-down resistors are NOT supported!
"""
return self._read_u8(_MCP23008_GPPU)

@gppu.setter
def gppu(self, val: int):
def gppu(self, val: int) -> None:
self._write_u8(_MCP23008_GPPU, val)

def get_pin(self, pin: int) -> DigitalInOut:
Expand Down
32 changes: 17 additions & 15 deletions adafruit_mcp230xx/mcp23016.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class MCP23016(MCP230XX):
at the specified I2C address.
"""

def __init__(self, i2c: I2C, address: int = _MCP23016_ADDRESS, reset: bool = True):
def __init__(
self, i2c: I2C, address: int = _MCP23016_ADDRESS, reset: bool = True
) -> None:
super().__init__(i2c, address)

if reset:
Expand All @@ -60,72 +62,72 @@ def __init__(self, i2c: I2C, address: int = _MCP23016_ADDRESS, reset: bool = Tru
self._write_u16le(_MCP23016_IPOL0, 0x0000)

@property
def gpio(self):
def gpio(self) -> int:
"""The raw GPIO output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high), assuming that
pin has been configured as an output previously.
"""
return self._read_u16le(_MCP23016_GPIO0)

@gpio.setter
def gpio(self, val: int):
def gpio(self, val: int) -> None:
self._write_u16le(_MCP23016_GPIO0, val)

@property
def gpioa(self):
def gpioa(self) -> int:
"""The raw GPIO 0 output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high), assuming that
pin has been configured as an output previously.
"""
return self._read_u8(_MCP23016_GPIO0)

@gpioa.setter
def gpioa(self, val: int):
def gpioa(self, val: int) -> None:
self._write_u8(_MCP23016_GPIO0, val)

@property
def gpiob(self):
def gpiob(self) -> int:
"""The raw GPIO 1 output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high), assuming that
pin has been configured as an output previously.
"""
return self._read_u8(_MCP23016_GPIO1)

@gpiob.setter
def gpiob(self, val: int):
def gpiob(self, val: int) -> None:
self._write_u8(_MCP23016_GPIO1, val)

@property
def iodir(self):
def iodir(self) -> int:
"""The raw IODIR direction register. Each bit represents
direction of a pin, either 1 for an input or 0 for an output mode.
"""
return self._read_u16le(_MCP23016_IODIR0)

@iodir.setter
def iodir(self, val: int):
def iodir(self, val: int) -> None:
self._write_u16le(_MCP23016_IODIR0, val)

@property
def iodira(self):
def iodira(self) -> int:
"""The raw IODIR0 direction register. Each bit represents
direction of a pin, either 1 for an input or 0 for an output mode.
"""
return self._read_u8(_MCP23016_IODIR0)

@iodira.setter
def iodira(self, val: int):
def iodira(self, val: int) -> None:
self._write_u8(_MCP23016_IODIR0, val)

@property
def iodirb(self):
def iodirb(self) -> int:
"""The raw IODIR0 direction register. Each bit represents
direction of a pin, either 1 for an input or 0 for an output mode.
"""
return self._read_u8(_MCP23016_IODIR1)

@iodirb.setter
def iodirb(self, val: int):
def iodirb(self, val: int) -> None:
self._write_u8(_MCP23016_IODIR1, val)

def get_pin(self, pin: int) -> DigitalInOut:
Expand All @@ -136,10 +138,10 @@ def get_pin(self, pin: int) -> DigitalInOut:
raise ValueError("Pin number must be 0-15.")
return DigitalInOut(pin, self)

def clear_inta(self):
def clear_inta(self) -> None:
"""Clears port 0 interrupts."""
self._read_u8(_MCP23016_INTCAP0)

def clear_intb(self):
def clear_intb(self) -> None:
"""Clears port 1 interrupts."""
self._read_u8(_MCP23016_INTCAP1)
Loading

0 comments on commit e5b89be

Please sign in to comment.