Skip to content

Commit 5b49d76

Browse files
authored
Merge pull request #26 from sokratisvas/add-type-annotations
Add Type Annotations
2 parents 856799c + 140b8de commit 5b49d76

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

adafruit_tlc5947.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards:
2525
https://github.com/adafruit/circuitpython/releases
2626
"""
27+
28+
try:
29+
from busio import SPI
30+
from digitalio import DigitalInOut
31+
except ImportError:
32+
pass
33+
2734
__version__ = "0.0.0+auto.0"
2835
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TLC5947.git"
2936

@@ -74,19 +81,19 @@ class PWMOut:
7481
as it is fixed by the TLC5947 to ~2.4-5.6 mhz.
7582
"""
7683

77-
def __init__(self, tlc5947, channel):
84+
def __init__(self, tlc5947: "TLC5947", channel: int) -> None:
7885
self._tlc5947 = tlc5947
7986
self._channel = channel
8087

8188
@property
82-
def duty_cycle(self):
89+
def duty_cycle(self) -> int:
8390
"""Get and set the 16-bit PWM duty cycle value for this channel."""
8491
raw_value = self._tlc5947._get_gs_value(self._channel)
8592
# Convert to 16-bit value from 12-bits and return it.
8693
return (raw_value << 4) & 0xFFFF
8794

8895
@duty_cycle.setter
89-
def duty_cycle(self, val):
96+
def duty_cycle(self, val: int) -> None:
9097
if val < 0 or val > 65535:
9198
raise ValueError(
9299
"PWM intensity {0} outside supported range [0;65535]".format(val)
@@ -96,7 +103,7 @@ def duty_cycle(self, val):
96103
self._tlc5947._set_gs_value(self._channel, val)
97104

98105
@property
99-
def frequency(self):
106+
def frequency(self) -> int:
100107
"""Frequency of the PWM channel, note you cannot change this and
101108
cannot read its exact value (it varies from 2.4-5.6 mhz, see the
102109
TLC5947 datasheet).
@@ -108,12 +115,19 @@ def frequency(self):
108115
# Must disable a few checks to make pylint happy (ugh).
109116
# pylint: disable=no-self-use,unused-argument
110117
@frequency.setter
111-
def frequency(self, val):
118+
def frequency(self, val: int) -> None:
112119
raise RuntimeError("Cannot set TLC5947 PWM frequency!")
113120

114121
# pylint: enable=no-self-use,unused-argument
115122

116-
def __init__(self, spi, latch, *, auto_write=True, num_drivers=1):
123+
def __init__(
124+
self,
125+
spi: SPI,
126+
latch: DigitalInOut,
127+
*,
128+
auto_write: bool = True,
129+
num_drivers: int = 1
130+
) -> None:
117131
if num_drivers < 1:
118132
raise ValueError(
119133
"Need at least one driver; {0} is not supported.".format(num_drivers)
@@ -130,7 +144,7 @@ def __init__(self, spi, latch, *, auto_write=True, num_drivers=1):
130144
# any channel value change).
131145
self.auto_write = auto_write
132146

133-
def write(self):
147+
def write(self) -> None:
134148
"""Write out the current channel PWM values to the chip. This is only
135149
necessary to call if you disabled auto_write in the initializer,
136150
otherwise write is automatically called on any channel update.
@@ -152,7 +166,7 @@ def write(self):
152166
# Ensure the SPI bus is unlocked.
153167
self._spi.unlock()
154168

155-
def _get_gs_value(self, channel):
169+
def _get_gs_value(self, channel: int) -> int:
156170
# pylint: disable=no-else-return
157171
# Disable should be removed when refactor can be tested
158172
if channel < 0 or channel >= _CHANNELS * self._n:
@@ -182,7 +196,7 @@ def _get_gs_value(self, channel):
182196
else:
183197
raise RuntimeError("Unsupported bit offset!")
184198

185-
def _set_gs_value(self, channel, val):
199+
def _set_gs_value(self, channel: int, val: int) -> None:
186200
if channel < 0 or channel >= _CHANNELS * self._n:
187201
raise ValueError(
188202
"Channel {0} not available with {1} board(s).".format(channel, self._n)
@@ -223,7 +237,7 @@ def _set_gs_value(self, channel, val):
223237
if self.auto_write:
224238
self.write()
225239

226-
def create_pwm_out(self, channel):
240+
def create_pwm_out(self, channel: int) -> PWMOut:
227241
"""Create an instance of a PWMOut-like class that mimics the built-in
228242
CircuitPython PWMOut class but is associated with the TLC5947 channel
229243
that is specified. This PWMOut class has a duty_cycle property which
@@ -237,19 +251,19 @@ def create_pwm_out(self, channel):
237251
# Define index and length properties to set and get each channel's raw
238252
# 12-bit value (useful for changing channels without quantization error
239253
# like when using the PWMOut mock class).
240-
def __len__(self):
254+
def __len__(self) -> int:
241255
"""Retrieve the total number of PWM channels available."""
242256
return _CHANNELS * self._n # number channels times number chips.
243257

244-
def __getitem__(self, key):
258+
def __getitem__(self, key: int) -> int:
245259
"""Retrieve the 12-bit PWM value for the specified channel (0-max).
246260
max depends on the number of boards.
247261
"""
248262
if key < 0: # allow reverse adressing with negative index
249263
key = key + _CHANNELS * self._n
250264
return self._get_gs_value(key) # does parameter checking
251265

252-
def __setitem__(self, key, val):
266+
def __setitem__(self, key: int, val: int) -> None:
253267
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max).
254268
max depends on the number of boards.
255269
If auto_write is enabled (the default) then the chip PWM state will

0 commit comments

Comments
 (0)