@@ -132,6 +132,18 @@ void common_hal__pixelbuf_pixelbuf_set_brightness(mp_obj_t self_in, mp_float_t b
132
132
}
133
133
}
134
134
135
+ uint8_t _pixelbuf_get_as_uint8 (mp_obj_t obj ) {
136
+ if (MP_OBJ_IS_SMALL_INT (obj )) {
137
+ return MP_OBJ_SMALL_INT_VALUE (obj );
138
+ } else if (MP_OBJ_IS_INT (obj )) {
139
+ return mp_obj_get_int_truncated (obj );
140
+ } else if (mp_obj_is_float (obj )) {
141
+ return (uint8_t )mp_obj_get_float (obj );
142
+ }
143
+ mp_raise_TypeError_varg (
144
+ translate ("can't convert %q to %q" ), mp_obj_get_type_qstr (obj ), MP_QSTR_int );
145
+ }
146
+
135
147
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 ) {
136
148
pixelbuf_byteorder_details_t * byteorder = & self -> byteorder ;
137
149
// w is shared between white in NeoPixels and brightness in dotstars (so that DotStars can have
@@ -142,8 +154,8 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
142
154
* w = 0 ;
143
155
}
144
156
145
- if (MP_OBJ_IS_INT (color )) {
146
- mp_int_t value = mp_obj_get_int_truncated (color );
157
+ if (MP_OBJ_IS_INT (color ) || mp_obj_is_float ( color ) ) {
158
+ mp_int_t value = MP_OBJ_IS_INT ( color ) ? mp_obj_get_int_truncated ( color ) : mp_obj_get_float (color );
147
159
* r = value >> 16 & 0xff ;
148
160
* g = (value >> 8 ) & 0xff ;
149
161
* b = value & 0xff ;
@@ -155,9 +167,9 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
155
167
mp_raise_ValueError_varg (translate ("Expected tuple of length %d, got %d" ), byteorder -> bpp , len );
156
168
}
157
169
158
- * r = mp_obj_get_int_truncated (items [PIXEL_R ]);
159
- * g = mp_obj_get_int_truncated (items [PIXEL_G ]);
160
- * b = mp_obj_get_int_truncated (items [PIXEL_B ]);
170
+ * r = _pixelbuf_get_as_uint8 (items [PIXEL_R ]);
171
+ * g = _pixelbuf_get_as_uint8 (items [PIXEL_G ]);
172
+ * b = _pixelbuf_get_as_uint8 (items [PIXEL_B ]);
161
173
if (len > 3 ) {
162
174
if (mp_obj_is_float (items [PIXEL_W ])) {
163
175
* w = 255 * mp_obj_get_float (items [PIXEL_W ]);
@@ -218,17 +230,36 @@ void _pixelbuf_set_pixel(pixelbuf_pixelbuf_obj_t* self, size_t index, mp_obj_t v
218
230
_pixelbuf_set_pixel_color (self , index , r , g , b , w );
219
231
}
220
232
221
- void common_hal__pixelbuf_pixelbuf_set_pixels (mp_obj_t self_in , size_t start , mp_int_t step , size_t slice_len , mp_obj_t * values ) {
233
+ void common_hal__pixelbuf_pixelbuf_set_pixels (mp_obj_t self_in , size_t start , mp_int_t step , size_t slice_len , mp_obj_t * values , bool flattened ) {
222
234
pixelbuf_pixelbuf_obj_t * self = native_pixelbuf (self_in );
223
- for (size_t i = 0 ; i < slice_len ; i ++ ) {
224
- _pixelbuf_set_pixel (self , start , values [i ]);
225
- start += step ;
235
+ mp_obj_iter_buf_t iter_buf ;
236
+ mp_obj_t iterable = mp_getiter (values , & iter_buf );
237
+ mp_obj_t item ;
238
+ size_t i = 0 ;
239
+ mp_obj_tuple_t * tuple ;
240
+ uint bpp = self -> bytes_per_pixel ;
241
+ if (flattened ) {
242
+ tuple = MP_OBJ_TO_PTR (mp_obj_new_tuple (bpp , NULL ));
243
+ }
244
+ while ((item = mp_iternext (iterable )) != MP_OBJ_STOP_ITERATION ) {
245
+ if (flattened ) {
246
+ tuple -> items [i % bpp ] = item ;
247
+ if (++ i % bpp == 0 ) {
248
+ _pixelbuf_set_pixel (self , start , tuple );
249
+ start += step ;
250
+ }
251
+ } else {
252
+ _pixelbuf_set_pixel (self , start , item );
253
+ start += step ;
254
+ }
226
255
}
227
256
if (self -> auto_write ) {
228
257
common_hal__pixelbuf_pixelbuf_show (self_in );
229
258
}
230
259
}
231
260
261
+
262
+
232
263
void common_hal__pixelbuf_pixelbuf_set_pixel (mp_obj_t self_in , size_t index , mp_obj_t value ) {
233
264
pixelbuf_pixelbuf_obj_t * self = native_pixelbuf (self_in );
234
265
_pixelbuf_set_pixel (self , index , value );
0 commit comments