allow tuple or list for Palette color

This commit is contained in:
Dan Halbert 2020-02-03 22:09:53 -05:00
parent 67440acd3e
commit a4ebd2f7c1
2 changed files with 13 additions and 2 deletions

View File

@ -10,6 +10,9 @@ INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE LONGINT_IMPL = NONE
CIRCUITPY_SMALL_BUILD = 1 CIRCUITPY_SMALL_BUILD = 1
# TODO: Turn off analogio for now for space reasons, but restore it
# when frozen module gets smaller.
CIRCUITPY_ANALOGIO = 0
CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_BITBANGIO = 0 CIRCUITPY_BITBANGIO = 0
CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_FREQUENCYIO = 0

View File

@ -86,7 +86,8 @@ STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
//| Sets the pixel color at the given index. The index should be an integer in the range 0 to color_count-1. //| Sets the pixel color at the given index. The index should be an integer in the range 0 to color_count-1.
//| //|
//| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value). //| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value).
//| Value can be an int, bytes (3 bytes (RGB) or 4 bytes (RGB + pad byte)), or bytearray. //| Value can be an int, bytes (3 bytes (RGB) or 4 bytes (RGB + pad byte)), bytearray,
//| or a tuple or list of 3 integers.
//| //|
//| This allows you to:: //| This allows you to::
//| //|
@ -94,6 +95,7 @@ STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
//| palette[1] = b'\xff\xff\x00' # set using 3 bytes //| palette[1] = b'\xff\xff\x00' # set using 3 bytes
//| palette[2] = b'\xff\xff\x00\x00' # set using 4 bytes //| palette[2] = b'\xff\xff\x00\x00' # set using 4 bytes
//| palette[3] = bytearray(b'\x00\x00\xFF') # set using a bytearay of 3 or 4 bytes //| palette[3] = bytearray(b'\x00\x00\xFF') # set using a bytearay of 3 or 4 bytes
//| palette[4] = (10, 20, 30) # set using a tuple of 3 integers
//| //|
STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
if (value == MP_OBJ_NULL) { if (value == MP_OBJ_NULL) {
@ -111,6 +113,12 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_palette_get_color(self, index)); return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_palette_get_color(self, index));
} }
// Convert a tuple or list to a bytearray.
if (MP_OBJ_IS_TYPE(value, &mp_type_tuple) ||
MP_OBJ_IS_TYPE(value, &mp_type_list)) {
value = mp_type_bytes.make_new(&mp_type_bytes, 1, &value, NULL);
}
uint32_t color; uint32_t color;
mp_int_t int_value; mp_int_t int_value;
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
@ -130,7 +138,7 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val
} }
color = int_value; color = int_value;
} else { } else {
mp_raise_TypeError(translate("color buffer must be a buffer or int")); mp_raise_TypeError(translate("color buffer must be a buffer, tuple, list, or int"));
} }
common_hal_displayio_palette_set_color(self, index, color); common_hal_displayio_palette_set_color(self, index, color);
return mp_const_none; return mp_const_none;