Add measure_text, fixed default palette init
This commit is contained in:
parent
51ad7edb09
commit
3f9f28c60b
|
@ -145,8 +145,8 @@ namespace pimoroni {
|
|||
}, t, p.x, p.y, wrap, scale);
|
||||
}
|
||||
|
||||
void PicoGraphics::measure_text(const std::string &t, uint8_t scale) {
|
||||
bitmap::measure_text(font, t, scale);
|
||||
int32_t PicoGraphics::measure_text(const std::string &t, uint8_t scale) {
|
||||
return bitmap::measure_text(font, t, scale);
|
||||
}
|
||||
|
||||
int32_t orient2d(Point p1, Point p2, Point p3) {
|
||||
|
|
|
@ -89,7 +89,8 @@ namespace pimoroni {
|
|||
|
||||
void default_palette() {
|
||||
for (auto i = 0u; i < 255; i++) {
|
||||
palette[i] = i;
|
||||
palette[i] = ((i & 0b11100000) << 8) | ((i & 0b00011100) << 6) | ((i & 0b00000011) << 3);
|
||||
palette[i] = __builtin_bswap16(palette[i]);
|
||||
}
|
||||
palette_entries = 255;
|
||||
}
|
||||
|
@ -112,7 +113,7 @@ namespace pimoroni {
|
|||
void circle(const Point &p, int32_t r);
|
||||
void character(const char c, const Point &p, uint8_t scale = 2);
|
||||
void text(const std::string &t, const Point &p, int32_t wrap, uint8_t scale = 2);
|
||||
void measure_text(const std::string &t, uint8_t scale = 2);
|
||||
int32_t measure_text(const std::string &t, uint8_t scale = 2);
|
||||
void polygon(const std::vector<Point> &points);
|
||||
void triangle(Point p1, Point p2, Point p3);
|
||||
void line(Point p1, Point p2);
|
||||
|
|
|
@ -15,6 +15,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_rectangle_obj, 1, GenericST7789_rectang
|
|||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_circle_obj, 1, GenericST7789_circle);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_character_obj, 1, GenericST7789_character);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_text_obj, 1, GenericST7789_text);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_measure_text_obj, 1, GenericST7789_measure_text);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_polygon_obj, 2, GenericST7789_polygon);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_triangle_obj, 1, GenericST7789_triangle);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(GenericST7789_line_obj, 1, GenericST7789_line);
|
||||
|
@ -35,6 +36,7 @@ STATIC const mp_rom_map_elem_t GenericST7789_locals_dict_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_circle), MP_ROM_PTR(&GenericST7789_circle_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_character), MP_ROM_PTR(&GenericST7789_character_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&GenericST7789_text_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_measure_text), MP_ROM_PTR(&GenericST7789_measure_text_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_polygon), MP_ROM_PTR(&GenericST7789_polygon_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_triangle), MP_ROM_PTR(&GenericST7789_triangle_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&GenericST7789_line_obj) },
|
||||
|
|
|
@ -511,17 +511,40 @@ mp_obj_t GenericST7789_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||
|
||||
self->st7789->text(t, Point(x, y), wrap, scale);
|
||||
}
|
||||
else if(mp_obj_is_float(text_obj)) {
|
||||
mp_raise_TypeError("can't convert 'float' object to str implicitly");
|
||||
else {
|
||||
mp_raise_TypeError("text: string required");
|
||||
}
|
||||
else if(mp_obj_is_int(text_obj)) {
|
||||
mp_raise_TypeError("can't convert 'int' object to str implicitly");
|
||||
}
|
||||
else if(mp_obj_is_bool(text_obj)) {
|
||||
mp_raise_TypeError("can't convert 'bool' object to str implicitly");
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t GenericST7789_measure_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_text, ARG_scale };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_text, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_scale, MP_ARG_INT, {.u_int = 2} },
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
GenericST7789_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, GenericST7789_obj_t);
|
||||
|
||||
mp_obj_t text_obj = args[ARG_text].u_obj;
|
||||
if(mp_obj_is_str_or_bytes(text_obj)) {
|
||||
GET_STR_DATA_LEN(text_obj, str, str_len);
|
||||
|
||||
std::string t((const char*)str);
|
||||
|
||||
int scale = args[ARG_scale].u_int;
|
||||
|
||||
int width = self->st7789->measure_text(t, scale);
|
||||
|
||||
return mp_obj_new_int(width);
|
||||
}
|
||||
else {
|
||||
mp_raise_TypeError("can't convert object to str implicitly");
|
||||
mp_raise_TypeError("text: string required");
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
|
|
|
@ -26,6 +26,7 @@ extern mp_obj_t GenericST7789_rectangle(size_t n_args, const mp_obj_t *pos_args,
|
|||
extern mp_obj_t GenericST7789_circle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t GenericST7789_character(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t GenericST7789_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t GenericST7789_measure_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t GenericST7789_polygon(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t GenericST7789_triangle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t GenericST7789_line(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
Loading…
Reference in New Issue