Skip to content

Commit ab56541

Browse files
authored
Merge pull request #46 from tekktrik/feature/add-typing
Add typing
2 parents 4221e1e + e5b89be commit ab56541

File tree

9 files changed

+224
-141
lines changed

9 files changed

+224
-141
lines changed

adafruit_mcp230xx/digital_inout.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,26 @@
1414

1515
import digitalio
1616

17+
try:
18+
from typing import Optional
19+
from adafruit_mcp230xx.mcp23xxx import MCP23XXX
20+
from digitalio import Pull, Direction
21+
except ImportError:
22+
pass
23+
1724
__version__ = "0.0.0-auto.0"
1825
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
1926

2027
# Internal helpers to simplify setting and getting a bit inside an integer.
21-
def _get_bit(val, bit):
28+
def _get_bit(val, bit: int) -> int:
2229
return val & (1 << bit) > 0
2330

2431

25-
def _enable_bit(val, bit):
32+
def _enable_bit(val, bit: int) -> int:
2633
return val | (1 << bit)
2734

2835

29-
def _clear_bit(val, bit):
36+
def _clear_bit(val, bit: int) -> int:
3037
return val & ~(1 << bit)
3138

3239

@@ -41,7 +48,7 @@ class DigitalInOut:
4148
configurations.
4249
"""
4350

44-
def __init__(self, pin_number, mcp230xx):
51+
def __init__(self, pin_number: int, mcp230xx: MCP23XXX) -> None:
4552
"""Specify the pin number of the MCP230xx (0...7 for MCP23008, or 0...15
4653
for MCP23017) and MCP23008 instance.
4754
"""
@@ -53,14 +60,16 @@ def __init__(self, pin_number, mcp230xx):
5360
# is unused by this class). Do not remove them, instead turn off pylint
5461
# in this case.
5562
# pylint: disable=unused-argument
56-
def switch_to_output(self, value=False, **kwargs):
63+
def switch_to_output(self, value: bool = False, **kwargs) -> None:
5764
"""Switch the pin state to a digital output with the provided starting
5865
value (True/False for high or low, default is False/low).
5966
"""
6067
self.direction = digitalio.Direction.OUTPUT
6168
self.value = value
6269

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

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

8291
@value.setter
83-
def value(self, val):
92+
def value(self, val: bool) -> None:
8493
if val:
8594
self._mcp.gpio = _enable_bit(self._mcp.gpio, self._pin)
8695
else:
8796
self._mcp.gpio = _clear_bit(self._mcp.gpio, self._pin)
8897

8998
@property
90-
def direction(self):
99+
def direction(self) -> bool:
91100
"""The direction of the pin, either True for an input or
92101
False for an output.
93102
"""
@@ -96,7 +105,7 @@ def direction(self):
96105
return digitalio.Direction.OUTPUT
97106

98107
@direction.setter
99-
def direction(self, val):
108+
def direction(self, val: Direction) -> None:
100109
if val == digitalio.Direction.INPUT:
101110
self._mcp.iodir = _enable_bit(self._mcp.iodir, self._pin)
102111
elif val == digitalio.Direction.OUTPUT:
@@ -105,7 +114,7 @@ def direction(self, val):
105114
raise ValueError("Expected INPUT or OUTPUT direction!")
106115

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

121130
@pull.setter
122-
def pull(self, val):
131+
def pull(self, val: Pull) -> None:
123132
try:
124133
if val is None:
125134
self._mcp.gppu = _clear_bit(self._mcp.gppu, self._pin)
@@ -134,7 +143,7 @@ def pull(self, val):
134143
raise ValueError("Pull-up/pull-down resistors not supported.") from error
135144

136145
@property
137-
def invert_polarity(self):
146+
def invert_polarity(self) -> bool:
138147
"""The polarity of the pin, either True for an Inverted or
139148
False for an normal.
140149
"""
@@ -143,7 +152,7 @@ def invert_polarity(self):
143152
return False
144153

145154
@invert_polarity.setter
146-
def invert_polarity(self, val):
155+
def invert_polarity(self, val: bool) -> None:
147156
if val:
148157
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
149158
else:

adafruit_mcp230xx/mcp23008.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
from .mcp230xx import MCP230XX
1717
from .digital_inout import DigitalInOut
1818

19+
try:
20+
import typing # pylint: disable=unused-import
21+
from busio import I2C
22+
except ImportError:
23+
pass
24+
1925
__version__ = "0.0.0-auto.0"
2026
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
2127

@@ -37,7 +43,9 @@ class MCP23008(MCP230XX):
3743
at the specified I2C address.
3844
"""
3945

