Skip to content

Commit e73dba2

Browse files
committed
make pixelbuf handle floats and iterables
1 parent 354edd9 commit e73dba2

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#include "shared-module/_pixelbuf/PixelBuf.h"
4141
#include "shared-bindings/digitalio/DigitalInOut.h"
4242

43+
#ifdef CIRCUITPY_ULAB
44+
#include "extmod/ulab/code/ndarray.h"
45+
#endif
46+
4347
extern const int32_t colorwheel(float pos);
4448

4549
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t* parsed);
@@ -326,27 +330,15 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
326330
} else { // Set
327331
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
328332

329-
if (!(MP_OBJ_IS_TYPE(value, &mp_type_list) || MP_OBJ_IS_TYPE(value, &mp_type_tuple))) {
330-
mp_raise_ValueError(translate("tuple/list required on RHS"));
331-
}
332-
333-
mp_obj_t *src_objs;
334-
size_t num_items;
335-
if (MP_OBJ_IS_TYPE(value, &mp_type_list)) {
336-
mp_obj_list_t *t = MP_OBJ_TO_PTR(value);
337-
num_items = t->len;
338-
src_objs = t->items;
339-
} else {
340-
mp_obj_tuple_t *l = MP_OBJ_TO_PTR(value);
341-
num_items = l->len;
342-
src_objs = l->items;
343-
}
344-
if (num_items != slice_len) {
345-
mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d, got %d)."),
346-
slice_len, num_items);
333+
size_t num_items = mp_obj_get_int(mp_obj_len(value));
334+
335+
if (num_items != slice_len && num_items != (slice_len * common_hal__pixelbuf_pixelbuf_get_bpp(self_in))) {
336+
mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d or %d, got %d)."),
337+
slice_len, slice_len * common_hal__pixelbuf_pixelbuf_get_bpp(self_in),
338+
num_items);
347339
}
348340

349-
common_hal__pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.step, slice_len, src_objs);
341+
common_hal__pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.step, slice_len, value, num_items != slice_len);
350342
return mp_const_none;
351343
#else
352344
return MP_OBJ_NULL; // op not supported

shared-bindings/_pixelbuf/PixelBuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ void common_hal__pixelbuf_pixelbuf_fill(mp_obj_t self, mp_obj_t item);
4747
void common_hal__pixelbuf_pixelbuf_show(mp_obj_t self);
4848
mp_obj_t common_hal__pixelbuf_pixelbuf_get_pixel(mp_obj_t self, size_t index);
4949
void common_hal__pixelbuf_pixelbuf_set_pixel(mp_obj_t self, size_t index, mp_obj_t item);
50-
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);
50+
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);
5151

5252
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H

shared-module/_pixelbuf/PixelBuf.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ void common_hal__pixelbuf_pixelbuf_set_brightness(mp_obj_t self_in, mp_float_t b
132132
}
133133
}
134134

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+
135147
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) {
136148
pixelbuf_byteorder_details_t *byteorder = &self->byteorder;
137149
// 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_
142154
*w = 0;
143155
}
144156

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);
147159
*r = value >> 16 & 0xff;
148160
*g = (value >> 8) & 0xff;
149161
*b = value & 0xff;
@@ -155,9 +167,9 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
155167
mp_raise_ValueError_varg(translate("Expected tuple of length %d, got %d"), byteorder->bpp, len);
156168
}
157169

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]);
161173
if (len > 3) {
162174
if (mp_obj_is_float(items[PIXEL_W])) {
163175
*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
218230
_pixelbuf_set_pixel_color(self, index, r, g, b, w);
219231
}
220232

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) {
222234
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+
}
226255
}
227256
if (self->auto_write) {
228257
common_hal__pixelbuf_pixelbuf_show(self_in);
229258
}
230259
}
231260

261+
262+
232263
void common_hal__pixelbuf_pixelbuf_set_pixel(mp_obj_t self_in, size_t index, mp_obj_t value) {
233264
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
234265
_pixelbuf_set_pixel(self, index, value);

0 commit comments

Comments
 (0)