Skip to content

Commit f77840d

Browse files
author
Alec Delaney
committed
Reformatted, finished annotating and documenting
1 parent 0fdc528 commit f77840d

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

adafruit_pcf8575.py

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
4+
45
"""
56
`adafruit_pcf8575`
67
================================================================================
@@ -26,8 +27,8 @@
2627
"""
2728

2829
try:
29-
# This is only needed for typing
30-
import busio # pylint: disable=unused-import
30+
from typing import Optional
31+
import busio
3132
except ImportError:
3233
pass
3334

@@ -41,9 +42,10 @@
4142

4243
PCF8575_I2CADDR_DEFAULT: int = const(0x20) # Default I2C address
4344

45+
4446
class PCF8575:
45-
"""
46-
Interface library for PCF8575 GPIO expanders
47+
"""Interface library for PCF8575 GPIO expanders
48+
4749
:param ~busio.I2C i2c_bus: The I2C bus the PCF8575 is connected to.
4850
:param int address: The I2C device address. Default is :const:`0x20`
4951
"""
@@ -55,29 +57,39 @@ def __init__(
5557
self._writebuf = bytearray([0, 0])
5658
self._readbuf = bytearray([0, 0])
5759

58-
def get_pin(self, pin):
60+
def get_pin(self, pin: int) -> "DigitalInOut":
5961
"""Convenience function to create an instance of the DigitalInOut class
6062
pointing at the specified pin of this PCF8575 device.
63+
6164
:param int pin: pin to use for digital IO, 0 to 15
6265
"""
6366
assert 0 <= pin <= 15
6467
return DigitalInOut(pin, self)
6568

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+
6875
self._writebuf[0] = val & 0xFF
6976
self._writebuf[1] = (val >> 8) & 0xFF
7077
with self.i2c_device as i2c:
7178
i2c.write(self._writebuf)
7279

73-
def read_gpio(self):
80+
def read_gpio(self) -> int:
7481
"""Read the full 16-bits of data from the GPIO register"""
7582
with self.i2c_device as i2c:
7683
i2c.readinto(self._readbuf)
7784
return self._readbuf[0] | (self._readbuf[1] << 8)
7885

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+
8193
buff = self._writebuf[0] | (self._writebuf[1] << 8)
8294
if val:
8395
# turn on the pullup (write high)
@@ -86,8 +98,12 @@ def write_pin(self, pin, val):
8698
# turn on the transistor (write low)
8799
self.write_gpio(buff & ~(1 << pin))
88100

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+
91107
return (self.read_gpio() >> pin) & 0x1
92108

93109

@@ -102,15 +118,22 @@ def read_pin(self, pin):
102118
class DigitalInOut:
103119
"""Digital input/output of the PCF8575. The interface is exactly the
104120
same as the digitalio.DigitalInOut class, however:
121+
105122
* PCF8575 does not support pull-down resistors
106123
* PCF8575 does not actually have a sourcing transistor, instead there's
107-
an internal pullup
124+
an internal pullup
125+
108126
Exceptions will be thrown when attempting to set unsupported pull
109127
configurations.
110128
"""
111129

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+
114137
self._pin = pin_number
115138
self._pcf = pcf
116139
self._dir = (
@@ -122,44 +145,53 @@ def __init__(self, pin_number, pcf):
122145
# is unused by this class). Do not remove them, instead turn off pylint
123146
# in this case.
124147
# pylint: disable=unused-argument
125-
def switch_to_output(self, value=False, **kwargs):
148+
def switch_to_output(self, value: bool = False, **kwargs) -> None:
126149
"""Switch the pin state to a digital output with the provided starting
127150
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``
128157
"""
129158
self.direction = digitalio.Direction.OUTPUT
130159
self.value = value
131160

132-
def switch_to_input(self, pull=None, **kwargs):
161+
def switch_to_input(self, pull: Optional[digitalio.Pull] = None, **kwargs) -> None:
133162
"""Switch the pin state to a digital input which is the same as
134163
setting the light pullup on. Note that true tri-state or
135164
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`.
136168
"""
169+
137170
self.direction = digitalio.Direction.INPUT
138171
self.pull = pull
139172

140173
# pylint: enable=unused-argument
141174

142175
@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.
146179
"""
147180
return self._pcf.read_pin(self._pin)
148181

149182
@value.setter
150-
def value(self, val):
183+
def value(self, val: bool) -> None:
151184
self._pcf.write_pin(self._pin, val)
152185

153186
@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
157189
an INPUT enables the light pullup.
158190
"""
159191
return self._dir
160192

161193
@direction.setter
162-
def direction(self, val):
194+
def direction(self, val: digitalio.Direction) -> None:
163195
if val == digitalio.Direction.INPUT:
164196
# for inputs, turn on the pullup (write high)
165197
self._pcf.write_pin(self._pin, True)
@@ -172,14 +204,15 @@ def direction(self, val):
172204
raise ValueError("Expected INPUT or OUTPUT direction!")
173205

174206
@property
175-
def pull(self):
207+
def pull(self) -> digitalio.Pull:
176208
"""
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`
178211
"""
179212
return digitalio.Pull.UP
180213

181214
@pull.setter
182-
def pull(self, val):
215+
def pull(self, val: digitalio.Pull) -> None:
183216
if val is digitalio.Pull.UP:
184217
# for inputs, turn on the pullup (write high)
185218
self._pcf.write_pin(self._pin, True)

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131

3232
intersphinx_mapping = {
33-
"python": ("https://docs.python.org/3", None),"BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None),
34-
33+
"python": ("https://docs.python.org/3", None),
34+
"BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None),
3535
"CircuitPython": ("https://docs.circuitpython.org/en/latest/", None),
3636
}
3737

0 commit comments

Comments
 (0)