Skip to content

Commit dc8c27d

Browse files
committed
make dotstar byteorder constants start with P. fix small bugs.
1 parent 55c688c commit dc8c27d

4 files changed

Lines changed: 24 additions & 38 deletions

File tree

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ extern const int32_t colorwheel(float pos);
5959
//|
6060
//| When only given ``buf``, ``brightness`` applies to the next pixel assignment.
6161
//|
62-
//| When ``D`` (dotstar mode) is present in the byteorder configuration, the
63-
//| 4th value in a tuple/list is the individual pixel brightness (0-1).
62+
//| When ``P`` (pwm duration) is present as the 4th character of the byteorder
63+
//| string, the 4th value in the tuple/list for a pixel is the individual pixel
64+
//| brightness (0.0-1.0) and will enable a Dotstar compatible 1st byte in the
65+
//| output buffer (``buf``).
6466
//|
6567
//| :param ~int size: Number of pixelsx
6668
//| :param ~bytearray buf: Bytearray in which to store pixel data
67-
//| :param ~str byteorder: Byte order string (such as "BGR" or "BGRD")
69+
//| :param ~str byteorder: Byte order string (such as "BGR" or "DBGR")
6870
//| :param ~float brightness: Brightness (0 to 1.0, default 1.0)
6971
//| :param ~bytearray rawbuf: Bytearray in which to store raw pixel data (before brightness adjustment)
7072
//| :param ~int offset: Offset from start of buffer (default 0)
@@ -77,7 +79,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
7779
static const mp_arg_t allowed_args[] = {
7880
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_INT },
7981
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ },
80-
{ MP_QSTR_byteorder, MP_ARG_OBJ, { .u_obj = mp_const_none } },
82+
{ MP_QSTR_byteorder, MP_ARG_OBJ, { .u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_BGR) } },
8183
{ MP_QSTR_brightness, MP_ARG_OBJ, { .u_obj = mp_const_none } },
8284
{ MP_QSTR_rawbuf, MP_ARG_OBJ, { .u_obj = mp_const_none } },
8385
{ MP_QSTR_offset, MP_ARG_INT, { .u_int = 0 } },
@@ -92,17 +94,13 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
9294
if (!MP_OBJ_IS_STR(args[ARG_byteorder].u_obj))
9395
mp_raise_TypeError(translate("byteorder is not a string"));
9496

95-
if (args[ARG_byteorder].u_obj == NULL)
96-
byteorder = "BGR";
97-
else
98-
byteorder = mp_obj_str_get_data(args[ARG_byteorder].u_obj, &bo_len);
99-
97+
byteorder = mp_obj_str_get_data(args[ARG_byteorder].u_obj, &bo_len);
10098
if (bo_len < 3 || bo_len > 4)
10199
mp_raise_ValueError(translate("Invalid byteorder string"));
102-
strncpy(byteorder_details.order, byteorder, sizeof(byteorder_details.order));
100+
byteorder_details.order = args[ARG_byteorder].u_obj;
103101

