39
39
_NEOPIXEL_BUF = const (0x04 )
40
40
_NEOPIXEL_SHOW = const (0x05 )
41
41
42
- class Neopixel :
42
+ # Pixel color order constants
43
+ RGB = (0 , 1 , 2 )
44
+ """Red Green Blue"""
45
+ GRB = (1 , 0 , 2 )
46
+ """Green Red Blue"""
47
+ RGBW = (0 , 1 , 2 , 3 )
48
+ """Red Green Blue White"""
49
+ GRBW = (1 , 0 , 2 , 3 )
50
+ """Green Red Blue White"""
51
+
52
+ class NeoPixel :
43
53
def __init__ (self , seesaw , pin , n , * , bpp = 3 , brightness = 1.0 , auto_write = True , pixel_order = None ):
54
+ # TODO: brightness not yet implemented.
44
55
self ._seesaw = seesaw
45
56
self ._pin = pin
46
57
self ._bpp = bpp
47
- self ._auto_write = auto_write
48
- self ._pixel_order = pixel_order
58
+ self .auto_write = auto_write
49
59
self ._n = n
50
60
self ._brightness = brightness
61
+ self ._pixel_order = GRBW if pixel_order is None else pixel_order
51
62
52
63
cmd = bytearray ([pin ])
53
64
self ._seesaw .write (_NEOPIXEL_BASE , _NEOPIXEL_PIN , cmd )
54
65
cmd = struct .pack (">H" , n * self ._bpp )
55
66
self ._seesaw .write (_NEOPIXEL_BASE , _NEOPIXEL_BUF_LENGTH , cmd )
56
67
68
+
57
69
@property
58
70
def brightness (self ):
59
71
pass
@@ -68,26 +80,51 @@ def deinit(self):
68
80
def __len__ (self ):
69
81
return self ._n
70
82
71
- def __setitem__ (self , key , value ):
72
- cmd = bytearray (6 )
73
- cmd [:2 ] = struct .pack (">H" , key * self ._bpp )
74
- cmd [2 :] = struct .pack (">I" , value )
83
+ def __setitem__ (self , key , color ):
84
+ cmd = bytearray (2 + self ._bpp )
85
+ struct .pack_into (">H" , cmd , 0 , key * self ._bpp )
86
+ if isinstance (color , int ):
87
+ w = color >> 24
88
+ r = (color >> 16 ) & 0xff
89
+ g = (color >> 8 ) & 0xff
90
+ b = color & 0xff
91
+ else :
92
+ if self ._bpp == 3 :
93
+ r , g , b = color
94
+ else :
95
+ r , g , b , w = color
96
+
97
+ # If all components are the same and we have a white pixel then use it
98
+ # instead of the individual components.
99
+ if self ._bpp == 4 and r == g == b :
100
+ w = r
101
+ r = 0
102
+ g = 0
103
+ b = 0
104
+
105
+ # Store colors in correct slots
106
+ cmd [2 + self ._pixel_order [0 ]] = r
107
+ cmd [2 + self ._pixel_order [1 ]] = g
108
+ cmd [2 + self ._pixel_order [2 ]] = b
109
+ if self ._bpp == 4 :
110
+ cmd [2 + self ._pixel_order [3 ]] = w
111
+
75
112
self ._seesaw .write (_NEOPIXEL_BASE , _NEOPIXEL_BUF , cmd )
76
- if self ._auto_write :
113
+ if self .auto_write :
77
114
self .show ()
78
115
79
116
def __getitem__ (self , key ):
80
117
pass
81
118
82
119
def fill (self , color ):
83
- cmd = bytearray (self ._n * self ._bpp + 2 )
120
+ # Suppress auto_write while filling.
121
+ current_auto_write = self .auto_write
122
+ self .auto_write = False
84
123
for i in range (self ._n ):
85
- cmd [self ._bpp * i + 2 :] = struct .pack (">I" , color )
86
-
87
- self ._seesaw .write (_NEOPIXEL_BASE , _NEOPIXEL_BUF , cmd )
88
-
89
- if self ._auto_write :
124
+ self [i ] = color
125
+ if current_auto_write :
90
126
self .show ()
127
+ self .auto_write = current_auto_write
91
128
92
129
def show (self ):
93
130
self ._seesaw .write (_NEOPIXEL_BASE , _NEOPIXEL_SHOW )
0 commit comments