Skip to content

Commit 55c688c

Browse files
committed
restructure to be subclassable
1 parent 000ae6b commit 55c688c

2 files changed

Lines changed: 15 additions & 50 deletions

File tree

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern const int32_t colorwheel(float pos);
5050
//|
5151
//| :class:`~_pixelbuf.PixelBuf` implements an RGB[W] bytearray abstraction.
5252
//|
53-
//| .. class:: PixelBuf(size, buf, byteorder="BGR", brightness=0, rawbuf=None, offset=0, auto_write=False, write_function=None, write_args=None)
53+
//| .. class:: PixelBuf(size, buf, byteorder="BGR", brightness=0, rawbuf=None, offset=0, auto_write=False)
5454
//|
5555
//| Create a PixelBuf object of the specified size, byteorder, and bits per pixel.
5656
//|
@@ -69,14 +69,11 @@ extern const int32_t colorwheel(float pos);
6969
//| :param ~bytearray rawbuf: Bytearray in which to store raw pixel data (before brightness adjustment)
7070
//| :param ~int offset: Offset from start of buffer (default 0)
7171
//| :param ~bool auto_write: Whether to automatically write pixels (Default False)
72-
//| :param ~callable write_function: (optional) Callable to use to send pixels
73-
//| :param ~list write_args: (optional) Tuple or list of args to pass to ``write_function``. The
74-
//| PixelBuf instance is appended after these args.
7572
//|
7673
STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
7774
mp_arg_check_num(n_args, kw_args, 2, MP_OBJ_FUN_ARGS_MAX, true);
7875
enum { ARG_size, ARG_buf, ARG_byteorder, ARG_brightness, ARG_rawbuf, ARG_offset,
79-
ARG_auto_write, ARG_write_function, ARG_write_args };
76+
ARG_auto_write };
8077
static const mp_arg_t allowed_args[] = {
8178
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_INT },
8279
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -85,8 +82,6 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
8582
{ MP_QSTR_rawbuf, MP_ARG_OBJ, { .u_obj = mp_const_none } },
8683
{ MP_QSTR_offset, MP_ARG_INT, { .u_int = 0 } },
8784
{ MP_QSTR_auto_write, MP_ARG_BOOL, {.u_bool = false} },
88-
{ MP_QSTR_write_function, MP_ARG_OBJ, {.u_obj = mp_const_none} },
89-
{ MP_QSTR_write_args, MP_ARG_OBJ, {.u_obj = mp_const_none} },
9085
};
9186
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
9287
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -155,13 +150,6 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
155150
if (bytes + offset > bufinfo.len)
156151
mp_raise_ValueError_varg(translate("buf is too small. need %d bytes"), bytes + offset);
157152

158-
if (!MP_OBJ_IS_TYPE(args[ARG_write_args].u_obj, &mp_type_list) &&
159-
!MP_OBJ_IS_TYPE(args[ARG_write_args].u_obj, &mp_type_tuple) &&
160-
args[ARG_write_args].u_obj != mp_const_none)
161-
{
162-
mp_raise_ValueError(translate("write_args must be a list, tuple, or None"));
163-
}
164-
165153
// Validation complete, allocate and populate object.
166154
pixelbuf_pixelbuf_obj_t *self = m_new_obj(pixelbuf_pixelbuf_obj_t);
167155

@@ -178,28 +166,6 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
178166
self->pixel_step = effective_bpp;
179167
self->auto_write = args[ARG_auto_write].u_bool;
180168

181-
// Show/auto-write callbacks
182-
self->write_function = args[ARG_write_function].u_obj;
183-
mp_obj_t function_args = args[ARG_write_args].u_obj;
184-
mp_obj_t *src_objs = (mp_obj_t *)&mp_const_none_obj;
185-
size_t num_items = 0;
186-
if (function_args != mp_const_none) {
187-
if (MP_OBJ_IS_TYPE(function_args, &mp_type_list)) {
188-
mp_obj_list_t *t = MP_OBJ_TO_PTR(function_args);
189-
num_items = t->len;
190-
src_objs = t->items;
191-
} else {
192-
mp_obj_tuple_t *l = MP_OBJ_TO_PTR(function_args);
193-
num_items = l->len;
194-
src_objs = l->items;
195-
}
196-
}
197-
self->write_function_args = mp_obj_new_tuple(num_items + 1, NULL);
198-
for (size_t i = 0; i < num_items; i++) {
199-
self->write_function_args->items[i] = src_objs[i];
200-
}
201-
self->write_function_args->items[num_items] = self;
202-
203169
if (args[ARG_brightness].u_obj == mp_const_none) {
204170
self->brightness = 1.0;
205171
} else {
@@ -276,7 +242,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_obj_set_brightness(mp_obj_t self_in, mp_obj_t
276242
if (self->two_buffers)
277243
pixelbuf_recalculate_brightness(self);
278244
if (self->auto_write)
279-
call_write_function(self);
245+
call_show(self_in);
280246
return mp_const_none;
281247
}
282248
MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_pixelbuf_set_brightness_obj, pixelbuf_pixelbuf_obj_set_brightness);
@@ -299,6 +265,14 @@ void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self) {
299265
}
300266
}
301267

268+
mp_obj_t call_show(mp_obj_t self_in) {
269+
mp_obj_t dest[2];
270+
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);
274+
}
275+
302276
//| .. attribute:: auto_write
303277
//|
304278
//| Whether to automatically write the pixels after each update.
@@ -372,23 +346,14 @@ STATIC mp_obj_t pixelbuf_pixelbuf_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
372346

373347
//| .. method:: show()
374348
//|
375-
//| Call the associated write function to display the pixels.
349+
//| Does nothing unless subclassed.
376350
//|
377351

378352
STATIC mp_obj_t pixelbuf_pixelbuf_show(mp_obj_t self_in) {
379-
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
380-
call_write_function(self);
381353
return mp_const_none;
382354
}
383355
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_show_obj, pixelbuf_pixelbuf_show);
384356

385-
void call_write_function(pixelbuf_pixelbuf_obj_t *self) {
386-
// execute function if it's set
387-
if (self->write_function != mp_const_none) {
388-
mp_call_function_n_kw(self->write_function, self->write_function_args->len, 0, self->write_function_args->items);
389-
}
390-
}
391-
392357
//| .. method:: __getitem__(index)
393358
//|
394359
//| Returns the pixel value at the given index.
@@ -450,7 +415,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
450415
}
451416
}
452417
if (self->auto_write)
453-
call_write_function(self);
418+
call_show(self_in);
454419
return mp_const_none;
455420
#else
456421
return MP_OBJ_NULL; // op not supported
@@ -470,7 +435,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
470435
pixelbuf_set_pixel(self->buf + offset, self->two_buffers ? self->rawbuf + offset : NULL,
471436
self->brightness, value, &self->byteorder, self->byteorder.is_dotstar);
472437
if (self->auto_write)
473-
call_write_function(self);
438+
call_show(self_in);
474439
return mp_const_none;
475440
}
476441
}

shared-bindings/_pixelbuf/PixelBuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ typedef struct {
5050
} pixelbuf_pixelbuf_obj_t;
5151

5252
void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self);
53-
void call_write_function(pixelbuf_pixelbuf_obj_t *self);
53+
mp_obj_t call_show(mp_obj_t self_in);
5454

5555
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H

0 commit comments

Comments
 (0)