1
1
# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
2
2
#
3
3
# SPDX-License-Identifier: MIT
4
+
4
5
"""
5
6
`adafruit_pcf8575`
6
7
================================================================================
26
27
"""
27
28
28
29
try :
29
- # This is only needed for typing
30
- import busio # pylint: disable=unused-import
30
+ from typing import Optional
31
+ import busio
31
32
except ImportError :
32
33
pass
33
34
41
42
42
43
PCF8575_I2CADDR_DEFAULT : int = const (0x20 ) # Default I2C address
43
44
45
+
44
46
class PCF8575 :
45
- """
46
- Interface library for PCF8575 GPIO expanders
47
+ """Interface library for PCF8575 GPIO expanders
48
+
47
49
:param ~busio.I2C i2c_bus: The I2C bus the PCF8575 is connected to.
48
50
:param int address: The I2C device address. Default is :const:`0x20`
49
51
"""
@@ -55,29 +57,39 @@ def __init__(
55
57
self ._writebuf = bytearray ([0 , 0 ])
56
58
self ._readbuf = bytearray ([0 , 0 ])
57
59
58
- def get_pin (self , pin ) :
60
+ def get_pin (self , pin : int ) -> "DigitalInOut" :
59
61
"""Convenience function to create an instance of the DigitalInOut class
60
62
pointing at the specified pin of this PCF8575 device.
63
+
61
64
:param int pin: pin to use for digital IO, 0 to 15
62
65
"""
63
66
assert 0 <= pin <= 15
64
67
return DigitalInOut (pin , self )
65
68
66
- def write_gpio (self , val ):
67
- """Write a full 16-bit value to the GPIO register"""
69
+ def write_gpio (self , val : int ) -> None :
70
+ """Write a full 16-bit value to the GPIO register
71
+
72
+ :param int val: The value to write to the register
73
+ """
74
+
68
75
self ._writebuf [0 ] = val & 0xFF
69
76
self ._writebuf [1 ] = (val >> 8 ) & 0xFF
70
77
with self .i2c_device as i2c :
71
78
i2c .write (self ._writebuf )
72
79
73
- def read_gpio (self ):
80
+ def read_gpio (self ) -> int :
74
81
"""Read the full 16-bits of data from the GPIO register"""
75
82
with self .i2c_device as i2c :
76
83
i2c .readinto (self ._readbuf )
77
84
return self ._readbuf [0 ] | (self ._readbuf [1 ] << 8 )
78
85
79
- def write_pin (self , pin , val ):
80
- """Set a single GPIO pin high/pulled-up or driven low"""
86
+ def write_pin (self , pin : int , val : bool ) -> None :
87
+ """Set a single GPIO pin high/pulled-up or driven low
88
+
89
+ :param int pin: The pin number
90
+ :param bool vale: The state to set
91
+ """
92
+
81
93
buff = self ._writebuf [0 ] | (self ._writebuf [1 ] << 8 )
82
94
if val :
83
95
# turn on the pullup (write high)
@@ -86,8 +98,12 @@ def write_pin(self, pin, val):
86
98
# turn on the transistor (write low)
87
99
self .write_gpio (buff & ~ (1 << pin ))
88
100
89
- def read_pin (self , pin ):
90
- """Read a single GPIO pin as high/pulled-up or driven low"""
101
+ def read_pin (self , pin : int ) -> bool :
102
+ """Read a single GPIO pin as high/pulled-up or driven low
103
+
104
+ :param int pin: The pin number
105
+ """
106
+
91
107
return (self .read_gpio () >> pin ) & 0x1
92
108
93
109
@@ -102,15 +118,22 @@ def read_pin(self, pin):
102
118
class DigitalInOut :
103
119
"""Digital input/output of the PCF8575. The interface is exactly the
104
120
same as the digitalio.DigitalInOut class, however:
121
+
105
122
* PCF8575 does not support pull-down resistors
106
123
* PCF8575 does not actually have a sourcing transistor, instead there's
107
- an internal pullup
124
+ an internal pullup
125
+
108
126
Exceptions will be thrown when attempting to set unsupported pull
109
127
configurations.
110
128
"""
111
129
112
- def __init__ (self , pin_number , pcf ):
113
- """Specify the pin number of the PCF8575 0..15, and instance."""
130
+ def __init__ (self , pin_number : int , pcf : PCF8575 ) -> None :
131
+ """Specify the pin number of the PCF8575 0..15, and instance.
132
+
133
+ :param int pin_number: The pin number
134
+ :param PCF8575 pfc: The associated PCF8575 instance
135
+ """
136
+
114
137
self ._pin = pin_number
115
138
self ._pcf = pcf
116
139
self ._dir = (
@@ -122,44 +145,53 @@ def __init__(self, pin_number, pcf):
122
145
# is unused by this class). Do not remove them, instead turn off pylint
123
146
# in this case.
124
147
# pylint: disable=unused-argument
125
- def switch_to_output (self , value = False , ** kwargs ):
148
+ def switch_to_output (self , value : bool = False , ** kwargs ) -> None :
126
149
"""Switch the pin state to a digital output with the provided starting
127
150
value (True/False for high or low, default is False/low).
151
+
152
+ Note that keyword arguments are not parsed, and only included for
153
+ compatibility with code expecting :py:class:`digitalio.DigitalInOut`.
154
+
155
+ :param bool value: The value to set upon switching to output; default
156
+ is ``False``
128
157
"""
129
158
self .direction = digitalio .Direction .OUTPUT
130
159
self .value = value
131
160
132
- def switch_to_input (self , pull = None , ** kwargs ):
161
+ def switch_to_input (self , pull : Optional [ digitalio . Pull ] = None , ** kwargs ) -> None :
133
162
"""Switch the pin state to a digital input which is the same as
134
163
setting the light pullup on. Note that true tri-state or
135
164
pull-down resistors are NOT supported!
165
+
166
+ Note that keyword arguments are not parsed, and only included for
167
+ compatibility with code expecting :py:class:`digitalio.DigitalInOut`.
136
168
"""
169
+
137
170
self .direction = digitalio .Direction .INPUT
138
171
self .pull = pull
139
172
140
173
# pylint: enable=unused-argument
141
174
142
175
@property
143
- def value (self ):
144
- """The value of the pin, either True for high/pulled-up or False for
145
- low.
176
+ def value (self ) -> bool :
177
+ """The value of the pin, either `` True`` for high/pulled-up or
178
+ ``False`` for low.
146
179
"""
147
180
return self ._pcf .read_pin (self ._pin )
148
181
149
182
@value .setter
150
- def value (self , val ) :
183
+ def value (self , val : bool ) -> None :
151
184
self ._pcf .write_pin (self ._pin , val )
152
185
153
186
@property
154
- def direction (self ):
155
- """
156
- Setting a pin to OUTPUT drives it low, setting it to
187
+ def direction (self ) -> digitalio .Direction :
188
+ """Setting a pin to OUTPUT drives it low, setting it to
157
189
an INPUT enables the light pullup.
158
190
"""
159
191
return self ._dir
160
192
161
193
@direction .setter
162
- def direction (self , val ) :
194
+ def direction (self , val : digitalio . Direction ) -> None :
163
195
if val == digitalio .Direction .INPUT :
164
196
# for inputs, turn on the pullup (write high)
165
197
self ._pcf .write_pin (self ._pin , True )
@@ -172,14 +204,15 @@ def direction(self, val):
172
204
raise ValueError ("Expected INPUT or OUTPUT direction!" )
173
205
174
206
@property
175
- def pull (self ):
207
+ def pull (self ) -> digitalio . Pull :
176
208
"""
177
- Pull-up is always activated so always return the same thing
209
+ Pull-up is always activated so this will always return the same,
210
+ thing: :py:attr:`digitalio.Pull.UP`
178
211
"""
179
212
return digitalio .Pull .UP
180
213
181
214
@pull .setter
182
- def pull (self , val ) :
215
+ def pull (self , val : digitalio . Pull ) -> None :
183
216
if val is digitalio .Pull .UP :
184
217
# for inputs, turn on the pullup (write high)
185
218
self ._pcf .write_pin (self ._pin , True )
0 commit comments