24
24
* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards:
25
25
https://github.com/adafruit/circuitpython/releases
26
26
"""
27
+
28
+ try :
29
+ from busio import SPI
30
+ from digitalio import DigitalInOut
31
+ except ImportError :
32
+ pass
33
+
27
34
__version__ = "0.0.0+auto.0"
28
35
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TLC5947.git"
29
36
@@ -74,19 +81,19 @@ class PWMOut:
74
81
as it is fixed by the TLC5947 to ~2.4-5.6 mhz.
75
82
"""
76
83
77
- def __init__ (self , tlc5947 , channel ) :
84
+ def __init__ (self , tlc5947 : "TLC5947" , channel : int ) -> None :
78
85
self ._tlc5947 = tlc5947
79
86
self ._channel = channel
80
87
81
88
@property
82
- def duty_cycle (self ):
89
+ def duty_cycle (self ) -> int :
83
90
"""Get and set the 16-bit PWM duty cycle value for this channel."""
84
91
raw_value = self ._tlc5947 ._get_gs_value (self ._channel )
85
92
# Convert to 16-bit value from 12-bits and return it.
86
93
return (raw_value << 4 ) & 0xFFFF
87
94
88
95
@duty_cycle .setter
89
- def duty_cycle (self , val ) :
96
+ def duty_cycle (self , val : int ) -> None :
90
97
if val < 0 or val > 65535 :
91
98
raise ValueError (
92
99
"PWM intensity {0} outside supported range [0;65535]" .format (val )
@@ -96,7 +103,7 @@ def duty_cycle(self, val):
96
103
self ._tlc5947 ._set_gs_value (self ._channel , val )
97
104
98
105
@property
99
- def frequency (self ):
106
+ def frequency (self ) -> int :
100
107
"""Frequency of the PWM channel, note you cannot change this and
101
108
cannot read its exact value (it varies from 2.4-5.6 mhz, see the
102
109
TLC5947 datasheet).
@@ -108,12 +115,19 @@ def frequency(self):
108
115
# Must disable a few checks to make pylint happy (ugh).
109
116
# pylint: disable=no-self-use,unused-argument
110
117
@frequency .setter
111
- def frequency (self , val ) :
118
+ def frequency (self , val : int ) -> None :
112
119
raise RuntimeError ("Cannot set TLC5947 PWM frequency!" )
113
120
114
121
# pylint: enable=no-self-use,unused-argument
115
122
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 :
117
131
if num_drivers < 1 :
118
132
raise ValueError (
119
133
"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):
130
144
# any channel value change).
131
145
self .auto_write = auto_write
132
146
133
- def write (self ):
147
+ def write (self ) -> None :
134
148
"""Write out the current channel PWM values to the chip. This is only
135
149
necessary to call if you disabled auto_write in the initializer,
136
150
otherwise write is automatically called on any channel update.
@@ -152,7 +166,7 @@ def write(self):
152
166
# Ensure the SPI bus is unlocked.
153
167
self ._spi .unlock ()
154
168
155
- def _get_gs_value (self , channel ) :
169
+ def _get_gs_value (self , channel : int ) -> int :
156
170
# pylint: disable=no-else-return
157
171
# Disable should be removed when refactor can be tested
158
172
if channel < 0 or channel >= _CHANNELS * self ._n :
@@ -182,7 +196,7 @@ def _get_gs_value(self, channel):
182
196
else :
183
197
raise RuntimeError ("Unsupported bit offset!" )
184
198
185
- def _set_gs_value (self , channel , val ) :
199
+ def _set_gs_value (self , channel : int , val : int ) -> None :
186
200
if channel < 0 or channel >= _CHANNELS * self ._n :
187
201
raise ValueError (
188
202
"Channel {0} not available with {1} board(s)." .format (channel , self ._n )
@@ -223,7 +237,7 @@ def _set_gs_value(self, channel, val):
223
237
if self .auto_write :
224
238
self .write ()
225
239
226
- def create_pwm_out (self , channel ) :
240
+ def create_pwm_out (self , channel : int ) -> PWMOut :
227
241
"""Create an instance of a PWMOut-like class that mimics the built-in
228
242
CircuitPython PWMOut class but is associated with the TLC5947 channel
229
243
that is specified. This PWMOut class has a duty_cycle property which
@@ -237,19 +251,19 @@ def create_pwm_out(self, channel):
237
251
# Define index and length properties to set and get each channel's raw
238
252
# 12-bit value (useful for changing channels without quantization error
239
253
# like when using the PWMOut mock class).
240
- def __len__ (self ):
254
+ def __len__ (self ) -> int :
241
255
"""Retrieve the total number of PWM channels available."""
242
256
return _CHANNELS * self ._n # number channels times number chips.
243
257
244
- def __getitem__ (self , key ) :
258
+ def __getitem__ (self , key : int ) -> int :
245
259
"""Retrieve the 12-bit PWM value for the specified channel (0-max).
246
260
max depends on the number of boards.
247
261
"""
248
262
if key < 0 : # allow reverse adressing with negative index
249
263
key = key + _CHANNELS * self ._n
250
264
return self ._get_gs_value (key ) # does parameter checking
251
265
252
- def __setitem__ (self , key , val ) :
266
+ def __setitem__ (self , key : int , val : int ) -> None :
253
267
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max).
254
268
max depends on the number of boards.
255
269
If auto_write is enabled (the default) then the chip PWM state will
0 commit comments