Badger2040: Add image & icon support

pull/252/head
Phil Howard 2022-02-23 15:24:24 +00:00
rodzic a5485862af
commit 2c27296cc4
5 zmienionych plików z 103 dodań i 8 usunięć

Wyświetl plik

@ -113,17 +113,32 @@ namespace pimoroni {
}
}
void Badger2040::image(const uint8_t *data) {
for(uint32_t y = 0; y < 128; y++) {
for(uint32_t x = 0; x < 296; x++) {
// Display a portion of an image (icon sheet) at dx, dy
void Badger2040::icon(const uint8_t *data, int sheet_width, int icon_size, int index, int dx, int dy) {
image(data, sheet_width, icon_size * index, 0, icon_size, icon_size, dx, dy);
}
// Display an image that fills the screen (286*128)
void Badger2040::image(const uint8_t* data) {
image(data, 296, 0, 0, 296, 128, 0, 0);
}
// Display an image smaller than the screen (sw*sh) at dx, dy
void Badger2040::image(const uint8_t *data, int w, int h, int x, int y) {
image(data, w, 0, 0, w, h, x, y);
}
void Badger2040::image(const uint8_t *data, int stride, int sx, int sy, int dw, int dh, int dx, int dy) {
for(auto y = 0; y < dh; y++) {
for(auto x = 0; x < dw; x++) {
// work out byte offset in source data
uint32_t o = (y * (296 >> 3)) + (x >> 3);
uint32_t o = ((y + sy) * (stride >> 3)) + ((x + sx) >> 3);
// extract bitmask for this pixel
uint32_t bm = 0b10000000 >> (x & 0b111);
uint32_t bm = 0b10000000 >> ((x + sx) & 0b111);
// draw the pixel
uc8151.pixel(x, y, data[o] & bm);
uc8151.pixel(dx + x, dy + y, data[o] & bm);
}
}
}

Wyświetl plik

@ -53,7 +53,11 @@ namespace pimoroni {
void pixel(int32_t x, int32_t y);
void line(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
void rectangle(int32_t x, int32_t y, int32_t w, int32_t h);
void image(const uint8_t *data);
void icon(const uint8_t *data, int sheet_width, int icon_size, int index, int dx, int dy);
void image(const uint8_t* data);
void image(const uint8_t *data, int w, int h, int x, int y);
void image(const uint8_t *data, int stride, int sx, int sy, int dw, int dh, int dx, int dy);
// text (fonts: sans, sans_bold, gothic, cursive_bold, cursive, serif_italic, serif, serif_bold)
const hershey_font_glyph_t* glyph_data(unsigned char c);

Wyświetl plik

@ -20,6 +20,9 @@ MP_DEFINE_CONST_FUN_OBJ_3(Badger2040_pixel_obj, Badger2040_pixel);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_line_obj, 4, Badger2040_line);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_rectangle_obj, 4, Badger2040_rectangle);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_icon_obj, 4, Badger2040_icon);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_image_obj, 2, Badger2040_image);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_text_obj, 4, Badger2040_text);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_glyph_obj, 4, Badger2040_glyph);
@ -48,6 +51,9 @@ STATIC const mp_rom_map_elem_t Badger2040_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&Badger2040_line_obj) },
{ MP_ROM_QSTR(MP_QSTR_rectangle), MP_ROM_PTR(&Badger2040_rectangle_obj) },
{ MP_ROM_QSTR(MP_QSTR_icon), MP_ROM_PTR(&Badger2040_icon_obj) },
{ MP_ROM_QSTR(MP_QSTR_image), MP_ROM_PTR(&Badger2040_image_obj) },
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&Badger2040_text_obj) },
{ MP_ROM_QSTR(MP_QSTR_glyph), MP_ROM_PTR(&Badger2040_glyph_obj) },

Wyświetl plik

@ -280,7 +280,74 @@ mp_obj_t Badger2040_rectangle(size_t n_args, const mp_obj_t *pos_args, mp_map_t
return mp_const_none;
}
// image
mp_obj_t Badger2040_image(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_data, ARG_w, ARG_h, ARG_x, ARG_y };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_w, MP_ARG_INT, {.u_int = 296} },
{ MP_QSTR_h, MP_ARG_INT, {.u_int = 128} },
{ MP_QSTR_x, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_y, MP_ARG_INT, {.u_int = 0} }
};
// Parse args.
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);
int dw = args[ARG_w].u_int;
int dh = args[ARG_h].u_int;
int dx = args[ARG_x].u_int;
int dy = args[ARG_y].u_int;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_data].u_obj, &bufinfo, MP_BUFFER_RW);
if(bufinfo.len < (size_t)(dw * dh / 8)) {
mp_raise_ValueError("image: Supplied buffer is too small!");
}
_Badger2040_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Badger2040_obj_t);
self->badger2040->image((uint8_t *)bufinfo.buf, dw, dh, dx, dy);
return mp_const_none;
}
mp_obj_t Badger2040_icon(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_data, ARG_icon_index, ARG_sheet_size, ARG_icon_size, ARG_dx, ARG_dy };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_icon_index, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sheet_size, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_icon_size, MP_ARG_INT, {.u_int = 64} },
{ MP_QSTR_dx, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_dy, MP_ARG_INT, {.u_int = 0} }
};
// Parse args.
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);
int ssize = args[ARG_sheet_size].u_int;
int isize = args[ARG_icon_size].u_int;
int index = args[ARG_icon_index].u_int;
int dx = args[ARG_dx].u_int;
int dy = args[ARG_dy].u_int;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_data].u_obj, &bufinfo, MP_BUFFER_RW);
if(bufinfo.len < (size_t)(ssize * isize / 8)) {
mp_raise_ValueError("icon: Supplied buffer is too small!");
}
_Badger2040_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Badger2040_obj_t);
self->badger2040->icon((uint8_t *)bufinfo.buf, ssize, isize, index, dx, dy);
return mp_const_none;
}
mp_obj_t Badger2040_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_message, ARG_x, ARG_y, ARG_scale, ARG_rotation };

Wyświetl plik

@ -27,6 +27,9 @@ extern mp_obj_t Badger2040_pixel(mp_obj_t self_in, mp_obj_t x, mp_obj_t y);
extern mp_obj_t Badger2040_line(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t Badger2040_rectangle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t Badger2040_image(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t Badger2040_icon(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t Badger2040_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t Badger2040_glyph(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);