Skip to content

Commit 63093e2

Browse files
authored
Merge pull request #7 from tcfranks/main
Add Missing Type Annotations
2 parents 571b83c + 0626956 commit 63093e2

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

adafruit_pcf8574.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
try:
3030
# This is only needed for typing
31-
import busio # pylint: disable=unused-import
31+
from typing import Optional
32+
from busio import I2C
3233
except ImportError:
3334
pass
3435

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

54-
def __init__(
55-
self, i2c_bus: busio.I2C, address: int = PCF8574_I2CADDR_DEFAULT
56-
) -> None:
55+
def __init__(self, i2c_bus: I2C, address: int = PCF8574_I2CADDR_DEFAULT) -> None:
5756
self.i2c_device = I2CDevice(i2c_bus, address)
5857
self._writebuf = bytearray(1)
5958
self._writebuf[0] = 0
6059
self._readbuf = bytearray(1)
6160
self._readbuf[0] = 0
6261

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

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

77-
def read_gpio(self):
76+
def read_gpio(self) -> int:
7877
"""Read the full 8-bits of data from the GPIO register"""
7978
with self.i2c_device as i2c:
8079
i2c.readinto(self._readbuf)
8180
return self._readbuf[0]
8281

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

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

@@ -114,7 +113,7 @@ class DigitalInOut:
114113
configurations.
115114
"""
116115

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

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

147146
@property
148-
def value(self):
147+
def value(self) -> bool:
149148
"""The value of the pin, either True for high/pulled-up or False for
150149
low.
151150
"""
152151
return self._pcf.read_pin(self._pin)
153152

154153
@value.setter
155-
def value(self, val):
154+
def value(self, val: bool) -> None:
156155
self._pcf.write_pin(self._pin, val)
157156

158157
@property
159-
def direction(self):
158+
def direction(self) -> digitalio.Direction:
160159
"""
161160
Setting a pin to OUTPUT drives it low, setting it to
162161
an INPUT enables the light pullup.
163162
"""
164163
return self._dir
165164

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

179178
@property
180-
def pull(self):
179+
def pull(self) -> digitalio.Pull.UP:
181180
"""
182181
Pull-up is always activated so always return the same thing
183182
"""
184183
return digitalio.Pull.UP
185184

186185
@pull.setter
187-
def pull(self, val):
186+
def pull(self, val: digitalio.Pull.UP) -> None:
188187
if val is digitalio.Pull.UP:
189188
# for inputs, turn on the pullup (write high)
190189
self._pcf.write_pin(self._pin, True)

0 commit comments

Comments
 (0)