From 72d361fa7448521dba883547fad442584b35d5a3 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 17 Feb 2022 11:17:21 +0000 Subject: [PATCH] Badger2040: Add text functions and input --- micropython/modules/badger2040/badger2040.c | 12 +++ micropython/modules/badger2040/badger2040.cpp | 82 ++++++++++++++++++- micropython/modules/badger2040/badger2040.h | 8 +- 3 files changed, 97 insertions(+), 5 deletions(-) diff --git a/micropython/modules/badger2040/badger2040.c b/micropython/modules/badger2040/badger2040.c index 47e81081..cba95d30 100644 --- a/micropython/modules/badger2040/badger2040.c +++ b/micropython/modules/badger2040/badger2040.c @@ -8,14 +8,20 @@ MP_DEFINE_CONST_FUN_OBJ_1(Badger2040_update_obj, Badger2040_update); MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_partial_update_obj, 4, Badger2040_partial_update); MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_led_obj, Badger2040_led); +MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_font_obj, Badger2040_font); MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_pen_obj, Badger2040_pen); MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_thickness_obj, Badger2040_thickness); +MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_pressed_obj, Badger2040_pressed); + MP_DEFINE_CONST_FUN_OBJ_1(Badger2040_clear_obj, Badger2040_clear); 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_glyph_obj, 4, Badger2040_glyph); +MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_text_obj, 4, Badger2040_text); + /***** Binding of Methods *****/ STATIC const mp_rom_map_elem_t Badger2040_locals_dict_table[] = { @@ -24,13 +30,19 @@ STATIC const mp_rom_map_elem_t Badger2040_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_partial_update), MP_ROM_PTR(&Badger2040_partial_update_obj) }, { MP_ROM_QSTR(MP_QSTR_led), MP_ROM_PTR(&Badger2040_led_obj) }, + { MP_ROM_QSTR(MP_QSTR_font), MP_ROM_PTR(&Badger2040_font_obj) }, { MP_ROM_QSTR(MP_QSTR_pen), MP_ROM_PTR(&Badger2040_pen_obj) }, { MP_ROM_QSTR(MP_QSTR_thickness), MP_ROM_PTR(&Badger2040_thickness_obj) }, + { MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&Badger2040_pressed_obj) }, + { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&Badger2040_clear_obj) }, { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&Badger2040_pixel_obj) }, { 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_glyph), MP_ROM_PTR(&Badger2040_glyph_obj) }, + { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&Badger2040_text_obj) }, }; STATIC MP_DEFINE_CONST_DICT(Badger2040_locals_dict, Badger2040_locals_dict_table); diff --git a/micropython/modules/badger2040/badger2040.cpp b/micropython/modules/badger2040/badger2040.cpp index cd2e6301..b58d88cb 100644 --- a/micropython/modules/badger2040/badger2040.cpp +++ b/micropython/modules/badger2040/badger2040.cpp @@ -9,6 +9,21 @@ extern "C" { #include "py/builtin.h" #include "py/mpthread.h" +std::string mp_obj_to_string_r(const mp_obj_t &obj) { + if(mp_obj_is_str_or_bytes(obj)) { + GET_STR_DATA_LEN(obj, str, str_len); + return (const char*)str; + } + else if(mp_obj_is_float(obj)) + mp_raise_TypeError("can't convert 'float' object to str implicitly"); + else if(mp_obj_is_int(obj)) + mp_raise_TypeError("can't convert 'int' object to str implicitly"); + else if(mp_obj_is_bool(obj)) + mp_raise_TypeError("can't convert 'bool' object to str implicitly"); + else + mp_raise_TypeError("can't convert object to str implicitly"); +} + typedef struct _mp_obj_float_t { mp_obj_base_t base; mp_float_t value; @@ -121,7 +136,11 @@ mp_obj_t Badger2040_led(mp_obj_t self_in, mp_obj_t brightness) { return mp_const_none; } -// font +mp_obj_t Badger2040_font(mp_obj_t self_in, mp_obj_t font) { + _Badger2040_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Badger2040_obj_t); + self->badger2040->font(mp_obj_to_string_r(font)); + return mp_const_none; +} mp_obj_t Badger2040_pen(mp_obj_t self_in, mp_obj_t color) { _Badger2040_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Badger2040_obj_t); @@ -135,9 +154,16 @@ mp_obj_t Badger2040_thickness(mp_obj_t self_in, mp_obj_t thickness) { return mp_const_none; } +mp_obj_t Badger2040_pressed(mp_obj_t self_in, mp_obj_t button) { + _Badger2040_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Badger2040_obj_t); + self->badger2040->update_button_states(); + bool state = self->badger2040->pressed(mp_obj_get_int(button)); + return state ? mp_const_true : mp_const_false; +} + // pressed // pressed_to_wake -// wait_for_press +// wait_for_press - implement in terms of MicroPython! // update_button_states // button_states @@ -204,7 +230,55 @@ mp_obj_t Badger2040_rectangle(size_t n_args, const mp_obj_t *pos_args, mp_map_t } // image -// text -// glyph + +mp_obj_t Badger2040_glyph(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_self, ARG_char, ARG_x, ARG_y, ARG_scale }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_char, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_scale, MP_ARG_REQUIRED | MP_ARG_OBJ } + }; + + // 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 c = args[ARG_char].u_int; + int x = args[ARG_x].u_int; + int y = args[ARG_y].u_int; + float scale = mp_obj_get_float(args[ARG_scale].u_obj); + + _Badger2040_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Badger2040_obj_t); + self->badger2040->glyph(c, x, y, scale); + + 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 }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_message, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_scale, MP_ARG_REQUIRED | MP_ARG_OBJ } + }; + + // 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); + + std::string message = mp_obj_to_string_r(args[ARG_message].u_obj); + int x = args[ARG_x].u_int; + int y = args[ARG_y].u_int; + float scale = mp_obj_get_float(args[ARG_scale].u_obj); + + _Badger2040_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Badger2040_obj_t); + self->badger2040->text(message, x, y, scale); + + return mp_const_none; +} } \ No newline at end of file diff --git a/micropython/modules/badger2040/badger2040.h b/micropython/modules/badger2040/badger2040.h index 821ceca4..a600617e 100644 --- a/micropython/modules/badger2040/badger2040.h +++ b/micropython/modules/badger2040/badger2040.h @@ -14,10 +14,16 @@ extern mp_obj_t Badger2040_update(mp_obj_t self_in); extern mp_obj_t Badger2040_partial_update(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t Badger2040_led(mp_obj_t self_in, mp_obj_t brightness); +extern mp_obj_t Badger2040_font(mp_obj_t self_in, mp_obj_t font); extern mp_obj_t Badger2040_pen(mp_obj_t self_in, mp_obj_t color); extern mp_obj_t Badger2040_thickness(mp_obj_t self_in, mp_obj_t thickness); +extern mp_obj_t Badger2040_pressed(mp_obj_t self_in, mp_obj_t button); + extern mp_obj_t Badger2040_clear(mp_obj_t self_in); 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); \ No newline at end of file +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_glyph(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); \ No newline at end of file