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