16
16
import busio
17
17
import digitalio
18
18
19
+ try :
20
+ from typing import Any , Union , Tuple , List
21
+ from microcontroller import Pin
22
+ except ImportError :
23
+ pass
24
+
19
25
__version__ = "0.0.0-auto.0"
20
26
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_WS2801.git"
21
27
22
- ### based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
28
+ # based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
23
29
24
30
25
31
class WS2801 :
@@ -49,7 +55,15 @@ class WS2801:
49
55
time.sleep(2)
50
56
"""
51
57
52
- def __init__ (self , clock , data , n , * , brightness = 1.0 , auto_write = True ):
58
+ def __init__ (
59
+ self ,
60
+ clock : Pin ,
61
+ data : Pin ,
62
+ n : int ,
63
+ * ,
64
+ brightness : float = 1.0 ,
65
+ auto_write : bool = True
66
+ ) -> None :
53
67
self ._spi = None
54
68
try :
55
69
self ._spi = busio .SPI (clock , MOSI = data )
@@ -64,15 +78,15 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
64
78
self .cpin .value = False
65
79
self ._n = n
66
80
self ._buf = bytearray (n * 3 )
67
- self ._brightness = 1.0 ### keeps pylint happy
81
+ self ._brightness = 1.0 # keeps pylint happy
68
82
# Set auto_write to False temporarily so brightness setter does _not_
69
83
# call show() while in __init__.
70
84
self .auto_write = False
71
85
self .brightness = brightness
72
86
self .auto_write = auto_write
73
- ### TODO - review/consider adding GRB support like that in c++ version
87
+ # TODO - review/consider adding GRB support like that in c++ version
74
88
75
- def deinit (self ):
89
+ def deinit (self ) -> None :
76
90
"""Blank out the DotStars and release the resources."""
77
91
self .auto_write = False
78
92
black = (0 , 0 , 0 )
@@ -84,16 +98,18 @@ def deinit(self):
84
98
self .dpin .deinit ()
85
99
self .cpin .deinit ()
86
100
87
- def __enter__ (self ):
101
+ def __enter__ (self ) -> "WS2801" :
88
102
return self
89
103
90
- def __exit__ (self , exception_type , exception_value , traceback ):
104
+ def __exit__ (
105
+ self , exception_type : Any , exception_value : Any , traceback : Any
106
+ ) -> None :
91
107
self .deinit ()
92
108
93
109
def __repr__ (self ):
94
110
return "[" + ", " .join ([str (x ) for x in self ]) + "]"
95
111
96
- def _set_item (self , index , value ):
112
+ def _set_item (self , index : int , value : Union [ Tuple [ int , ...], int ] ):
97
113
offset = index * 3
98
114
if isinstance (value , int ):
99
115
r = value >> 16
@@ -106,7 +122,7 @@ def _set_item(self, index, value):
106
122
self ._buf [offset + 1 ] = g
107
123
self ._buf [offset + 2 ] = b
108
124
109
- def __setitem__ (self , index , val ):
125
+ def __setitem__ (self , index : int , val : Union [ Tuple [ int , ...], int ] ):
110
126
if isinstance (index , slice ):
111
127
start , stop , step = index .indices (self ._n )
112
128
length = stop - start
@@ -122,7 +138,9 @@ def __setitem__(self, index, val):
122
138
if self .auto_write :
123
139
self .show ()
124
140
125
- def __getitem__ (self , index ):
141
+ def __getitem__ (
142
+ self , index : Union [slice , int ]
143
+ ) -> Union [Tuple [int , ...], List [Tuple [int , ...]]]:
126
144
if isinstance (index , slice ):
127
145
out = []
128
146
for in_i in range (* index .indices (self ._n )):
@@ -135,21 +153,21 @@ def __getitem__(self, index):
135
153
offset = index * 3
136
154
return tuple (self ._buf [offset + i ] for i in range (3 ))
137
155
138
- def __len__ (self ):
156
+ def __len__ (self ) -> int :
139
157
return self ._n
140
158
141
159
@property
142
- def brightness (self ):
160
+ def brightness (self ) -> float :
143
161
"""Overall brightness of the pixel"""
144
162
return self ._brightness
145
163
146
164
@brightness .setter
147
- def brightness (self , brightness ) :
165
+ def brightness (self , brightness : float ) -> None :
148
166
self ._brightness = min (max (brightness , 0.0 ), 1.0 )
149
167
if self .auto_write :
150
168
self .show ()
151
169
152
- def fill (self , color ) :
170
+ def fill (self , color : Union [ Tuple [ int , ...], int ]) -> None :
153
171
"""Colors all pixels the given ***color***."""
154
172
auto_write = self .auto_write
155
173
self .auto_write = False
@@ -159,15 +177,15 @@ def fill(self, color):
159
177
self .show ()
160
178
self .auto_write = auto_write
161
179
162
- def _ds_writebytes (self , buf ) :
180
+ def _ds_writebytes (self , buf : bytearray ) -> None :
163
181
for b in buf :
164
182
for _ in range (8 ):
165
183
self .dpin .value = b & 0x80
166
184
self .cpin .value = True
167
185
self .cpin .value = False
168
186
b = b << 1
169
187
170
- def show (self ):
188
+ def show (self ) -> None :
171
189
"""Shows the new colors on the pixels themselves if they haven't already
172
190
been autowritten.
173
191
0 commit comments