28
28
29
29
try :
30
30
# This is only needed for typing
31
- import busio # pylint: disable=unused-import
31
+ from typing import Optional
32
+ from busio import I2C
32
33
except ImportError :
33
34
pass
34
35
@@ -51,36 +52,34 @@ class PCF8574:
51
52
:param int address: The I2C device address. Default is :const:`0x20`
52
53
"""
53
54
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 :
57
56
self .i2c_device = I2CDevice (i2c_bus , address )
58
57
self ._writebuf = bytearray (1 )
59
58
self ._writebuf [0 ] = 0
60
59
self ._readbuf = bytearray (1 )
61
60
self ._readbuf [0 ] = 0
62
61
63
- def get_pin (self , pin ) :
62
+ def get_pin (self , pin : int ) -> "DigitalInOut" :
64
63
"""Convenience function to create an instance of the DigitalInOut class
65
64
pointing at the specified pin of this PCF8574 device.
66
65
:param int pin: pin to use for digital IO, 0 to 7
67
66
"""
68
67
assert 0 <= pin <= 7
69
68
return DigitalInOut (pin , self )
70
69
71
- def write_gpio (self , val ) :
70
+ def write_gpio (self , val : int ) -> None :
72
71
"""Write a full 8-bit value to the GPIO register"""
73
72
self ._writebuf [0 ] = val & 0xFF
74
73
with self .i2c_device as i2c :
75
74
i2c .write (self ._writebuf )
76
75
77
- def read_gpio (self ):
76
+ def read_gpio (self ) -> int :
78
77
"""Read the full 8-bits of data from the GPIO register"""
79
78
with self .i2c_device as i2c :
80
79
i2c .readinto (self ._readbuf )
81
80
return self ._readbuf [0 ]
82
81
83
- def write_pin (self , pin , val ) :
82
+ def write_pin (self , pin : int , val : bool ) -> None :
84
83
"""Set a single GPIO pin high/pulled-up or driven low"""
85
84
if val :
86
85
# turn on the pullup (write high)
@@ -89,7 +88,7 @@ def write_pin(self, pin, val):
89
88
# turn on the transistor (write low)
90
89
self .write_gpio (self ._writebuf [0 ] & ~ (1 << pin ))
91
90
92
- def read_pin (self , pin ) :
91
+ def read_pin (self , pin : int ) -> bool :
93
92
"""Read a single GPIO pin as high/pulled-up or driven low"""
94
93
return (self .read_gpio () >> pin ) & 0x1
95
94
@@ -114,7 +113,7 @@ class DigitalInOut:
114
113
configurations.
115
114
"""
116
115
117
- def __init__ (self , pin_number , pcf ) :
116
+ def __init__ (self , pin_number : int , pcf : PCF8574 ) -> None :
118
117
"""Specify the pin number of the PCF8574 0..7, and instance."""
119
118
self ._pin = pin_number
120
119
self ._pcf = pcf
@@ -127,14 +126,14 @@ def __init__(self, pin_number, pcf):
127
126
# is unused by this class). Do not remove them, instead turn off pylint
128
127
# in this case.
129
128
# pylint: disable=unused-argument
130
- def switch_to_output (self , value = False , ** kwargs ):
129
+ def switch_to_output (self , value : bool = False , ** kwargs ) -> None :
131
130
"""Switch the pin state to a digital output with the provided starting
132
131
value (True/False for high or low, default is False/low).
133
132
"""
134
133
self .direction = digitalio .Direction .OUTPUT
135
134
self .value = value
136
135
137
- def switch_to_input (self , pull = None , ** kwargs ):
136
+ def switch_to_input (self , pull : Optional [ digitalio . Pull ] = None , ** kwargs ) -> None :
138
137
"""Switch the pin state to a digital input which is the same as
139
138
setting the light pullup on. Note that true tri-state or
140
139
pull-down resistors are NOT supported!
@@ -145,26 +144,26 @@ def switch_to_input(self, pull=None, **kwargs):
145
144
# pylint: enable=unused-argument
146
145
147
146
@property
148
- def value (self ):
147
+ def value (self ) -> bool :
149
148
"""The value of the pin, either True for high/pulled-up or False for
150
149
low.
151
150
"""
152
151
return self ._pcf .read_pin (self ._pin )
153
152
154
153
@value .setter
155
- def value (self , val ) :
154
+ def value (self , val : bool ) -> None :
156
155
self ._pcf .write_pin (self ._pin , val )
157
156
158
157
@property
159
- def direction (self ):
158
+ def direction (self ) -> digitalio . Direction :
160
159
"""
161
160
Setting a pin to OUTPUT drives it low, setting it to
162
161
an INPUT enables the light pullup.
163
162
"""
164
163
return self ._dir
165
164
166
165
@direction .setter
167
- def direction (self , val ) :
166
+ def direction (self , val : digitalio . Direction ) -> None :
168
167
if val == digitalio .Direction .INPUT :
169
168
# for inputs, turn on the pullup (write high)
170
169
self ._pcf .write_pin (self ._pin , True )
@@ -177,14 +176,14 @@ def direction(self, val):
177
176
raise ValueError ("Expected INPUT or OUTPUT direction!" )
178
177
179
178
@property
180
- def pull (self ):
179
+ def pull (self ) -> digitalio . Pull . UP :
181
180
"""
182
181
Pull-up is always activated so always return the same thing
183
182
"""
184
183
return digitalio .Pull .UP
185
184
186
185
@pull .setter
187
- def pull (self , val ) :
186
+ def pull (self , val : digitalio . Pull . UP ) -> None :
188
187
if val is digitalio .Pull .UP :
189
188
# for inputs, turn on the pullup (write high)
190
189
self ._pcf .write_pin (self ._pin , True )
0 commit comments