13
13
14
14
"""
15
15
16
+ try :
17
+ from typing import Optional , Tuple , Union , Sequence
18
+
19
+ ColorUnion = Union [int , Tuple [int , int , int ], Tuple [int , int , int , int ]]
20
+ except ImportError :
21
+ pass
22
+
16
23
__version__ = "0.0.0-auto.0"
17
24
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Pixelbuf.git"
18
25
@@ -37,12 +44,12 @@ class PixelBuf: # pylint: disable=too-many-instance-attributes
37
44
38
45
def __init__ ( # pylint: disable=too-many-locals,too-many-arguments
39
46
self ,
40
- n ,
41
- byteorder = "BGR" ,
42
- brightness = 1.0 ,
43
- auto_write = False ,
44
- header = None ,
45
- trailer = None ,
47
+ n : int ,
48
+ byteorder : str = "BGR" ,
49
+ brightness : float = 1.0 ,
50
+ auto_write : bool = False ,
51
+ header : Optional [ bytes ] = None ,
52
+ trailer : Optional [ bytes ] = None ,
46
53
):
47
54
48
55
bpp , byteorder_tuple , has_white , dotstar_mode = self .parse_byteorder (byteorder )
@@ -94,7 +101,7 @@ def __init__( # pylint: disable=too-many-locals,too-many-arguments
94
101
self .auto_write = auto_write
95
102
96
103
@staticmethod
97
- def parse_byteorder (byteorder ) :
104
+ def parse_byteorder (byteorder : str ) -> Tuple [ int , str , bool , bool ] :
98
105
"""
99
106
Parse a Byteorder string for validity and determine bpp, byte order, and
100
107
dostar brightness bits.
@@ -106,8 +113,8 @@ def parse_byteorder(byteorder):
106
113
W - White
107
114
P - PWM (PWM Duty cycle for pixel - dotstars 0 - 1.0)
108
115
109
- :param: ~str bpp: bpp string.
110
- :return: ~tuple: bpp, byteorder, has_white, dotstar_mode
116
+ :param ~str bpp: bpp string.
117
+ :return ~tuple: bpp, byteorder, has_white, dotstar_mode
111
118
"""
112
119
bpp = len (byteorder )
113
120
dotstar_mode = False
@@ -153,7 +160,7 @@ def brightness(self):
153
160
return self ._brightness
154
161
155
162
@brightness .setter
156
- def brightness (self , value ):
163
+ def brightness (self , value : float ):
157
164
value = min (max (value , 0.0 ), 1.0 )
158
165
change = value - self ._brightness
159
166
if - 0.001 < change < 0.001 :
@@ -196,7 +203,7 @@ def show(self):
196
203
"""
197
204
return self ._transmit (self ._post_brightness_buffer )
198
205
199
- def fill (self , color ):
206
+ def fill (self , color : ColorUnion ):
200
207
"""
201
208
Fills the given pixelbuf with the given color.
202
209
:param pixelbuf: A pixel object.
@@ -208,7 +215,7 @@ def fill(self, color):
208
215
if self .auto_write :
209
216
self .show ()
210
217
211
- def _parse_color (self , value ) :
218
+ def _parse_color (self , value : ColorUnion ) -> Tuple [ int , int , int , int ] :
212
219
r = 0
213
220
g = 0
214
221
b = 0
@@ -258,7 +265,7 @@ def _parse_color(self, value):
258
265
return (r , g , b , w )
259
266
260
267
def _set_item (
261
- self , index , r , g , b , w
268
+ self , index : int , r : int , g : int , b : int , w : int
262
269
): # pylint: disable=too-many-locals,too-many-branches,too-many-arguments
263
270
if index < 0 :
264
271
index += len (self )
@@ -289,7 +296,9 @@ def _set_item(
289
296
b * self ._brightness
290
297
)
291
298
292
- def __setitem__ (self , index , val ):
299
+ def __setitem__ (
300
+ self , index : Union [int , slice ], val : Union [ColorUnion , Sequence [ColorUnion ]]
301
+ ):
293
302
if isinstance (index , slice ):
294
303
start , stop , step = index .indices (self ._pixels )
295
304
for val_i , in_i in enumerate (range (start , stop , step )):
@@ -302,7 +311,7 @@ def __setitem__(self, index, val):
302
311
if self .auto_write :
303
312
self .show ()
304
313
305
- def _getitem (self , index ):
314
+ def _getitem (self , index : int ):
306
315
start = self ._offset + (index * self ._bpp )
307
316
buffer = (
308
317
self ._pre_brightness_buffer
@@ -322,7 +331,7 @@ def _getitem(self, index):
322
331
)
323
332
return value
324
333
325
- def __getitem__ (self , index ):
334
+ def __getitem__ (self , index : Union [ int , slice ] ):
326
335
if isinstance (index , slice ):
327
336
out = []
328
337
for in_i in range (
@@ -336,5 +345,5 @@ def __getitem__(self, index):
336
345
raise IndexError
337
346
return self ._getitem (index )
338
347
339
- def _transmit (self , buffer ):
348
+ def _transmit (self , buffer : bytearray ):
340
349
raise NotImplementedError ("Must be subclassed" )
0 commit comments