104102
byteorder_details.bpp = bo_len;
105-
char *dotstar = strchr(byteorder, 'D');
103+
char *dotstar = strchr(byteorder, 'P');
106104
char *r = strchr(byteorder, 'R');
107105
char *g = strchr(byteorder, 'G');
108106
char *b = strchr(byteorder, 'B');
@@ -117,18 +115,8 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
117115
byteorder_details.byteorder.b = b - byteorder;
118116
byteorder_details.byteorder.w = w ? w - byteorder : 0;
119117
// The dotstar brightness byte is always first (as it goes with the pixel start bits)
120-
// if 'D' is found at the end, adjust byte position
121-
// if 'D' is elsewhere, error out
122-
if (dotstar) {
123-
size_t dotstar_pos = dotstar - byteorder;
124-
if (dotstar_pos == 3) {
125-
byteorder_details.byteorder.b += 1;
126-
byteorder_details.byteorder.g += 1;
127-
byteorder_details.byteorder.r += 1;
128-
byteorder_details.byteorder.w = 0;
129-
} else if (dotstar_pos != 0) {
130-
mp_raise_ValueError(translate("Invalid byteorder string"));
131-
}
118+
if (dotstar && byteorder[0] != 'P') {
119+
mp_raise_ValueError(translate("Invalid byteorder string"));
132120
}
133121
if (byteorder_details.has_white && byteorder_details.is_dotstar)
134122
mp_raise_ValueError(translate("Invalid byteorder string"));
@@ -178,7 +166,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
178166

179167
if (self->byteorder.is_dotstar) {
180168
// Initialize the buffer with the dotstar start bytes.
181-
// Header and end must be setup by caller
169+
// Note: Header and end must be setup by caller
182170
for (uint i = 0; i < self->pixels * 4; i += 4) {
183171
self->buf[i] = DOTSTAR_LED_START_FULL_BRIGHT;
184172
if (two_buffers) {
@@ -268,9 +256,7 @@ void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self) {
268256
mp_obj_t call_show(mp_obj_t self_in) {
269257
mp_obj_t dest[2];
270258
mp_load_method(self_in, MP_QSTR_show, dest);
271-
if (dest[0] == MP_OBJ_NULL)
272-
return mp_const_none;
273-
return mp_call_method_self_n_kw(dest[0], self_in, 0, 0, mp_const_none);
259+
return mp_call_method_n_kw(0, 0, dest);
274260
}
275261

276262
//| .. attribute:: auto_write
@@ -324,7 +310,7 @@ const mp_obj_property_t pixelbuf_pixelbuf_buf_obj = {
324310
//|
325311
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_byteorder(mp_obj_t self_in) {
326312
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
327-
return mp_obj_new_str(self->byteorder.order, strlen(self->byteorder.order));
313+
return self->byteorder.order;
328314
}
329315
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_byteorder_str, pixelbuf_pixelbuf_obj_get_byteorder);
330316

@@ -376,13 +362,15 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
376362
mp_bound_slice_t slice;
377363

378364
if (!mp_seq_get_fast_slice_indexes(self->bytes, index_in, &slice))
365+
// TODO support stepping!!!
379366
mp_raise_NotImplementedError(translate("Only slices with step=1 (aka None) are supported"));
380367
if ((slice.stop * self->pixel_step) > self->bytes)
381368
mp_raise_IndexError(translate("Range out of bounds"));
382369

383370
if (value == MP_OBJ_SENTINEL) { // Get
384371
size_t len = slice.stop - slice.start;
385-
return pixelbuf_get_pixel_array((uint8_t *) self->buf + slice.start, len, &self->byteorder, self->pixel_step, self->byteorder.is_dotstar);
372+
uint8_t *readbuf = self->two_buffers ? self->rawbuf : self->buf;
373+
return pixelbuf_get_pixel_array(readbuf + slice.start, len, &self->byteorder, self->pixel_step, self->byteorder.is_dotstar);
386374
} else { // Set
387375
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
388376

shared-bindings/_pixelbuf/PixelBuf.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ typedef struct {
4444
size_t offset;
4545
uint8_t *rawbuf;
4646
uint8_t *buf;
47-
mp_obj_t write_function;
48-
mp_obj_tuple_t *write_function_args;
4947
bool auto_write;
5048
} pixelbuf_pixelbuf_obj_t;
5149

shared-bindings/_pixelbuf/__init__.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ STATIC const mp_rom_map_elem_t pixelbuf_module_globals_table[] = {
9797
{ MP_ROM_QSTR(MP_QSTR_GBRW), MP_ROM_QSTR(MP_QSTR_GBRW) },
9898
{ MP_ROM_QSTR(MP_QSTR_BRGW), MP_ROM_QSTR(MP_QSTR_BRGW) },
9999
{ MP_ROM_QSTR(MP_QSTR_BGRW), MP_ROM_QSTR(MP_QSTR_BGRW) },
100-
{ MP_ROM_QSTR(MP_QSTR_RGBD), MP_ROM_QSTR(MP_QSTR_RGBD) },
101-
{ MP_ROM_QSTR(MP_QSTR_RBGD), MP_ROM_QSTR(MP_QSTR_RBGD) },
102-
{ MP_ROM_QSTR(MP_QSTR_GRBD), MP_ROM_QSTR(MP_QSTR_GRBD) },
103-
{ MP_ROM_QSTR(MP_QSTR_GBRD), MP_ROM_QSTR(MP_QSTR_GBRD) },
104-
{ MP_ROM_QSTR(MP_QSTR_BRGD), MP_ROM_QSTR(MP_QSTR_BRGD) },
105-
{ MP_ROM_QSTR(MP_QSTR_BGRD), MP_ROM_QSTR(MP_QSTR_BGRD) },
100+
{ MP_ROM_QSTR(MP_QSTR_PRGB), MP_ROM_QSTR(MP_QSTR_PRGB) },
101+
{ MP_ROM_QSTR(MP_QSTR_PRBG), MP_ROM_QSTR(MP_QSTR_PRBG) },
102+
{ MP_ROM_QSTR(MP_QSTR_PGRB), MP_ROM_QSTR(MP_QSTR_PGRB) },
103+
{ MP_ROM_QSTR(MP_QSTR_PGBR), MP_ROM_QSTR(MP_QSTR_PGBR) },
104+
{ MP_ROM_QSTR(MP_QSTR_PBRG), MP_ROM_QSTR(MP_QSTR_PBRG) },
105+
{ MP_ROM_QSTR(MP_QSTR_PBGR), MP_ROM_QSTR(MP_QSTR_PBGR) },
106106
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_wheel_obj) },
107107
};
108108

shared-bindings/_pixelbuf/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct {
4141
pixelbuf_rgbw_t byteorder;
4242
bool has_white;
4343
bool is_dotstar;
44-
char order[5];
44+
mp_obj_t *order;
4545
} pixelbuf_byteorder_details_t;
4646

4747
#endif // CIRCUITPYTHON_PIXELBUF_TYPES_H

0 commit comments

Comments
 (0)