summaryrefslogtreecommitdiff
path: root/shared-bindings/displayio/Group.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2019-02-06 12:13:17 -0800
committerScott Shawcroft <scott@tannewt.org>2019-02-11 20:55:05 -0800
commitc17f147be95e7490e5207c747add2da1cc8b167f (patch)
tree90abe0fad90920415ae4f1e118854cf56e8dfe04 /shared-bindings/displayio/Group.c
parent1a6ad209439e0c8b0d01701b76195f9761949e31 (diff)
A variety of displayio improvements
This changes a number of things in displayio: * Introduces BuiltinFont and Glyph so the built in font can be used by libraries. For boards with a font it is available as board.TERMINAL_FONT. Fixes #1172 * Remove _load_row from Bitmap in favor of bitmap[] access. Index can be x/y tuple or overall index. Fixes #1191 * Add width and height properties to Bitmap. * Add insert and [] access to Group. Fixes #1518 * Add index param to pop on Group. * Terminal no longer takes unicode character info. It takes a BuiltinFont instead. * Fix Terminal's handling of [###D vt100 commands used when up arrowing into repl history. * Add x and y positions to Group plus scale as well. * Add bitmap accessor for BuiltinFont
Diffstat (limited to 'shared-bindings/displayio/Group.c')
-rw-r--r--shared-bindings/displayio/Group.c205
1 files changed, 193 insertions, 12 deletions
diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c
index 253812e00..f8013050d 100644
--- a/shared-bindings/displayio/Group.c
+++ b/shared-bindings/displayio/Group.c
@@ -41,57 +41,236 @@
//|
//| Manage a group of sprites and groups and how they are inter-related.
//|
-//| .. warning:: This will be changed before 4.0.0. Consider it very experimental.
-//|
-//| .. class:: Group(*, max_size=4)
+//| .. class:: Group(*, max_size=4, scale=1)
//|
//| Create a Group of a given size.
//|
//| :param int max_size: The maximum group size.
//|
STATIC mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_max_size };
+ enum { ARG_max_size, ARG_scale };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_max_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 4} },
+ { MP_QSTR_scale, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_int_t max_size = args[ARG_max_size].u_int;
if (max_size < 1) {
- mp_raise_ValueError(translate("Group must have size at least 1"));
+ mp_raise_ValueError_varg(translate("Group must have %q at least 1"), MP_QSTR_max_size);
+ }
+
+ mp_int_t scale = args[ARG_scale].u_int;
+ if (scale < 1) {
+ mp_raise_ValueError_varg(translate("Group must have %q at least 1"), MP_QSTR_scale);
}
displayio_group_t *self = m_new_obj(displayio_group_t);
self->base.type = &displayio_group_type;
- common_hal_displayio_group_construct(self, max_size);
+ common_hal_displayio_group_construct(self, max_size, scale);
return MP_OBJ_FROM_PTR(self);
}
+// Helper to ensure we have the native super class instead of a subclass.
+static displayio_group_t* native_group(mp_obj_t group_obj) {
+ mp_obj_t native_group = mp_instance_cast_to_native_base(group_obj, &displayio_group_type);
+ assert(native_group == MP_OBJ_NULL);
+ return MP_OBJ_TO_PTR(native_group);
+}
+
+//| .. attribute:: scale
+//|
+//| Scales each pixel within the Group in both directions. For example, when scale=2 each pixel
+//| will be represented by 2x2 pixels.
+//|
+STATIC mp_obj_t displayio_group_obj_get_scale(mp_obj_t self_in) {
+ displayio_group_t *self = native_group(self_in);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_scale(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_scale_obj, displayio_group_obj_get_scale);
+
+STATIC mp_obj_t displayio_group_obj_set_scale(mp_obj_t self_in, mp_obj_t scale_obj) {
+ displayio_group_t *self = native_group(self_in);
+
+ mp_int_t scale = mp_obj_get_int(scale_obj);
+ if (scale < 1) {
+ mp_raise_ValueError_varg(translate("Group must have %q at least 1"), MP_QSTR_scale);
+ }
+ common_hal_displayio_group_set_scale(self, scale);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_scale_obj, displayio_group_obj_set_scale);
+
+const mp_obj_property_t displayio_group_scale_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&displayio_group_get_scale_obj,
+ (mp_obj_t)&displayio_group_set_scale_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| .. attribute:: x
+//|
+//| X position of the Group in the parent.
+//|
+STATIC mp_obj_t displayio_group_obj_get_x(mp_obj_t self_in) {
+ displayio_group_t *self = native_group(self_in);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_x(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_x_obj, displayio_group_obj_get_x);
+
+STATIC mp_obj_t displayio_group_obj_set_x(mp_obj_t self_in, mp_obj_t x_obj) {
+ displayio_group_t *self = native_group(self_in);
+
+ mp_int_t x = mp_obj_get_int(x_obj);
+ common_hal_displayio_group_set_x(self, x);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_x_obj, displayio_group_obj_set_x);
+
+const mp_obj_property_t displayio_group_x_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&displayio_group_get_x_obj,
+ (mp_obj_t)&displayio_group_set_x_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| .. attribute:: y
+//|
+//| Y position of the Group in the parent.
+//|
+STATIC mp_obj_t displayio_group_obj_get_y(mp_obj_t self_in) {
+ displayio_group_t *self = native_group(self_in);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_y(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_y_obj, displayio_group_obj_get_y);
+
+STATIC mp_obj_t displayio_group_obj_set_y(mp_obj_t self_in, mp_obj_t y_obj) {
+ displayio_group_t *self = native_group(self_in);
+
+ mp_int_t y = mp_obj_get_int(y_obj);
+ common_hal_displayio_group_set_y(self, y);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_y_obj, displayio_group_obj_set_y);
+
+const mp_obj_property_t displayio_group_y_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&displayio_group_get_y_obj,
+ (mp_obj_t)&displayio_group_set_y_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
//| .. method:: append(layer)
//|
//| Append a layer to the group. It will be drawn above other layers.
//|
STATIC mp_obj_t displayio_group_obj_append(mp_obj_t self_in, mp_obj_t layer) {
displayio_group_t *self = MP_OBJ_TO_PTR(self_in);
- common_hal_displayio_group_append(self, layer);
+ common_hal_displayio_group_insert(self, common_hal_displayio_group_get_len(self), layer);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_append_obj, displayio_group_obj_append);
-//| .. method:: pop()
+//| .. method:: insert(index, layer)
//|
-//| Remove the last item and return it.
+//| Insert a layer into the group.
//|
-STATIC mp_obj_t displayio_group_obj_pop(mp_obj_t self_in) {
+STATIC mp_obj_t displayio_group_obj_insert(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t layer) {
displayio_group_t *self = MP_OBJ_TO_PTR(self_in);
- return common_hal_displayio_group_pop(self);
+ size_t index = mp_get_index(&displayio_group_type, common_hal_displayio_group_get_len(self), index_obj, false);
+ common_hal_displayio_group_insert(self, index, layer);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_3(displayio_group_insert_obj, displayio_group_obj_insert);
+
+//| .. method:: pop(i=-1)
+//|
+//| Remove the ith item and return it.
+//|
+STATIC mp_obj_t displayio_group_obj_pop(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_i };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_i, MP_ARG_INT, {.u_int = -1} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ displayio_group_t *self = MP_OBJ_TO_PTR(pos_args[0]);
+
+ size_t index = mp_get_index(&displayio_group_type,
+ common_hal_displayio_group_get_len(self),
+ MP_OBJ_NEW_SMALL_INT(args[ARG_i].u_int),
+ false);
+ return common_hal_displayio_group_pop(self, index);
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_pop_obj, 1, displayio_group_obj_pop);
+
+//| .. method:: __len__()
+//|
+//| Returns the number of layers in a Group
+//|
+STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
+ displayio_group_t *self = MP_OBJ_TO_PTR(self_in);
+ uint16_t len = common_hal_displayio_group_get_len(self);
+ switch (op) {
+ case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
+ case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
+ default: return MP_OBJ_NULL; // op not supported
+ }
+}
+
+//| .. method:: __getitem__(index)
+//|
+//| Returns the value at the given index.
+//|
+//| This allows you to::
+//|
+//| print(group[0])
+//|
+//| .. method:: __setitem__(index, value)
+//|
+//| Sets the value at the given index.
+//|
+//| This allows you to::
+//|
+//| group[0] = sprite
+//|
+//| .. method:: __delitem__(index)
+//|
+//| Deletes the value at the given index.
+//|
+//| This allows you to::
+//|
+//| del group[0]
+//|
+STATIC mp_obj_t group_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) {
+ displayio_group_t *self = MP_OBJ_TO_PTR(self_in);
+
+ if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) {
+ mp_raise_NotImplementedError(translate("Slices not supported"));
+ } else {
+ size_t index = mp_get_index(&displayio_group_type, common_hal_displayio_group_get_len(self), index_obj, false);
+
+ if (value == MP_OBJ_SENTINEL) {
+ // load
+ return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get(self, index));
+ } else if (value == mp_const_none) {
+ common_hal_displayio_group_pop(self, index);
+ } else {
+ common_hal_displayio_group_set(self, index, value);
+ }
+ }
+ return mp_const_none;
}
-MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_pop_obj, displayio_group_obj_pop);
STATIC const mp_rom_map_elem_t displayio_group_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&displayio_group_scale_obj) },
+ { MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&displayio_group_x_obj) },
+ { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&displayio_group_y_obj) },
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&displayio_group_append_obj) },
+ { MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&displayio_group_insert_obj) },
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&displayio_group_pop_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_group_locals_dict, displayio_group_locals_dict_table);
@@ -100,5 +279,7 @@ const mp_obj_type_t displayio_group_type = {
{ &mp_type_type },
.name = MP_QSTR_Group,
.make_new = displayio_group_make_new,
+ .subscr = group_subscr,
+ .unary_op = group_unary_op,
.locals_dict = (mp_obj_dict_t*)&displayio_group_locals_dict,
};