From 70e949ff268f3201ce79e5f6c6821a1c495b4e7c Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 8 Nov 2021 17:29:04 -0500 Subject: [PATCH 1/4] Add type hints Using digitalio.DigitalInOut long form where appropriate so it's not confused with the library's custom version of it, and doesn't have an impact on existing code --- adafruit_mcp230xx/digital_inout.py | 27 ++++++++++++------- adafruit_mcp230xx/mcp23008.py | 16 +++++++---- adafruit_mcp230xx/mcp23016.py | 22 +++++++++------ adafruit_mcp230xx/mcp23017.py | 42 ++++++++++++++++------------- adafruit_mcp230xx/mcp230xx.py | 8 +++--- adafruit_mcp230xx/mcp23s08.py | 17 ++++++++---- adafruit_mcp230xx/mcp23s17.py | 43 +++++++++++++++++------------- adafruit_mcp230xx/mcp23sxx.py | 17 ++++++++---- adafruit_mcp230xx/mcp23xxx.py | 9 ++++++- 9 files changed, 127 insertions(+), 74 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 25cbe71..1428d3c 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -14,19 +14,26 @@ import digitalio +try: + import typing # pylint: disable=unused-import + from adafruit_mcp230xx.mcp230xx import MCP230XX + from digitalio import Pull, Direction +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" # Internal helpers to simplify setting and getting a bit inside an integer. -def _get_bit(val, bit): +def _get_bit(val, bit: int) -> int: return val & (1 << bit) > 0 -def _enable_bit(val, bit): +def _enable_bit(val, bit: int) -> int: return val | (1 << bit) -def _clear_bit(val, bit): +def _clear_bit(val, bit: int) -> int: return val & ~(1 << bit) @@ -41,7 +48,7 @@ class DigitalInOut: configurations. """ - def __init__(self, pin_number, mcp230xx): + def __init__(self, pin_number: int, mcp230xx: MCP230XX): """Specify the pin number of the MCP230xx (0...7 for MCP23008, or 0...15 for MCP23017) and MCP23008 instance. """ @@ -53,14 +60,14 @@ def __init__(self, pin_number, mcp230xx): # 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): """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, invert_polarity=False, **kwargs): + def switch_to_input(self, pull: Pull = None, invert_polarity: bool = False, **kwargs): """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! @@ -80,7 +87,7 @@ def value(self): return _get_bit(self._mcp.gpio, self._pin) @value.setter - def value(self, val): + def value(self, val: bool): if val: self._mcp.gpio = _enable_bit(self._mcp.gpio, self._pin) else: @@ -96,7 +103,7 @@ def direction(self): return digitalio.Direction.OUTPUT @direction.setter - def direction(self, val): + def direction(self, val: Direction): if val == digitalio.Direction.INPUT: self._mcp.iodir = _enable_bit(self._mcp.iodir, self._pin) elif val == digitalio.Direction.OUTPUT: @@ -119,7 +126,7 @@ def pull(self): return None @pull.setter - def pull(self, val): + def pull(self, val: Pull): try: if val is None: self._mcp.gppu = _clear_bit(self._mcp.gppu, self._pin) @@ -143,7 +150,7 @@ def invert_polarity(self): return False @invert_polarity.setter - def invert_polarity(self, val): + def invert_polarity(self, val: bool): if val: self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin) else: diff --git a/adafruit_mcp230xx/mcp23008.py b/adafruit_mcp230xx/mcp23008.py index 7de2476..a4d472a 100644 --- a/adafruit_mcp230xx/mcp23008.py +++ b/adafruit_mcp230xx/mcp23008.py @@ -16,6 +16,12 @@ from .mcp230xx import MCP230XX from .digital_inout import DigitalInOut +try: + import typing # pylint: disable=unused-import + from busio import I2C +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" @@ -37,7 +43,7 @@ class MCP23008(MCP230XX): at the specified I2C address. """ - def __init__(self, i2c, address=_MCP23008_ADDRESS, reset=True): + def __init__(self, i2c: I2C, address: int = _MCP23008_ADDRESS, reset: bool = True): super().__init__(i2c, address) if reset: @@ -55,7 +61,7 @@ def gpio(self): return self._read_u8(_MCP23008_GPIO) @gpio.setter - def gpio(self, val): + def gpio(self, val: int): self._write_u8(_MCP23008_GPIO, val) @property @@ -66,7 +72,7 @@ def iodir(self): return self._read_u8(_MCP23008_IODIR) @iodir.setter - def iodir(self, val): + def iodir(self, val: int): self._write_u8(_MCP23008_IODIR, val) @property @@ -78,10 +84,10 @@ def gppu(self): return self._read_u8(_MCP23008_GPPU) @gppu.setter - def gppu(self, val): + def gppu(self, val: int): self._write_u8(_MCP23008_GPPU, val) - 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 MCP23008 device. """ diff --git a/adafruit_mcp230xx/mcp23016.py b/adafruit_mcp230xx/mcp23016.py index a68306b..347ca90 100644 --- a/adafruit_mcp230xx/mcp23016.py +++ b/adafruit_mcp230xx/mcp23016.py @@ -24,6 +24,12 @@ from .mcp230xx import MCP230XX from .digital_inout import DigitalInOut +try: + import typing # pylint: disable=unused-import + from busio import I2C +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" @@ -45,7 +51,7 @@ class MCP23016(MCP230XX): at the specified I2C address. """ - def __init__(self, i2c, address=_MCP23016_ADDRESS, reset=True): + def __init__(self, i2c: I2C, address: int = _MCP23016_ADDRESS, reset: bool = True): super().__init__(i2c, address) if reset: @@ -62,7 +68,7 @@ def gpio(self): return self._read_u16le(_MCP23016_GPIO0) @gpio.setter - def gpio(self, val): + def gpio(self, val: int): self._write_u16le(_MCP23016_GPIO0, val) @property @@ -74,7 +80,7 @@ def gpioa(self): return self._read_u8(_MCP23016_GPIO0) @gpioa.setter - def gpioa(self, val): + def gpioa(self, val: int): self._write_u8(_MCP23016_GPIO0, val) @property @@ -86,7 +92,7 @@ def gpiob(self): return self._read_u8(_MCP23016_GPIO1) @gpiob.setter - def gpiob(self, val): + def gpiob(self, val: int): self._write_u8(_MCP23016_GPIO1, val) @property @@ -97,7 +103,7 @@ def iodir(self): return self._read_u16le(_MCP23016_IODIR0) @iodir.setter - def iodir(self, val): + def iodir(self, val: int): self._write_u16le(_MCP23016_IODIR0, val) @property @@ -108,7 +114,7 @@ def iodira(self): return self._read_u8(_MCP23016_IODIR0) @iodira.setter - def iodira(self, val): + def iodira(self, val: int): self._write_u8(_MCP23016_IODIR0, val) @property @@ -119,10 +125,10 @@ def iodirb(self): return self._read_u8(_MCP23016_IODIR1) @iodirb.setter - def iodirb(self, val): + def iodirb(self, val: int): self._write_u8(_MCP23016_IODIR1, val) - 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 MCP23016 device. """ diff --git a/adafruit_mcp230xx/mcp23017.py b/adafruit_mcp230xx/mcp23017.py index 4a2fdba..1ef5364 100644 --- a/adafruit_mcp230xx/mcp23017.py +++ b/adafruit_mcp230xx/mcp23017.py @@ -18,6 +18,12 @@ from .mcp230xx import MCP230XX from .digital_inout import DigitalInOut +try: + import typing # pylint: disable=unused-import + from busio import I2C +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" @@ -45,7 +51,7 @@ class MCP23017(MCP230XX): at the specified I2C address. """ - def __init__(self, i2c, address=_MCP23017_ADDRESS, reset=True): + def __init__(self, i2c: I2C, address: int = _MCP23017_ADDRESS, reset: bool = True): super().__init__(i2c, address) if reset: # Reset to all inputs with no pull-ups and no inverted polarity. @@ -63,7 +69,7 @@ def gpio(self): return self._read_u16le(_MCP23017_GPIOA) @gpio.setter - def gpio(self, val): + def gpio(self, val: int): self._write_u16le(_MCP23017_GPIOA, val) @property @@ -75,7 +81,7 @@ def gpioa(self): return self._read_u8(_MCP23017_GPIOA) @gpioa.setter - def gpioa(self, val): + def gpioa(self, val: int): self._write_u8(_MCP23017_GPIOA, val) @property @@ -87,7 +93,7 @@ def gpiob(self): return self._read_u8(_MCP23017_GPIOB) @gpiob.setter - def gpiob(self, val): + def gpiob(self, val: int): self._write_u8(_MCP23017_GPIOB, val) @property @@ -98,7 +104,7 @@ def iodir(self): return self._read_u16le(_MCP23017_IODIRA) @iodir.setter - def iodir(self, val): + def iodir(self, val: int): self._write_u16le(_MCP23017_IODIRA, val) @property @@ -109,7 +115,7 @@ def iodira(self): return self._read_u8(_MCP23017_IODIRA) @iodira.setter - def iodira(self, val): + def iodira(self, val: int): self._write_u8(_MCP23017_IODIRA, val) @property @@ -120,7 +126,7 @@ def iodirb(self): return self._read_u8(_MCP23017_IODIRB) @iodirb.setter - def iodirb(self, val): + def iodirb(self, val: int): self._write_u8(_MCP23017_IODIRB, val) @property @@ -132,7 +138,7 @@ def gppu(self): return self._read_u16le(_MCP23017_GPPUA) @gppu.setter - def gppu(self, val): + def gppu(self, val: int): self._write_u16le(_MCP23017_GPPUA, val) @property @@ -144,7 +150,7 @@ def gppua(self): return self._read_u8(_MCP23017_GPPUA) @gppua.setter - def gppua(self, val): + def gppua(self, val: int): self._write_u8(_MCP23017_GPPUA, val) @property @@ -156,10 +162,10 @@ def gppub(self): return self._read_u8(_MCP23017_GPPUB) @gppub.setter - def gppub(self, val): + def gppub(self, val: int): self._write_u8(_MCP23017_GPPUB, val) - 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 MCP23017 device. """ @@ -176,7 +182,7 @@ def ipol(self): return self._read_u16le(_MCP23017_IPOLA) @ipol.setter - def ipol(self, val): + def ipol(self, val: int): self._write_u16le(_MCP23017_IPOLA, val) @property @@ -188,7 +194,7 @@ def ipola(self): return self._read_u8(_MCP23017_IPOLA) @ipola.setter - def ipola(self, val): + def ipola(self, val: int): self._write_u8(_MCP23017_IPOLA, val) @property @@ -200,7 +206,7 @@ def ipolb(self): return self._read_u8(_MCP23017_IPOLB) @ipolb.setter - def ipolb(self, val): + def ipolb(self, val: int): self._write_u8(_MCP23017_IPOLB, val) @property @@ -215,7 +221,7 @@ def interrupt_configuration(self): return self._read_u16le(_MCP23017_INTCONA) @interrupt_configuration.setter - def interrupt_configuration(self, val): + def interrupt_configuration(self, val: int): self._write_u16le(_MCP23017_INTCONA, val) @property @@ -229,7 +235,7 @@ def interrupt_enable(self): return self._read_u16le(_MCP23017_GPINTENA) @interrupt_enable.setter - def interrupt_enable(self, val): + def interrupt_enable(self, val: int): self._write_u16le(_MCP23017_GPINTENA, val) @property @@ -242,7 +248,7 @@ def default_value(self): return self._read_u16le(_MCP23017_DEFVALA) @default_value.setter - def default_value(self, val): + def default_value(self, val: int): self._write_u16le(_MCP23017_DEFVALA, val) @property @@ -258,7 +264,7 @@ def io_control(self): return self._read_u8(_MCP23017_IOCON) @io_control.setter - def io_control(self, val): + def io_control(self, val: int): val &= ~0x80 self._write_u8(_MCP23017_IOCON, val) diff --git a/adafruit_mcp230xx/mcp230xx.py b/adafruit_mcp230xx/mcp230xx.py index 5993bca..8dc2a9e 100644 --- a/adafruit_mcp230xx/mcp230xx.py +++ b/adafruit_mcp230xx/mcp230xx.py @@ -28,7 +28,7 @@ class MCP230XX(MCP23XXX): """Base class for MCP230xx devices.""" - def _read_u16le(self, register): + def _read_u16le(self, register: int) -> int: # Read an unsigned 16 bit little endian value from the specified 8-bit # register. with self._device as bus_device: @@ -39,7 +39,7 @@ def _read_u16le(self, register): ) return (_BUFFER[2] << 8) | _BUFFER[1] - def _write_u16le(self, register, val): + def _write_u16le(self, register: int, val: int): # Write an unsigned 16 bit little endian value to the specified 8-bit # register. with self._device as bus_device: @@ -48,7 +48,7 @@ def _write_u16le(self, register, val): _BUFFER[2] = (val >> 8) & 0xFF bus_device.write(_BUFFER, end=3) - def _read_u8(self, register): + def _read_u8(self, register: int): # Read an unsigned 8 bit value from the specified 8-bit register. with self._device as bus_device: _BUFFER[0] = register & 0xFF @@ -58,7 +58,7 @@ def _read_u8(self, register): ) return _BUFFER[1] - def _write_u8(self, register, val): + def _write_u8(self, register: int, val: int): # Write an 8 bit value to the specified 8-bit register. with self._device as bus_device: _BUFFER[0] = register & 0xFF diff --git a/adafruit_mcp230xx/mcp23s08.py b/adafruit_mcp230xx/mcp23s08.py index 06b7472..ef67fe7 100644 --- a/adafruit_mcp230xx/mcp23s08.py +++ b/adafruit_mcp230xx/mcp23s08.py @@ -17,6 +17,13 @@ from .mcp23sxx import MCP23SXX from .digital_inout import DigitalInOut +try: + import typing # pylint: disable=unused-import + from busio import SPI + import digitalio +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP23Sxx.git" @@ -39,7 +46,7 @@ class MCP23S08(MCP23SXX): """ def __init__( - self, spi, chip_select, address=_MCP23S08_ADDRESS, reset=True, baudrate=100000 + self, spi: SPI, chip_select: digitalio.DigitalInOut, address: int = _MCP23S08_ADDRESS, reset: bool = True, baudrate: int = 100000 ): super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information @@ -59,7 +66,7 @@ def gpio(self): return self._read_u8(_MCP23S08_GPIO) @gpio.setter - def gpio(self, val): + def gpio(self, val: int): self._write_u8(_MCP23S08_GPIO, val) @property @@ -70,7 +77,7 @@ def iodir(self): return self._read_u8(_MCP23S08_IODIR) @iodir.setter - def iodir(self, val): + def iodir(self, val: int): self._write_u8(_MCP23S08_IODIR, val) @property @@ -82,10 +89,10 @@ def gppu(self): return self._read_u8(_MCP23S08_GPPU) @gppu.setter - def gppu(self, val): + def gppu(self, val: int): self._write_u8(_MCP23S08_GPPU, val) - 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 MCP23S08 device. """ diff --git a/adafruit_mcp230xx/mcp23s17.py b/adafruit_mcp230xx/mcp23s17.py index 0d77b9b..abe27b5 100644 --- a/adafruit_mcp230xx/mcp23s17.py +++ b/adafruit_mcp230xx/mcp23s17.py @@ -17,6 +17,13 @@ from .mcp23sxx import MCP23SXX from .digital_inout import DigitalInOut +try: + import typing # pylint: disable=unused-import + from busio import SPI + import digitalio +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" @@ -46,7 +53,7 @@ class MCP23S17(MCP23SXX): """ def __init__( - self, spi, chip_select, address=_MCP23S17_ADDRESS, reset=True, baudrate=100000 + self, spi: SPI, chip_select: digitalio.DigitalInOut, address: int = _MCP23S17_ADDRESS, reset: bool = True, baudrate: int = 100000 ): super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information @@ -67,7 +74,7 @@ def gpio(self): return self._read_u16le(_MCP23S17_GPIOA) @gpio.setter - def gpio(self, val): + def gpio(self, val: int) : self._write_u16le(_MCP23S17_GPIOA, val) @property @@ -79,7 +86,7 @@ def gpioa(self): return self._read_u8(_MCP23S17_GPIOA) @gpioa.setter - def gpioa(self, val): + def gpioa(self, val: int): self._write_u8(_MCP23S17_GPIOA, val) @property @@ -91,7 +98,7 @@ def gpiob(self): return self._read_u8(_MCP23S17_GPIOB) @gpiob.setter - def gpiob(self, val): + def gpiob(self, val: int): self._write_u8(_MCP23S17_GPIOB, val) @property @@ -102,7 +109,7 @@ def iodir(self): return self._read_u16le(_MCP23S17_IODIRA) @iodir.setter - def iodir(self, val): + def iodir(self, val: int): self._write_u16le(_MCP23S17_IODIRA, val) @property @@ -113,7 +120,7 @@ def iodira(self): return self._read_u8(_MCP23S17_IODIRA) @iodira.setter - def iodira(self, val): + def iodira(self, val: int): self._write_u8(_MCP23S17_IODIRA, val) @property @@ -124,7 +131,7 @@ def iodirb(self): return self._read_u8(_MCP23S17_IODIRB) @iodirb.setter - def iodirb(self, val): + def iodirb(self, val: int): self._write_u8(_MCP23S17_IODIRB, val) @property @@ -136,7 +143,7 @@ def gppu(self): return self._read_u16le(_MCP23S17_GPPUA) @gppu.setter - def gppu(self, val): + def gppu(self, val: int): self._write_u16le(_MCP23S17_GPPUA, val) @property @@ -148,7 +155,7 @@ def gppua(self): return self._read_u8(_MCP23S17_GPPUA) @gppua.setter - def gppua(self, val): + def gppua(self, val: int): self._write_u8(_MCP23S17_GPPUA, val) @property @@ -160,10 +167,10 @@ def gppub(self): return self._read_u8(_MCP23S17_GPPUB) @gppub.setter - def gppub(self, val): + def gppub(self, val: int): self._write_u8(_MCP23S17_GPPUB, val) - 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 MCP23S17 device. """ @@ -180,7 +187,7 @@ def ipol(self): return self._read_u16le(_MCP23S17_IPOLA) @ipol.setter - def ipol(self, val): + def ipol(self, val: int): self._write_u16le(_MCP23S17_IPOLA, val) @property @@ -192,7 +199,7 @@ def ipola(self): return self._read_u8(_MCP23S17_IPOLA) @ipola.setter - def ipola(self, val): + def ipola(self, val: int): self._write_u8(_MCP23S17_IPOLA, val) @property @@ -204,7 +211,7 @@ def ipolb(self): return self._read_u8(_MCP23S17_IPOLB) @ipolb.setter - def ipolb(self, val): + def ipolb(self, val: int): self._write_u8(_MCP23S17_IPOLB, val) @property @@ -219,7 +226,7 @@ def interrupt_configuration(self): return self._read_u16le(_MCP23S17_INTCONA) @interrupt_configuration.setter - def interrupt_configuration(self, val): + def interrupt_configuration(self, val: int): self._write_u16le(_MCP23S17_INTCONA, val) @property @@ -233,7 +240,7 @@ def interrupt_enable(self): return self._read_u16le(_MCP23S17_GPINTENA) @interrupt_enable.setter - def interrupt_enable(self, val): + def interrupt_enable(self, val: int): self._write_u16le(_MCP23S17_GPINTENA, val) @property @@ -246,7 +253,7 @@ def default_value(self): return self._read_u16le(_MCP23S17_DEFVALA) @default_value.setter - def default_value(self, val): + def default_value(self, val: int): self._write_u16le(_MCP23S17_DEFVALA, val) @property @@ -262,7 +269,7 @@ def io_control(self): return self._read_u8(_MCP23S17_IOCON) @io_control.setter - def io_control(self, val): + def io_control(self, val: int): val &= ~0x80 self._write_u8(_MCP23S17_IOCON, val) diff --git a/adafruit_mcp230xx/mcp23sxx.py b/adafruit_mcp230xx/mcp23sxx.py index 34a43a6..c3e38ad 100644 --- a/adafruit_mcp230xx/mcp23sxx.py +++ b/adafruit_mcp230xx/mcp23sxx.py @@ -15,6 +15,13 @@ from .mcp23xxx import MCP23XXX +try: + import typing # pylint: disable=unused-import + from busio import SPI + import digitalio +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" @@ -30,12 +37,12 @@ class MCP23SXX(MCP23XXX): """Base class for MCP23Sxx devices.""" - def __init__(self, spi, address, chip_select, baudrate=100000): + def __init__(self, spi: SPI, address: int, chip_select: digitalio.DigitalInOut, baudrate: int = 100000): self.cmd_write = MCP23SXX_CODE_WRITE | (address << 1) self.cmd_read = MCP23SXX_CODE_READ | (address << 1) super().__init__(spi, address, chip_select, baudrate=baudrate) - def _read_u16le(self, register): + def _read_u16le(self, register: int) -> int: # Read an unsigned 16 bit little endian value from the specified 8-bit # register. _OUT_BUFFER[0] = self.cmd_read @@ -44,7 +51,7 @@ def _read_u16le(self, register): bus_device.write_readinto(_OUT_BUFFER, _IN_BUFFER) return (_IN_BUFFER[3] << 8) | _IN_BUFFER[2] - def _write_u16le(self, register, value): + def _write_u16le(self, register: int, value: int): # Write an unsigned 16 bit little endian value to the specified 8-bit # register. _OUT_BUFFER[0] = self.cmd_write @@ -54,7 +61,7 @@ def _write_u16le(self, register, value): with self._device as bus_device: bus_device.write(_OUT_BUFFER) - def _read_u8(self, register): + def _read_u8(self, register: int) -> int: # Read an unsigned 8 bit value from the specified 8-bit register. _OUT_BUFFER[0] = self.cmd_read _OUT_BUFFER[1] = register & 0xFF @@ -62,7 +69,7 @@ def _read_u8(self, register): bus_device.write_readinto(_OUT_BUFFER, _IN_BUFFER) return _IN_BUFFER[2] - def _write_u8(self, register, value): + def _write_u8(self, register: int, value: int): # Write an 8 bit value to the specified 8-bit register. _OUT_BUFFER[0] = self.cmd_write _OUT_BUFFER[1] = register & 0xFF diff --git a/adafruit_mcp230xx/mcp23xxx.py b/adafruit_mcp230xx/mcp23xxx.py index d92aa7e..9be7d62 100644 --- a/adafruit_mcp230xx/mcp23xxx.py +++ b/adafruit_mcp230xx/mcp23xxx.py @@ -13,6 +13,13 @@ from adafruit_bus_device import i2c_device, spi_device +try: + from typing import Union, Optional + from busio import I2C, SPI + import digitalio +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" @@ -20,7 +27,7 @@ class MCP23XXX: """Base class for MCP23xxx devices.""" - def __init__(self, bus_device, address, chip_select=None, baudrate=100000): + def __init__(self, bus_device: Union[I2C, SPI], address: int, chip_select: Optional[digitalio.DigitalInOut] = None, baudrate: int = 100000): if chip_select is None: self._device = i2c_device.I2CDevice(bus_device, address) else: From fcde2779a670f38d85f759fd242e73dc0db707fc Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 8 Nov 2021 17:32:26 -0500 Subject: [PATCH 2/4] Reformatting per pre-commit --- adafruit_mcp230xx/digital_inout.py | 6 ++++-- adafruit_mcp230xx/mcp23008.py | 2 +- adafruit_mcp230xx/mcp23016.py | 2 +- adafruit_mcp230xx/mcp23017.py | 2 +- adafruit_mcp230xx/mcp23s08.py | 9 +++++++-- adafruit_mcp230xx/mcp23s17.py | 11 ++++++++--- adafruit_mcp230xx/mcp23sxx.py | 10 ++++++++-- adafruit_mcp230xx/mcp23xxx.py | 8 +++++++- 8 files changed, 37 insertions(+), 13 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 1428d3c..48182e4 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -15,7 +15,7 @@ import digitalio try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from adafruit_mcp230xx.mcp230xx import MCP230XX from digitalio import Pull, Direction except ImportError: @@ -67,7 +67,9 @@ def switch_to_output(self, value: bool = False, **kwargs): self.direction = digitalio.Direction.OUTPUT self.value = value - def switch_to_input(self, pull: Pull = None, invert_polarity: bool = False, **kwargs): + def switch_to_input( + self, pull: Pull = None, invert_polarity: bool = False, **kwargs + ): """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! diff --git a/adafruit_mcp230xx/mcp23008.py b/adafruit_mcp230xx/mcp23008.py index a4d472a..86a7ab0 100644 --- a/adafruit_mcp230xx/mcp23008.py +++ b/adafruit_mcp230xx/mcp23008.py @@ -17,7 +17,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from busio import I2C except ImportError: pass diff --git a/adafruit_mcp230xx/mcp23016.py b/adafruit_mcp230xx/mcp23016.py index 347ca90..05638d1 100644 --- a/adafruit_mcp230xx/mcp23016.py +++ b/adafruit_mcp230xx/mcp23016.py @@ -25,7 +25,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from busio import I2C except ImportError: pass diff --git a/adafruit_mcp230xx/mcp23017.py b/adafruit_mcp230xx/mcp23017.py index 1ef5364..709abf7 100644 --- a/adafruit_mcp230xx/mcp23017.py +++ b/adafruit_mcp230xx/mcp23017.py @@ -19,7 +19,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from busio import I2C except ImportError: pass diff --git a/adafruit_mcp230xx/mcp23s08.py b/adafruit_mcp230xx/mcp23s08.py index ef67fe7..764d262 100644 --- a/adafruit_mcp230xx/mcp23s08.py +++ b/adafruit_mcp230xx/mcp23s08.py @@ -18,7 +18,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from busio import SPI import digitalio except ImportError: @@ -46,7 +46,12 @@ class MCP23S08(MCP23SXX): """ def __init__( - self, spi: SPI, chip_select: digitalio.DigitalInOut, address: int = _MCP23S08_ADDRESS, reset: bool = True, baudrate: int = 100000 + self, + spi: SPI, + chip_select: digitalio.DigitalInOut, + address: int = _MCP23S08_ADDRESS, + reset: bool = True, + baudrate: int = 100000, ): super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information diff --git a/adafruit_mcp230xx/mcp23s17.py b/adafruit_mcp230xx/mcp23s17.py index abe27b5..6e3c2ac 100644 --- a/adafruit_mcp230xx/mcp23s17.py +++ b/adafruit_mcp230xx/mcp23s17.py @@ -18,7 +18,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from busio import SPI import digitalio except ImportError: @@ -53,7 +53,12 @@ class MCP23S17(MCP23SXX): """ def __init__( - self, spi: SPI, chip_select: digitalio.DigitalInOut, address: int = _MCP23S17_ADDRESS, reset: bool = True, baudrate: int = 100000 + self, + spi: SPI, + chip_select: digitalio.DigitalInOut, + address: int = _MCP23S17_ADDRESS, + reset: bool = True, + baudrate: int = 100000, ): super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information @@ -74,7 +79,7 @@ def gpio(self): return self._read_u16le(_MCP23S17_GPIOA) @gpio.setter - def gpio(self, val: int) : + def gpio(self, val: int): self._write_u16le(_MCP23S17_GPIOA, val) @property diff --git a/adafruit_mcp230xx/mcp23sxx.py b/adafruit_mcp230xx/mcp23sxx.py index c3e38ad..43fca36 100644 --- a/adafruit_mcp230xx/mcp23sxx.py +++ b/adafruit_mcp230xx/mcp23sxx.py @@ -16,7 +16,7 @@ from .mcp23xxx import MCP23XXX try: - import typing # pylint: disable=unused-import + import typing # pylint: disable=unused-import from busio import SPI import digitalio except ImportError: @@ -37,7 +37,13 @@ class MCP23SXX(MCP23XXX): """Base class for MCP23Sxx devices.""" - def __init__(self, spi: SPI, address: int, chip_select: digitalio.DigitalInOut, baudrate: int = 100000): + def __init__( + self, + spi: SPI, + address: int, + chip_select: digitalio.DigitalInOut, + baudrate: int = 100000, + ): self.cmd_write = MCP23SXX_CODE_WRITE | (address << 1) self.cmd_read = MCP23SXX_CODE_READ | (address << 1) super().__init__(spi, address, chip_select, baudrate=baudrate) diff --git a/adafruit_mcp230xx/mcp23xxx.py b/adafruit_mcp230xx/mcp23xxx.py index 9be7d62..e63962c 100644 --- a/adafruit_mcp230xx/mcp23xxx.py +++ b/adafruit_mcp230xx/mcp23xxx.py @@ -27,7 +27,13 @@ class MCP23XXX: """Base class for MCP23xxx devices.""" - def __init__(self, bus_device: Union[I2C, SPI], address: int, chip_select: Optional[digitalio.DigitalInOut] = None, baudrate: int = 100000): + def __init__( + self, + bus_device: Union[I2C, SPI], + address: int, + chip_select: Optional[digitalio.DigitalInOut] = None, + baudrate: int = 100000, + ): if chip_select is None: self._device = i2c_device.I2CDevice(bus_device, address) else: From b62cfded38fd069fc01204b2c5e0b04fb7cee605 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 8 Nov 2021 17:38:07 -0500 Subject: [PATCH 3/4] Fix to use correct base class --- adafruit_mcp230xx/digital_inout.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 48182e4..c31aa87 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -16,7 +16,7 @@ try: import typing # pylint: disable=unused-import - from adafruit_mcp230xx.mcp230xx import MCP230XX + from adafruit_mcp230xx.mcp23xxx import MCP23XXX from digitalio import Pull, Direction except ImportError: pass @@ -48,7 +48,7 @@ class DigitalInOut: configurations. """ - def __init__(self, pin_number: int, mcp230xx: MCP230XX): + def __init__(self, pin_number: int, mcp230xx: MCP23XXX): """Specify the pin number of the MCP230xx (0...7 for MCP23008, or 0...15 for MCP23017) and MCP23008 instance. """ From e5b89be0cac5bd01e056b5ac94469a19849b5e2c Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:49:37 -0500 Subject: [PATCH 4/4] Added remaining typehints --- adafruit_mcp230xx/digital_inout.py | 24 ++++---- adafruit_mcp230xx/mcp23008.py | 16 +++--- adafruit_mcp230xx/mcp23016.py | 32 ++++++----- adafruit_mcp230xx/mcp23017.py | 88 +++++++++++++++--------------- adafruit_mcp230xx/mcp230xx.py | 6 +- adafruit_mcp230xx/mcp23s08.py | 14 ++--- adafruit_mcp230xx/mcp23s17.py | 80 +++++++++++++-------------- adafruit_mcp230xx/mcp23sxx.py | 6 +- adafruit_mcp230xx/mcp23xxx.py | 2 +- 9 files changed, 137 insertions(+), 131 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index c31aa87..ed1a0d4 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -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: @@ -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. """ @@ -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). """ @@ -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! @@ -81,7 +81,7 @@ 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. @@ -89,14 +89,14 @@ def value(self): 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. """ @@ -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: @@ -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! @@ -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) @@ -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. """ @@ -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: diff --git a/adafruit_mcp230xx/mcp23008.py b/adafruit_mcp230xx/mcp23008.py index 86a7ab0..69f0677 100644 --- a/adafruit_mcp230xx/mcp23008.py +++ b/adafruit_mcp230xx/mcp23008.py @@ -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: @@ -53,7 +55,7 @@ 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. @@ -61,22 +63,22 @@ def gpio(self): 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! @@ -84,7 +86,7 @@ def gppu(self): 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: diff --git a/adafruit_mcp230xx/mcp23016.py b/adafruit_mcp230xx/mcp23016.py index 05638d1..d7d095c 100644 --- a/adafruit_mcp230xx/mcp23016.py +++ b/adafruit_mcp230xx/mcp23016.py @@ -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: @@ -60,7 +62,7 @@ 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. @@ -68,11 +70,11 @@ def gpio(self): 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. @@ -80,11 +82,11 @@ def gpioa(self): 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. @@ -92,40 +94,40 @@ def gpiob(self): 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: @@ -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) diff --git a/adafruit_mcp230xx/mcp23017.py b/adafruit_mcp230xx/mcp23017.py index 709abf7..39720bd 100644 --- a/adafruit_mcp230xx/mcp23017.py +++ b/adafruit_mcp230xx/mcp23017.py @@ -19,7 +19,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + from typing import List from busio import I2C except ImportError: pass @@ -51,7 +51,9 @@ class MCP23017(MCP230XX): at the specified I2C address. """ - def __init__(self, i2c: I2C, address: int = _MCP23017_ADDRESS, reset: bool = True): + def __init__( + self, i2c: I2C, address: int = _MCP23017_ADDRESS, reset: bool = True + ) -> None: super().__init__(i2c, address) if reset: # Reset to all inputs with no pull-ups and no inverted polarity. @@ -61,7 +63,7 @@ def __init__(self, i2c: I2C, address: int = _MCP23017_ADDRESS, reset: bool = Tru self._write_u16le(_MCP23017_IPOLA, 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. @@ -69,11 +71,11 @@ def gpio(self): return self._read_u16le(_MCP23017_GPIOA) @gpio.setter - def gpio(self, val: int): + def gpio(self, val: int) -> None: self._write_u16le(_MCP23017_GPIOA, val) @property - def gpioa(self): + def gpioa(self) -> int: """The raw GPIO A 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. @@ -81,11 +83,11 @@ def gpioa(self): return self._read_u8(_MCP23017_GPIOA) @gpioa.setter - def gpioa(self, val: int): + def gpioa(self, val: int) -> None: self._write_u8(_MCP23017_GPIOA, val) @property - def gpiob(self): + def gpiob(self) -> int: """The raw GPIO B 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. @@ -93,44 +95,44 @@ def gpiob(self): return self._read_u8(_MCP23017_GPIOB) @gpiob.setter - def gpiob(self, val: int): + def gpiob(self, val: int) -> None: self._write_u8(_MCP23017_GPIOB, 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(_MCP23017_IODIRA) @iodir.setter - def iodir(self, val: int): + def iodir(self, val: int) -> None: self._write_u16le(_MCP23017_IODIRA, val) @property - def iodira(self): + def iodira(self) -> int: """The raw IODIR A direction register. Each bit represents direction of a pin, either 1 for an input or 0 for an output mode. """ return self._read_u8(_MCP23017_IODIRA) @iodira.setter - def iodira(self, val: int): + def iodira(self, val: int) -> None: self._write_u8(_MCP23017_IODIRA, val) @property - def iodirb(self): + def iodirb(self) -> int: """The raw IODIR B direction register. Each bit represents direction of a pin, either 1 for an input or 0 for an output mode. """ return self._read_u8(_MCP23017_IODIRB) @iodirb.setter - def iodirb(self, val: int): + def iodirb(self, val: int) -> None: self._write_u8(_MCP23017_IODIRB, 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! @@ -138,11 +140,11 @@ def gppu(self): return self._read_u16le(_MCP23017_GPPUA) @gppu.setter - def gppu(self, val: int): + def gppu(self, val: int) -> None: self._write_u16le(_MCP23017_GPPUA, val) @property - def gppua(self): + def gppua(self) -> int: """The raw GPPU A 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! @@ -150,11 +152,11 @@ def gppua(self): return self._read_u8(_MCP23017_GPPUA) @gppua.setter - def gppua(self, val: int): + def gppua(self, val: int) -> None: self._write_u8(_MCP23017_GPPUA, val) @property - def gppub(self): + def gppub(self) -> int: """The raw GPPU B 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! @@ -162,7 +164,7 @@ def gppub(self): return self._read_u8(_MCP23017_GPPUB) @gppub.setter - def gppub(self, val: int): + def gppub(self, val: int) -> None: self._write_u8(_MCP23017_GPPUB, val) def get_pin(self, pin: int) -> DigitalInOut: @@ -174,7 +176,7 @@ def get_pin(self, pin: int) -> DigitalInOut: return DigitalInOut(pin, self) @property - def ipol(self): + def ipol(self) -> int: """The raw IPOL output register. Each bit represents the polarity value of the associated pin (0 = normal, 1 = inverted), assuming that pin has been configured as an input previously. @@ -182,11 +184,11 @@ def ipol(self): return self._read_u16le(_MCP23017_IPOLA) @ipol.setter - def ipol(self, val: int): + def ipol(self, val: int) -> None: self._write_u16le(_MCP23017_IPOLA, val) @property - def ipola(self): + def ipola(self) -> int: """The raw IPOL A output register. Each bit represents the polarity value of the associated pin (0 = normal, 1 = inverted), assuming that pin has been configured as an input previously. @@ -194,11 +196,11 @@ def ipola(self): return self._read_u8(_MCP23017_IPOLA) @ipola.setter - def ipola(self, val: int): + def ipola(self, val: int) -> None: self._write_u8(_MCP23017_IPOLA, val) @property - def ipolb(self): + def ipolb(self) -> int: """The raw IPOL B output register. Each bit represents the polarity value of the associated pin (0 = normal, 1 = inverted), assuming that pin has been configured as an input previously. @@ -206,11 +208,11 @@ def ipolb(self): return self._read_u8(_MCP23017_IPOLB) @ipolb.setter - def ipolb(self, val: int): + def ipolb(self, val: int) -> None: self._write_u8(_MCP23017_IPOLB, val) @property - def interrupt_configuration(self): + def interrupt_configuration(self) -> int: """The raw INTCON interrupt control register. The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature. If a bit is set, the corresponding @@ -221,11 +223,11 @@ def interrupt_configuration(self): return self._read_u16le(_MCP23017_INTCONA) @interrupt_configuration.setter - def interrupt_configuration(self, val: int): + def interrupt_configuration(self, val: int) -> None: self._write_u16le(_MCP23017_INTCONA, val) @property - def interrupt_enable(self): + def interrupt_enable(self) -> int: """The raw GPINTEN interrupt control register. The GPINTEN register controls the interrupt-on-change feature for each pin. If a bit is set, the corresponding pin is enabled for interrupt-on-change. @@ -235,11 +237,11 @@ def interrupt_enable(self): return self._read_u16le(_MCP23017_GPINTENA) @interrupt_enable.setter - def interrupt_enable(self, val: int): + def interrupt_enable(self, val: int) -> None: self._write_u16le(_MCP23017_GPINTENA, val) @property - def default_value(self): + def default_value(self) -> int: """The raw DEFVAL interrupt control register. The default comparison value is configured in the DEFVAL register. If enabled (via GPINTEN and INTCON) to compare against the DEFVAL register, an opposite value @@ -248,11 +250,11 @@ def default_value(self): return self._read_u16le(_MCP23017_DEFVALA) @default_value.setter - def default_value(self, val: int): + def default_value(self, val: int) -> None: self._write_u16le(_MCP23017_DEFVALA, val) @property - def io_control(self): + def io_control(self) -> int: """The raw IOCON configuration register. Bit 1 controls interrupt polarity (1 = active-high, 0 = active-low). Bit 2 is whether irq pin is open drain (1 = open drain, 0 = push-pull). Bit 3 is unused. @@ -264,12 +266,12 @@ def io_control(self): return self._read_u8(_MCP23017_IOCON) @io_control.setter - def io_control(self, val: int): + def io_control(self, val: int) -> None: val &= ~0x80 self._write_u8(_MCP23017_IOCON, val) @property - def int_flag(self): + def int_flag(self) -> List[int]: """Returns a list with the pin numbers that caused an interrupt port A ----> pins 0-7 port B ----> pins 8-15 @@ -279,7 +281,7 @@ def int_flag(self): return flags @property - def int_flaga(self): + def int_flaga(self) -> List[int]: """Returns a list of pin numbers that caused an interrupt in port A pins: 0-7 """ @@ -288,7 +290,7 @@ def int_flaga(self): return flags @property - def int_flagb(self): + def int_flagb(self) -> List[int]: """Returns a list of pin numbers that caused an interrupt in port B pins: 8-15 """ @@ -297,7 +299,7 @@ def int_flagb(self): return flags @property - def int_cap(self): + def int_cap(self) -> List[int]: """Returns a list with the pin values at time of interrupt port A ----> pins 0-7 port B ----> pins 8-15 @@ -306,7 +308,7 @@ def int_cap(self): return [(intcap >> pin) & 1 for pin in range(16)] @property - def int_capa(self): + def int_capa(self) -> List[int]: """Returns a list of pin values at time of interrupt pins: 0-7 """ @@ -314,21 +316,21 @@ def int_capa(self): return [(intcapa >> pin) & 1 for pin in range(8)] @property - def int_capb(self): + def int_capb(self) -> List[int]: """Returns a list of pin values at time of interrupt pins: 8-15 """ intcapb = self._read_u8(_MCP23017_INTCAPB) return [(intcapb >> pin) & 1 for pin in range(8)] - def clear_ints(self): + def clear_ints(self) -> None: """Clears interrupts by reading INTCAP.""" self._read_u16le(_MCP23017_INTCAPA) - def clear_inta(self): + def clear_inta(self) -> None: """Clears port A interrupts.""" self._read_u8(_MCP23017_INTCAPA) - def clear_intb(self): + def clear_intb(self) -> None: """Clears port B interrupts.""" self._read_u8(_MCP23017_INTCAPB) diff --git a/adafruit_mcp230xx/mcp230xx.py b/adafruit_mcp230xx/mcp230xx.py index 8dc2a9e..7667e71 100644 --- a/adafruit_mcp230xx/mcp230xx.py +++ b/adafruit_mcp230xx/mcp230xx.py @@ -39,7 +39,7 @@ def _read_u16le(self, register: int) -> int: ) return (_BUFFER[2] << 8) | _BUFFER[1] - def _write_u16le(self, register: int, val: int): + def _write_u16le(self, register: int, val: int) -> None: # Write an unsigned 16 bit little endian value to the specified 8-bit # register. with self._device as bus_device: @@ -48,7 +48,7 @@ def _write_u16le(self, register: int, val: int): _BUFFER[2] = (val >> 8) & 0xFF bus_device.write(_BUFFER, end=3) - def _read_u8(self, register: int): + def _read_u8(self, register: int) -> int: # Read an unsigned 8 bit value from the specified 8-bit register. with self._device as bus_device: _BUFFER[0] = register & 0xFF @@ -58,7 +58,7 @@ def _read_u8(self, register: int): ) return _BUFFER[1] - def _write_u8(self, register: int, val: int): + def _write_u8(self, register: int, val: int) -> None: # Write an 8 bit value to the specified 8-bit register. with self._device as bus_device: _BUFFER[0] = register & 0xFF diff --git a/adafruit_mcp230xx/mcp23s08.py b/adafruit_mcp230xx/mcp23s08.py index 764d262..74fa26e 100644 --- a/adafruit_mcp230xx/mcp23s08.py +++ b/adafruit_mcp230xx/mcp23s08.py @@ -52,7 +52,7 @@ def __init__( address: int = _MCP23S08_ADDRESS, reset: bool = True, baudrate: int = 100000, - ): + ) -> None: super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information self.address = address @@ -63,7 +63,7 @@ def __init__( self._write_u8(_MCP23S08_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. @@ -71,22 +71,22 @@ def gpio(self): return self._read_u8(_MCP23S08_GPIO) @gpio.setter - def gpio(self, val: int): + def gpio(self, val: int) -> None: self._write_u8(_MCP23S08_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(_MCP23S08_IODIR) @iodir.setter - def iodir(self, val: int): + def iodir(self, val: int) -> None: self._write_u8(_MCP23S08_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! @@ -94,7 +94,7 @@ def gppu(self): return self._read_u8(_MCP23S08_GPPU) @gppu.setter - def gppu(self, val: int): + def gppu(self, val: int) -> None: self._write_u8(_MCP23S08_GPPU, val) def get_pin(self, pin: int) -> DigitalInOut: diff --git a/adafruit_mcp230xx/mcp23s17.py b/adafruit_mcp230xx/mcp23s17.py index 6e3c2ac..223672e 100644 --- a/adafruit_mcp230xx/mcp23s17.py +++ b/adafruit_mcp230xx/mcp23s17.py @@ -18,7 +18,7 @@ from .digital_inout import DigitalInOut try: - import typing # pylint: disable=unused-import + from typing import List from busio import SPI import digitalio except ImportError: @@ -59,7 +59,7 @@ def __init__( address: int = _MCP23S17_ADDRESS, reset: bool = True, baudrate: int = 100000, - ): + ) -> None: super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information self.address = address @@ -71,7 +71,7 @@ def __init__( self._write_u16le(_MCP23S17_IPOLA, 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. @@ -79,11 +79,11 @@ def gpio(self): return self._read_u16le(_MCP23S17_GPIOA) @gpio.setter - def gpio(self, val: int): + def gpio(self, val: int) -> None: self._write_u16le(_MCP23S17_GPIOA, val) @property - def gpioa(self): + def gpioa(self) -> int: """The raw GPIO A 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. @@ -91,11 +91,11 @@ def gpioa(self): return self._read_u8(_MCP23S17_GPIOA) @gpioa.setter - def gpioa(self, val: int): + def gpioa(self, val: int) -> None: self._write_u8(_MCP23S17_GPIOA, val) @property - def gpiob(self): + def gpiob(self) -> int: """The raw GPIO B 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. @@ -103,44 +103,44 @@ def gpiob(self): return self._read_u8(_MCP23S17_GPIOB) @gpiob.setter - def gpiob(self, val: int): + def gpiob(self, val: int) -> None: self._write_u8(_MCP23S17_GPIOB, 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(_MCP23S17_IODIRA) @iodir.setter - def iodir(self, val: int): + def iodir(self, val: int) -> None: self._write_u16le(_MCP23S17_IODIRA, val) @property - def iodira(self): + def iodira(self) -> int: """The raw IODIR A direction register. Each bit represents direction of a pin, either 1 for an input or 0 for an output mode. """ return self._read_u8(_MCP23S17_IODIRA) @iodira.setter - def iodira(self, val: int): + def iodira(self, val: int) -> None: self._write_u8(_MCP23S17_IODIRA, val) @property - def iodirb(self): + def iodirb(self) -> int: """The raw IODIR B direction register. Each bit represents direction of a pin, either 1 for an input or 0 for an output mode. """ return self._read_u8(_MCP23S17_IODIRB) @iodirb.setter - def iodirb(self, val: int): + def iodirb(self, val: int) -> None: self._write_u8(_MCP23S17_IODIRB, 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! @@ -148,11 +148,11 @@ def gppu(self): return self._read_u16le(_MCP23S17_GPPUA) @gppu.setter - def gppu(self, val: int): + def gppu(self, val: int) -> None: self._write_u16le(_MCP23S17_GPPUA, val) @property - def gppua(self): + def gppua(self) -> int: """The raw GPPU A 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! @@ -160,11 +160,11 @@ def gppua(self): return self._read_u8(_MCP23S17_GPPUA) @gppua.setter - def gppua(self, val: int): + def gppua(self, val: int) -> None: self._write_u8(_MCP23S17_GPPUA, val) @property - def gppub(self): + def gppub(self) -> int: """The raw GPPU B 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! @@ -172,7 +172,7 @@ def gppub(self): return self._read_u8(_MCP23S17_GPPUB) @gppub.setter - def gppub(self, val: int): + def gppub(self, val: int) -> None: self._write_u8(_MCP23S17_GPPUB, val) def get_pin(self, pin: int) -> DigitalInOut: @@ -184,7 +184,7 @@ def get_pin(self, pin: int) -> DigitalInOut: return DigitalInOut(pin, self) @property - def ipol(self): + def ipol(self) -> int: """The raw IPOL output register. Each bit represents the polarity value of the associated pin (0 = normal, 1 = inverted), assuming that pin has been configured as an input previously. @@ -192,11 +192,11 @@ def ipol(self): return self._read_u16le(_MCP23S17_IPOLA) @ipol.setter - def ipol(self, val: int): + def ipol(self, val: int) -> None: self._write_u16le(_MCP23S17_IPOLA, val) @property - def ipola(self): + def ipola(self) -> int: """The raw IPOL A output register. Each bit represents the polarity value of the associated pin (0 = normal, 1 = inverted), assuming that pin has been configured as an input previously. @@ -204,11 +204,11 @@ def ipola(self): return self._read_u8(_MCP23S17_IPOLA) @ipola.setter - def ipola(self, val: int): + def ipola(self, val: int) -> None: self._write_u8(_MCP23S17_IPOLA, val) @property - def ipolb(self): + def ipolb(self) -> int: """The raw IPOL B output register. Each bit represents the polarity value of the associated pin (0 = normal, 1 = inverted), assuming that pin has been configured as an input previously. @@ -216,11 +216,11 @@ def ipolb(self): return self._read_u8(_MCP23S17_IPOLB) @ipolb.setter - def ipolb(self, val: int): + def ipolb(self, val: int) -> None: self._write_u8(_MCP23S17_IPOLB, val) @property - def interrupt_configuration(self): + def interrupt_configuration(self) -> int: """The raw INTCON interrupt control register. The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature. If a bit is set, the corresponding @@ -231,11 +231,11 @@ def interrupt_configuration(self): return self._read_u16le(_MCP23S17_INTCONA) @interrupt_configuration.setter - def interrupt_configuration(self, val: int): + def interrupt_configuration(self, val: int) -> None: self._write_u16le(_MCP23S17_INTCONA, val) @property - def interrupt_enable(self): + def interrupt_enable(self) -> int: """The raw GPINTEN interrupt control register. The GPINTEN register controls the interrupt-on-change feature for each pin. If a bit is set, the corresponding pin is enabled for interrupt-on-change. @@ -245,11 +245,11 @@ def interrupt_enable(self): return self._read_u16le(_MCP23S17_GPINTENA) @interrupt_enable.setter - def interrupt_enable(self, val: int): + def interrupt_enable(self, val: int) -> None: self._write_u16le(_MCP23S17_GPINTENA, val) @property - def default_value(self): + def default_value(self) -> int: """The raw DEFVAL interrupt control register. The default comparison value is configured in the DEFVAL register. If enabled (via GPINTEN and INTCON) to compare against the DEFVAL register, an opposite value @@ -258,11 +258,11 @@ def default_value(self): return self._read_u16le(_MCP23S17_DEFVALA) @default_value.setter - def default_value(self, val: int): + def default_value(self, val: int) -> None: self._write_u16le(_MCP23S17_DEFVALA, val) @property - def io_control(self): + def io_control(self) -> int: """The raw IOCON configuration register. Bit 1 controls interrupt polarity (1 = active-high, 0 = active-low). Bit 2 is whether irq pin is open drain (1 = open drain, 0 = push-pull). Bit 3 is unused. @@ -274,12 +274,12 @@ def io_control(self): return self._read_u8(_MCP23S17_IOCON) @io_control.setter - def io_control(self, val: int): + def io_control(self, val: int) -> None: val &= ~0x80 self._write_u8(_MCP23S17_IOCON, val) @property - def int_flag(self): + def int_flag(self) -> List[int]: """Returns a list with the pin numbers that caused an interrupt port A ----> pins 0-7 port B ----> pins 8-15 @@ -289,7 +289,7 @@ def int_flag(self): return flags @property - def int_flaga(self): + def int_flaga(self) -> List[int]: """Returns a list of pin numbers that caused an interrupt in port A pins: 0-7 """ @@ -298,7 +298,7 @@ def int_flaga(self): return flags @property - def int_flagb(self): + def int_flagb(self) -> List[int]: """Returns a list of pin numbers that caused an interrupt in port B pins: 8-15 """ @@ -306,14 +306,14 @@ def int_flagb(self): flags = [pin + 8 for pin in range(8) if intfb & (1 << pin)] return flags - def clear_ints(self): + def clear_ints(self) -> None: """Clears interrupts by reading INTCAP.""" self._read_u16le(_MCP23S17_INTCAPA) - def clear_inta(self): + def clear_inta(self) -> None: """Clears port A interrupts.""" self._read_u8(_MCP23S17_INTCAPA) - def clear_intb(self): + def clear_intb(self) -> None: """Clears port B interrupts.""" self._read_u8(_MCP23S17_INTCAPB) diff --git a/adafruit_mcp230xx/mcp23sxx.py b/adafruit_mcp230xx/mcp23sxx.py index 43fca36..803d32a 100644 --- a/adafruit_mcp230xx/mcp23sxx.py +++ b/adafruit_mcp230xx/mcp23sxx.py @@ -43,7 +43,7 @@ def __init__( address: int, chip_select: digitalio.DigitalInOut, baudrate: int = 100000, - ): + ) -> None: self.cmd_write = MCP23SXX_CODE_WRITE | (address << 1) self.cmd_read = MCP23SXX_CODE_READ | (address << 1) super().__init__(spi, address, chip_select, baudrate=baudrate) @@ -57,7 +57,7 @@ def _read_u16le(self, register: int) -> int: bus_device.write_readinto(_OUT_BUFFER, _IN_BUFFER) return (_IN_BUFFER[3] << 8) | _IN_BUFFER[2] - def _write_u16le(self, register: int, value: int): + def _write_u16le(self, register: int, value: int) -> None: # Write an unsigned 16 bit little endian value to the specified 8-bit # register. _OUT_BUFFER[0] = self.cmd_write @@ -75,7 +75,7 @@ def _read_u8(self, register: int) -> int: bus_device.write_readinto(_OUT_BUFFER, _IN_BUFFER) return _IN_BUFFER[2] - def _write_u8(self, register: int, value: int): + def _write_u8(self, register: int, value: int) -> None: # Write an 8 bit value to the specified 8-bit register. _OUT_BUFFER[0] = self.cmd_write _OUT_BUFFER[1] = register & 0xFF diff --git a/adafruit_mcp230xx/mcp23xxx.py b/adafruit_mcp230xx/mcp23xxx.py index e63962c..f050d36 100644 --- a/adafruit_mcp230xx/mcp23xxx.py +++ b/adafruit_mcp230xx/mcp23xxx.py @@ -33,7 +33,7 @@ def __init__( address: int, chip_select: Optional[digitalio.DigitalInOut] = None, baudrate: int = 100000, - ): + ) -> None: if chip_select is None: self._device = i2c_device.I2CDevice(bus_device, address) else: