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