40-
def __init__(self, i2c, address=_MCP23008_ADDRESS, reset=True):
46+
def __init__(
47+
self, i2c: I2C, address: int = _MCP23008_ADDRESS, reset: bool = True
48+
) -> None:
4149
super().__init__(i2c, address)
4250

4351
if reset:
@@ -47,41 +55,41 @@ def __init__(self, i2c, address=_MCP23008_ADDRESS, reset=True):
4755
self._write_u8(_MCP23008_IPOL, 0x00)
4856

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

5765
@gpio.setter
58-
def gpio(self, val):
66+
def gpio(self, val: int) -> None:
5967
self._write_u8(_MCP23008_GPIO, val)
6068

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

6876
@iodir.setter
69-
def iodir(self, val):
77+
def iodir(self, val: int) -> None:
7078
self._write_u8(_MCP23008_IODIR, val)
7179

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

8088
@gppu.setter
81-
def gppu(self, val):
89+
def gppu(self, val: int) -> None:
8290
self._write_u8(_MCP23008_GPPU, val)
8391

84-
def get_pin(self, pin):
92+
def get_pin(self, pin: int) -> DigitalInOut:
8593
"""Convenience function to create an instance of the DigitalInOut class
8694
pointing at the specified pin of this MCP23008 device.
8795
"""

adafruit_mcp230xx/mcp23016.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
from .mcp230xx import MCP230XX
2525
from .digital_inout import DigitalInOut
2626

27+
try:
28+
import typing # pylint: disable=unused-import
29+
from busio import I2C
30+
except ImportError:
31+
pass
32+
2733
__version__ = "0.0.0-auto.0"
2834
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
2935

@@ -45,7 +51,9 @@ class MCP23016(MCP230XX):
4551
at the specified I2C address.
4652
"""
4753

48-
def __init__(self, i2c, address=_MCP23016_ADDRESS, reset=True):
54+
def __init__(
55+
self, i2c: I2C, address: int = _MCP23016_ADDRESS, reset: bool = True
56+
) -> None:
4957
super().__init__(i2c, address)
5058

5159
if reset:
@@ -54,86 +62,86 @@ def __init__(self, i2c, address=_MCP23016_ADDRESS, reset=True):
5462
self._write_u16le(_MCP23016_IPOL0, 0x0000)
5563

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

6472
@gpio.setter
65-
def gpio(self, val):
73+
def gpio(self, val: int) -> None:
6674
self._write_u16le(_MCP23016_GPIO0, val)
6775

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

7684
@gpioa.setter
77-
def gpioa(self, val):
85+
def gpioa(self, val: int) -> None:
7886
self._write_u8(_MCP23016_GPIO0, val)
7987

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

8896
@gpiob.setter
89-
def gpiob(self, val):
97+
def gpiob(self, val: int) -> None:
9098
self._write_u8(_MCP23016_GPIO1, val)
9199

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

99107
@iodir.setter
100-
def iodir(self, val):
108+
def iodir(self, val: int) -> None:
101109
self._write_u16le(_MCP23016_IODIR0, val)
102110

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

110118
@iodira.setter
111-
def iodira(self, val):
119+
def iodira(self, val: int) -> None:
112120
self._write_u8(_MCP23016_IODIR0, val)
113121

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

121129
@iodirb.setter
122-
def iodirb(self, val):
130+
def iodirb(self, val: int) -> None:
123131
self._write_u8(_MCP23016_IODIR1, val)
124132

125-
def get_pin(self, pin):
133+
def get_pin(self, pin: int) -> DigitalInOut:
126134
"""Convenience function to create an instance of the DigitalInOut class
127135
pointing at the specified pin of this MCP23016 device.
128136
"""
129137
if not 0 <= pin <= 15:
130138
raise ValueError("Pin number must be 0-15.")
131139
return DigitalInOut(pin, self)
132140

133-
def clear_inta(self):
141+
def clear_inta(self) -> None:
134142
"""Clears port 0 interrupts."""
135143
self._read_u8(_MCP23016_INTCAP0)
136144

137-
def clear_intb(self):
145+
def clear_intb(self) -> None:
138146
"""Clears port 1 interrupts."""
139147
self._read_u8(_MCP23016_INTCAP1)

0 commit comments

Comments
 (0)