@@ -152,50 +152,56 @@ STATIC uint8_t _pixelbuf_get_as_uint8(mp_obj_t obj) {
152
152
translate ("can't convert %q to %q" ), mp_obj_get_type_qstr (obj ), MP_QSTR_int );
153
153
}
154
154
155
- STATIC void _pixelbuf_parse_color (pixelbuf_pixelbuf_obj_t * self , mp_obj_t color , uint8_t * r , uint8_t * g , uint8_t * b , uint8_t * w ) {
155
+ STATIC color_u _pixelbuf_parse_color (pixelbuf_pixelbuf_obj_t * self , mp_obj_t color ) {
156
+ color_u result ;
156
157
pixelbuf_byteorder_details_t * byteorder = & self -> byteorder ;
157
158
// w is shared between white in NeoPixels and brightness in dotstars (so that DotStars can have
158
159
// per-pixel brightness). Set the defaults here in case it isn't set below.
159
160
if (byteorder -> is_dotstar ) {
160
- * w = 255 ;
161
+ result . w = 255 ;
161
162
} else {
162
- * w = 0 ;
163
+ result . w = 0 ;
163
164
}
164
165
165
166
if (mp_obj_is_int (color ) || mp_obj_is_float (color )) {
166
167
mp_int_t value = mp_obj_is_int (color ) ? mp_obj_get_int_truncated (color ) : (mp_int_t )mp_obj_get_float (color );
167
- * r = value >> 16 & 0xff ;
168
- * g = (value >> 8 ) & 0xff ;
169
- * b = value & 0xff ;
168
+ result . r = value >> 16 & 0xff ;
169
+ result . g = (value >> 8 ) & 0xff ;
170
+ result . b = value & 0xff ;
170
171
} else {
171
172
mp_obj_t * items ;
172
173
size_t len ;
173
174
mp_obj_get_array (color , & len , & items );
174
175
mp_arg_validate_length_range (len , 3 , 4 , MP_QSTR_color );
175
176
176
- * r = _pixelbuf_get_as_uint8 (items [PIXEL_R ]);
177
- * g = _pixelbuf_get_as_uint8 (items [PIXEL_G ]);
178
- * b = _pixelbuf_get_as_uint8 (items [PIXEL_B ]);
177
+ result . r = _pixelbuf_get_as_uint8 (items [PIXEL_R ]);
178
+ result . g = _pixelbuf_get_as_uint8 (items [PIXEL_G ]);
179
+ result . b = _pixelbuf_get_as_uint8 (items [PIXEL_B ]);
179
180
if (len > 3 ) {
180
181
if (mp_obj_is_float (items [PIXEL_W ])) {
181
- * w = 255 * mp_obj_get_float (items [PIXEL_W ]);
182
+ result . w = 255 * mp_obj_get_float (items [PIXEL_W ]);
182
183
} else {
183
- * w = mp_obj_get_int_truncated (items [PIXEL_W ]);
184
+ result . w = mp_obj_get_int_truncated (items [PIXEL_W ]);
184
185
}
185
- return ;
186
+ return result ;
186
187
}
187
188
}
188
189
// Int colors can't set white directly so convert to white when all components are equal.
189
190
// Also handles RGBW values assigned an RGB tuple.
190
- if (!byteorder -> is_dotstar && byteorder -> bpp == 4 && byteorder -> has_white && * r == * g && * r == * b ) {
191
- * w = * r ;
192
- * r = 0 ;
193
- * g = 0 ;
194
- * b = 0 ;
191
+ if (!byteorder -> is_dotstar && byteorder -> bpp == 4 && byteorder -> has_white && result . r == result . g && result . r == result . b ) {
192
+ result . w = result . r ;
193
+ result . r = 0 ;
194
+ result . g = 0 ;
195
+ result . b = 0 ;
195
196
}
197
+ return result ;
196
198
}
197
199
198
- STATIC void _pixelbuf_set_pixel_color (pixelbuf_pixelbuf_obj_t * self , size_t index , uint8_t r , uint8_t g , uint8_t b , uint8_t w ) {
200
+ STATIC void _pixelbuf_set_pixel_color (pixelbuf_pixelbuf_obj_t * self , size_t index , color_u rgbw ) {
201
+ int r = rgbw .r ;
202
+ int g = rgbw .g ;
203
+ int b = rgbw .b ;
204
+ int w = rgbw .w ;
199
205
// DotStars don't have white, instead they have 5 bit brightness so pack it into w. Shift right
200
206
// by three to leave the top five bits.
201
207
if (self -> bytes_per_pixel == 4 && self -> byteorder .is_dotstar ) {
@@ -234,12 +240,8 @@ STATIC void _pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t *self, size_t inde
234
240
}
235
241
236
242
STATIC void _pixelbuf_set_pixel (pixelbuf_pixelbuf_obj_t * self , size_t index , mp_obj_t value ) {
237
- uint8_t r ;
238
- uint8_t g ;
239
- uint8_t b ;
240
- uint8_t w ;
241
- _pixelbuf_parse_color (self , value , & r , & g , & b , & w );
242
- _pixelbuf_set_pixel_color (self , index , r , g , b , w );
243
+ color_u rgbw = _pixelbuf_parse_color (self , value );
244
+ _pixelbuf_set_pixel_color (self , index , rgbw );
243
245
}
244
246
245
247
void common_hal_adafruit_pixelbuf_pixelbuf_set_pixels (mp_obj_t self_in , size_t start , mp_int_t step , size_t slice_len , mp_obj_t * values ,
@@ -318,14 +320,10 @@ void common_hal_adafruit_pixelbuf_pixelbuf_show(mp_obj_t self_in) {
318
320
void common_hal_adafruit_pixelbuf_pixelbuf_fill (mp_obj_t self_in , mp_obj_t fill_color ) {
319
321
pixelbuf_pixelbuf_obj_t * self = native_pixelbuf (self_in );
320
322
321
- uint8_t r ;
322
- uint8_t g ;
323
- uint8_t b ;
324
- uint8_t w ;
325
- _pixelbuf_parse_color (self , fill_color , & r , & g , & b , & w );
323
+ color_u rgbw = _pixelbuf_parse_color (self , fill_color );
326
324
327
325
for (size_t i = 0 ; i < self -> pixel_count ; i ++ ) {
328
- _pixelbuf_set_pixel_color (self , i , r , g , b , w );
326
+ _pixelbuf_set_pixel_color (self , i , rgbw );
329
327
}
330
328
if (self -> auto_write ) {
331
329
common_hal_adafruit_pixelbuf_pixelbuf_show (self_in );
0 commit comments