fix show and fix step > 1
This commit is contained in:
parent
dc8c27d129
commit
2970680e6a
9
py/obj.c
9
py/obj.c
@ -489,14 +489,19 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
|
||||
|
||||
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
|
||||
mp_obj_type_t *type = mp_obj_get_type(base);
|
||||
mp_obj_t instance = base;
|
||||
// If we got an MP_OBJ_SENTINEL as the type, then we got called by instance_subscr
|
||||
if (type == MP_OBJ_SENTINEL) {
|
||||
instance = ((mp_obj_t *)base)[1];
|
||||
type = mp_obj_get_type(((mp_obj_t *)base)[2]);
|
||||
}
|
||||
if (type->subscr != NULL) {
|
||||
mp_obj_t ret = type->subscr(base, index, value);
|
||||
mp_obj_t ret = type->subscr(instance, index, value);
|
||||
// May have called port specific C code. Make sure it didn't mess up the heap.
|
||||
assert_heap_ok();
|
||||
if (ret != MP_OBJ_NULL) {
|
||||
return ret;
|
||||
}
|
||||
// TODO: call base classes here?
|
||||
}
|
||||
if (value == MP_OBJ_NULL) {
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
|
@ -850,7 +850,8 @@ STATIC mp_obj_t instance_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value
|
||||
meth_args = 3;
|
||||
}
|
||||
if (member[0] == MP_OBJ_SENTINEL) {
|
||||
return mp_obj_subscr(self->subobj[0], index, value);
|
||||
mp_obj_t args[3] = {MP_OBJ_SENTINEL, self_in, self->subobj[0]};
|
||||
return mp_obj_subscr(args, index, value);
|
||||
} else if (member[0] != MP_OBJ_NULL) {
|
||||
mp_obj_t args[3] = {self_in, index, value};
|
||||
// TODO probably need to call mp_convert_member_lookup, and use mp_call_method_n_kw
|
||||
|
@ -230,7 +230,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_obj_set_brightness(mp_obj_t self_in, mp_obj_t
|
||||
if (self->two_buffers)
|
||||
pixelbuf_recalculate_brightness(self);
|
||||
if (self->auto_write)
|
||||
call_show(self_in);
|
||||
call_show(self_in, 'b');
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_pixelbuf_set_brightness_obj, pixelbuf_pixelbuf_obj_set_brightness);
|
||||
@ -253,7 +253,7 @@ void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self) {
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t call_show(mp_obj_t self_in) {
|
||||
mp_obj_t call_show(mp_obj_t self_in, char origin) {
|
||||
mp_obj_t dest[2];
|
||||
mp_load_method(self_in, MP_QSTR_show, dest);
|
||||
return mp_call_method_n_kw(0, 0, dest);
|
||||
@ -356,29 +356,35 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
|
||||
}
|
||||
|
||||
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
|
||||
|
||||
if (0) {
|
||||
#if MICROPY_PY_BUILTINS_SLICE
|
||||
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
|
||||
mp_bound_slice_t slice;
|
||||
|
||||
if (!mp_seq_get_fast_slice_indexes(self->bytes, index_in, &slice))
|
||||
// TODO support stepping!!!
|
||||
mp_raise_NotImplementedError(translate("Only slices with step=1 (aka None) are supported"));
|
||||
mp_seq_get_fast_slice_indexes(self->bytes, index_in, &slice);
|
||||
|
||||
if ((slice.stop * self->pixel_step) > self->bytes)
|
||||
mp_raise_IndexError(translate("Range out of bounds"));
|
||||
if (slice.step < 0)
|
||||
mp_raise_IndexError(translate("Negative step not supported"));
|
||||
|
||||
if (value == MP_OBJ_SENTINEL) { // Get
|
||||
size_t len = slice.stop - slice.start;
|
||||
uint8_t *readbuf = self->two_buffers ? self->rawbuf : self->buf;
|
||||
return pixelbuf_get_pixel_array(readbuf + slice.start, len, &self->byteorder, self->pixel_step, self->byteorder.is_dotstar);
|
||||
return pixelbuf_get_pixel_array(readbuf + slice.start, len, &self->byteorder, self->pixel_step, slice.step, self->byteorder.is_dotstar);
|
||||
} else { // Set
|
||||
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
|
||||
|
||||
if (!(MP_OBJ_IS_TYPE(value, &mp_type_list) || MP_OBJ_IS_TYPE(value, &mp_type_tuple)))
|
||||
mp_raise_ValueError(translate("tuple/list required on RHS"));
|
||||
|
||||
size_t dst_len = slice.stop - slice.start;
|
||||
|
||||
size_t dst_len = (slice.stop - slice.start);
|
||||
dst_len = (slice.stop - slice.start) / slice.step;
|
||||
if (slice.step > 1) {
|
||||
if ((slice.stop - slice.start) % slice.step)
|
||||
dst_len ++;
|
||||
}
|
||||
mp_obj_t *src_objs;
|
||||
size_t num_items;
|
||||
if (MP_OBJ_IS_TYPE(value, &mp_type_list)) {
|
||||
@ -394,16 +400,17 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
|
||||
mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d, got %d)."),
|
||||
dst_len, num_items);
|
||||
|
||||
for (size_t i = slice.start; i < slice.stop; i++) {
|
||||
size_t target_i = slice.start;
|
||||
for (size_t i = slice.start; target_i < slice.stop; i++, target_i += slice.step) {
|
||||
mp_obj_t *item = src_objs[i-slice.start];
|
||||
if (MP_OBJ_IS_TYPE(value, &mp_type_list) || MP_OBJ_IS_TYPE(value, &mp_type_tuple) || MP_OBJ_IS_INT(value)) {
|
||||
pixelbuf_set_pixel(self->buf + (i * self->pixel_step),
|
||||
pixelbuf_set_pixel(self->buf + (target_i * self->pixel_step),
|
||||
self->two_buffers ? self->rawbuf + (i * self->pixel_step) : NULL,
|
||||
self->brightness, item, &self->byteorder, self->byteorder.is_dotstar);
|
||||
}
|
||||
}
|
||||
if (self->auto_write)
|
||||
call_show(self_in);
|
||||
call_show(self_in, 's');
|
||||
return mp_const_none;
|
||||
#else
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
@ -423,7 +430,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
|
||||
pixelbuf_set_pixel(self->buf + offset, self->two_buffers ? self->rawbuf + offset : NULL,
|
||||
self->brightness, value, &self->byteorder, self->byteorder.is_dotstar);
|
||||
if (self->auto_write)
|
||||
call_show(self_in);
|
||||
call_show(self_in, 'i');
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,6 @@ typedef struct {
|
||||
} pixelbuf_pixelbuf_obj_t;
|
||||
|
||||
void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self);
|
||||
mp_obj_t call_show(mp_obj_t self_in);
|
||||
mp_obj_t call_show(mp_obj_t self_in, char origin);
|
||||
|
||||
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H
|
||||
|
@ -94,10 +94,13 @@ void pixelbuf_set_pixel(uint8_t *buf, uint8_t *rawbuf, float brightness, mp_obj_
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t *pixelbuf_get_pixel_array(uint8_t *buf, uint len, pixelbuf_byteorder_details_t *byteorder, uint8_t step, bool dotstar) {
|
||||
mp_obj_t *pixelbuf_get_pixel_array(uint8_t *buf, uint len, pixelbuf_byteorder_details_t *byteorder, uint8_t step, mp_int_t slice_step, bool dotstar) {
|
||||
mp_obj_t elems[len];
|
||||
if (slice_step > 1) {
|
||||
len = len / slice_step;
|
||||
}
|
||||
for (uint i = 0; i < len; i++) {
|
||||
elems[i] = pixelbuf_get_pixel(buf + (i * step), byteorder, dotstar);
|
||||
elems[i] = pixelbuf_get_pixel(buf + ((i * slice_step) * step), byteorder, dotstar);
|
||||
}
|
||||
return mp_obj_new_tuple(len, elems);
|
||||
}
|
||||
|
@ -44,7 +44,7 @@
|
||||
|
||||
void pixelbuf_set_pixel(uint8_t *buf, uint8_t *rawbuf, float brightness, mp_obj_t *item, pixelbuf_byteorder_details_t *byteorder, bool dotstar);
|
||||
mp_obj_t *pixelbuf_get_pixel(uint8_t *buf, pixelbuf_byteorder_details_t *byteorder, bool dotstar);
|
||||
mp_obj_t *pixelbuf_get_pixel_array(uint8_t *buf, uint len, pixelbuf_byteorder_details_t *byteorder, uint8_t step, bool dotstar);
|
||||
mp_obj_t *pixelbuf_get_pixel_array(uint8_t *buf, uint len, pixelbuf_byteorder_details_t *byteorder, uint8_t step, mp_int_t slice_step, bool dotstar);
|
||||
void pixelbuf_set_pixel_int(uint8_t *buf, mp_int_t value, pixelbuf_byteorder_details_t *byteorder);
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user