Skip to content

Commit 759e912

Browse files
authored
Merge pull request #42 from tekktrik/doc/add-typing
Update documentation
2 parents 923a1a0 + 6ec9486 commit 759e912

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

adafruit_pca9685.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,27 @@
4040
from adafruit_register.i2c_struct_array import StructArray
4141
from adafruit_bus_device import i2c_device
4242

43+
try:
44+
from typing import Optional, Type
45+
from types import TracebackType
46+
from busio import I2C
47+
except ImportError:
48+
pass
49+
4350

4451
class PWMChannel:
45-
"""A single PCA9685 channel that matches the :py:class:`~pwmio.PWMOut` API."""
52+
"""A single PCA9685 channel that matches the :py:class:`~pwmio.PWMOut` API.
53+
54+
:param PCA9685 pca: The PCA9685 object
55+
:param int index: The index of the channel
56+
"""
4657

47-
def __init__(self, pca, index):
58+
def __init__(self, pca: "PCA9685", index: int):
4859
self._pca = pca
4960
self._index = index
5061

5162
@property
52-
def frequency(self):
63+
def frequency(self) -> float:
5364
"""The overall PWM frequency in Hertz (read-only).
5465
A PWMChannel's frequency cannot be set individually.
5566
All channels share a common frequency, set by PCA9685.frequency."""
@@ -60,7 +71,7 @@ def frequency(self, _):
6071
raise NotImplementedError("frequency cannot be set on individual channels")
6172

6273
@property
63-
def duty_cycle(self):
74+
def duty_cycle(self) -> int:
6475
"""16 bit value that dictates how much of one cycle is high (1) versus low (0). 0xffff will
6576
always be high, 0 will always be low and 0x7fff will be half high and then half low."""
6677
pwm = self._pca.pwm_regs[self._index]
@@ -69,7 +80,7 @@ def duty_cycle(self):
6980
return pwm[1] << 4
7081

7182
@duty_cycle.setter
72-
def duty_cycle(self, value):
83+
def duty_cycle(self, value: int) -> None:
7384
if not 0 <= value <= 0xFFFF:
7485
raise ValueError("Out of range")
7586

@@ -82,16 +93,19 @@ def duty_cycle(self, value):
8293

8394

8495
class PCAChannels: # pylint: disable=too-few-public-methods
85-
"""Lazily creates and caches channel objects as needed. Treat it like a sequence."""
96+
"""Lazily creates and caches channel objects as needed. Treat it like a sequence.
97+
98+
:param PCA9685 pca: The PCA9685 object
99+
"""
86100

87-
def __init__(self, pca):
101+
def __init__(self, pca: "PCA9685") -> None:
88102
self._pca = pca
89103
self._channels = [None] * len(self)
90104

91-
def __len__(self):
105+
def __len__(self) -> int:
92106
return 16
93107

94-
def __getitem__(self, index):
108+
def __getitem__(self, index: int) -> PWMChannel:
95109
if not self._channels[index]:
96110
self._channels[index] = PWMChannel(self._pca, index)
97111
return self._channels[index]
@@ -117,25 +131,31 @@ class PCA9685:
117131
prescale_reg = UnaryStruct(0xFE, "<B")
118132
pwm_regs = StructArray(0x06, "<HH", 16)
119133

120-
def __init__(self, i2c_bus, *, address=0x40, reference_clock_speed=25000000):
134+
def __init__(
135+
self,
136+
i2c_bus: I2C,
137+
*,
138+
address: int = 0x40,
139+
reference_clock_speed: int = 25000000
140+
) -> None:
121141
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
122142
self.channels = PCAChannels(self)
123143
"""Sequence of 16 `PWMChannel` objects. One for each channel."""
124144
self.reference_clock_speed = reference_clock_speed
125145
"""The reference clock speed in Hz."""
126146
self.reset()
127147

128-
def reset(self):
148+
def reset(self) -> None:
129149
"""Reset the chip."""
130150
self.mode1_reg = 0x00 # Mode1
131151

132152
@property
133-
def frequency(self):
153+
def frequency(self) -> float:
134154
"""The overall PWM frequency in Hertz."""
135155
return self.reference_clock_speed / 4096 / self.prescale_reg
136156

137157
@frequency.setter
138-
def frequency(self, freq):
158+
def frequency(self, freq: float) -> None:
139159
prescale = int(self.reference_clock_speed / 4096.0 / freq + 0.5)
140160
if prescale < 3:
141161
raise ValueError("PCA9685 cannot output at the given frequency")
@@ -147,12 +167,17 @@ def frequency(self, freq):
147167
# Mode 1, autoincrement on, fix to stop pca9685 from accepting commands at all addresses
148168
self.mode1_reg = old_mode | 0xA0
149169

150-
def __enter__(self):
170+
def __enter__(self) -> "PCA9685":
151171
return self
152172

153-
def __exit__(self, exception_type, exception_value, traceback):
173+
def __exit__(
174+
self,
175+
exception_type: Optional[Type[type]],
176+
exception_value: Optional[BaseException],
177+
traceback: Optional[TracebackType],
178+
) -> None:
154179
self.deinit()
155180

156-
def deinit(self):
181+
def deinit(self) -> None:
157182
"""Stop using the pca9685."""
158183
self.reset()

0 commit comments

Comments
 (0)