diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 25cbe71..ed1a0d4 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -14,19 +14,26 @@ import digitalio +try: + from typing import Optional + from adafruit_mcp230xx.mcp23xxx import MCP23XXX + 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: MCP23XXX) -> None: """Specify the pin number of the MCP230xx (0...7 for MCP23008, or 0...15 for MCP23017) and MCP23008 instance. """ @@ -53,14 +60,16 @@ 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) -> 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, invert_polarity=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! @@ -72,7 +81,7 @@ def switch_to_input(self, pull=None, invert_polarity=False, **kwargs): # 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. @@ -80,14 +89,14 @@ def value(self): return _get_bit(self._mcp.gpio, self._pin) @value.setter - def value(self, val): + 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. """ @@ -96,7 +105,7 @@ def direction(self): return digitalio.Direction.OUTPUT @direction.setter - def direction(self, val): + 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: @@ -105,7 +114,7 @@ def direction(self, val): 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! @@ -119,7 +128,7 @@ def pull(self): return None @pull.setter - def pull(self, val): + def pull(self, val: Pull) -> None: try: if val is None: self._mcp.gppu = _clear_bit(self._mcp.gppu, self._pin) @@ -134,7 +143,7 @@ def pull(self, val): 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. """ @@ -143,7 +152,7 @@ def invert_polarity(self): return False @invert_polarity.setter - def invert_polarity(self, val): + 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 7de2476..69f0677 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,9 @@ 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 + ) -> None: super().__init__(i2c, address) if reset: @@ -47,7 +55,7 @@ def __init__(self, i2c, address=_MCP23008_ADDRESS, reset=True): 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. @@ -55,22 +63,22 @@ def gpio(self): return self._read_u8(_MCP23008_GPIO) @gpio.setter - def gpio(self, val): + 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): + 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! @@ -78,10 +86,10 @@ def gppu(self): return self._read_u8(_MCP23008_GPPU) @gppu.setter - def gppu(self, val): + def gppu(self, val: int) -> None: 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..d7d095c 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,9 @@ 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 + ) -> None: super().__init__(i2c, address) if reset: @@ -54,7 +62,7 @@ def __init__(self, i2c, address=_MCP23016_ADDRESS, reset=True): 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. @@ -62,11 +70,11 @@ def gpio(self): return self._read_u16le(_MCP23016_GPIO0) @gpio.setter - def gpio(self, val): + 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. @@ -74,11 +82,11 @@ def gpioa(self): return self._read_u8(_MCP23016_GPIO0) @gpioa.setter - def gpioa(self, val): + 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. @@ -86,43 +94,43 @@ def gpiob(self): return self._read_u8(_MCP23016_GPIO1) @gpiob.setter - def gpiob(self, val): + 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): + 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): + 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): + def iodirb(self, val: int) -> None: 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. """ @@ -130,10 +138,10 @@ def get_pin(self, pin): 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 4a2fdba..39720bd 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: + from typing import List + 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,9 @@ 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 + ) -> None: super().__init__(i2c, address) if reset: # Reset to all inputs with no pull-ups and no inverted polarity. @@ -55,7 +63,7 @@ def __init__(self, i2c, address=_MCP23017_ADDRESS, reset=True): 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. @@ -63,11 +71,11 @@ def gpio(self): return self._read_u16le(_MCP23017_GPIOA) @gpio.setter - def gpio(self, val): + 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. @@ -75,11 +83,11 @@ def gpioa(self): return self._read_u8(_MCP23017_GPIOA) @gpioa.setter - def gpioa(self, val): + 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. @@ -87,44 +95,44 @@ def gpiob(self): return self._read_u8(_MCP23017_GPIOB) @gpiob.setter - def gpiob(self, val): + 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): + 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): + 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): + 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! @@ -132,11 +140,11 @@ def gppu(self): return self._read_u16le(_MCP23017_GPPUA) @gppu.setter - def gppu(self, val): + 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! @@ -144,11 +152,11 @@ def gppua(self): return self._read_u8(_MCP23017_GPPUA) @gppua.setter - def gppua(self, val): + 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! @@ -156,10 +164,10 @@ def gppub(self): return self._read_u8(_MCP23017_GPPUB) @gppub.setter - def gppub(self, val): + def gppub(self, val: int) -> None: 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. """ @@ -168,7 +176,7 @@ def get_pin(self, pin): 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. @@ -176,11 +184,11 @@ def ipol(self): return self._read_u16le(_MCP23017_IPOLA) @ipol.setter - def ipol(self, val): + 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. @@ -188,11 +196,11 @@ def ipola(self): return self._read_u8(_MCP23017_IPOLA) @ipola.setter - def ipola(self, val): + 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. @@ -200,11 +208,11 @@ def ipolb(self): return self._read_u8(_MCP23017_IPOLB) @ipolb.setter - def ipolb(self, val): + 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 @@ -215,11 +223,11 @@ def interrupt_configuration(self): return self._read_u16le(_MCP23017_INTCONA) @interrupt_configuration.setter - def interrupt_configuration(self, val): + 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. @@ -229,11 +237,11 @@ def interrupt_enable(self): return self._read_u16le(_MCP23017_GPINTENA) @interrupt_enable.setter - def interrupt_enable(self, val): + 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 @@ -242,11 +250,11 @@ def default_value(self): return self._read_u16le(_MCP23017_DEFVALA) @default_value.setter - def default_value(self, val): + 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. @@ -258,12 +266,12 @@ def io_control(self): return self._read_u8(_MCP23017_IOCON) @io_control.setter - def io_control(self, val): + 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 @@ -273,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 """ @@ -282,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 """ @@ -291,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 @@ -300,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 """ @@ -308,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 5993bca..7667e71 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) -> 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, val): _BUFFER[2] = (val >> 8) & 0xFF bus_device.write(_BUFFER, end=3) - def _read_u8(self, register): + 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): ) return _BUFFER[1] - def _write_u8(self, register, val): + 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 06b7472..74fa26e 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,8 +46,13 @@ 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, + ) -> None: super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information self.address = address @@ -51,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. @@ -59,22 +71,22 @@ def gpio(self): return self._read_u8(_MCP23S08_GPIO) @gpio.setter - def gpio(self, val): + 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): + 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! @@ -82,10 +94,10 @@ def gppu(self): return self._read_u8(_MCP23S08_GPPU) @gppu.setter - def gppu(self, val): + def gppu(self, val: int) -> None: 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..223672e 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: + from typing import List + 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,8 +53,13 @@ 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, + ) -> None: super().__init__(spi, address, chip_select, baudrate=baudrate) # For user information self.address = address @@ -59,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. @@ -67,11 +79,11 @@ def gpio(self): return self._read_u16le(_MCP23S17_GPIOA) @gpio.setter - def gpio(self, val): + 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. @@ -79,11 +91,11 @@ def gpioa(self): return self._read_u8(_MCP23S17_GPIOA) @gpioa.setter - def gpioa(self, val): + 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. @@ -91,44 +103,44 @@ def gpiob(self): return self._read_u8(_MCP23S17_GPIOB) @gpiob.setter - def gpiob(self, val): + 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): + 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): + 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): + 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! @@ -136,11 +148,11 @@ def gppu(self): return self._read_u16le(_MCP23S17_GPPUA) @gppu.setter - def gppu(self, val): + 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! @@ -148,11 +160,11 @@ def gppua(self): return self._read_u8(_MCP23S17_GPPUA) @gppua.setter - def gppua(self, val): + 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! @@ -160,10 +172,10 @@ def gppub(self): return self._read_u8(_MCP23S17_GPPUB) @gppub.setter - def gppub(self, val): + def gppub(self, val: int) -> None: 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. """ @@ -172,7 +184,7 @@ def get_pin(self, pin): 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. @@ -180,11 +192,11 @@ def ipol(self): return self._read_u16le(_MCP23S17_IPOLA) @ipol.setter - def ipol(self, val): + 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. @@ -192,11 +204,11 @@ def ipola(self): return self._read_u8(_MCP23S17_IPOLA) @ipola.setter - def ipola(self, val): + 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. @@ -204,11 +216,11 @@ def ipolb(self): return self._read_u8(_MCP23S17_IPOLB) @ipolb.setter - def ipolb(self, val): + 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 @@ -219,11 +231,11 @@ def interrupt_configuration(self): return self._read_u16le(_MCP23S17_INTCONA) @interrupt_configuration.setter - def interrupt_configuration(self, val): + 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. @@ -233,11 +245,11 @@ def interrupt_enable(self): return self._read_u16le(_MCP23S17_GPINTENA) @interrupt_enable.setter - def interrupt_enable(self, val): + 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 @@ -246,11 +258,11 @@ def default_value(self): return self._read_u16le(_MCP23S17_DEFVALA) @default_value.setter - def default_value(self, val): + 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. @@ -262,12 +274,12 @@ def io_control(self): return self._read_u8(_MCP23S17_IOCON) @io_control.setter - def io_control(self, val): + 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 @@ -277,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 """ @@ -286,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 """ @@ -294,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 34a43a6..803d32a 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,18 @@ 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, + ) -> 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) - 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 +57,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) -> None: # Write an unsigned 16 bit little endian value to the specified 8-bit # register. _OUT_BUFFER[0] = self.cmd_write @@ -54,7 +67,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 +75,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) -> 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 d92aa7e..f050d36 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,13 @@ 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, + ) -> None: if chip_select is None: self._device = i2c_device.I2CDevice(bus_device, address) else: