diff --git a/components/badge23/display.c b/components/badge23/display.c index 0ad3e62c6e228240294b0bd757bb83e031dfc3b1..93a5454b046ae0a4cc912f3d135869a0d10b6bc7 100644 --- a/components/badge23/display.c +++ b/components/badge23/display.c @@ -13,6 +13,10 @@ #include "badge23/scope.h" #include "esp_system.h" +#include "../../usermodule/uctx/uctx/ctx.h" + +Ctx *the_ctx = NULL; + uint16_t *pixels; typedef struct leds_cfg { @@ -28,6 +32,14 @@ static void _display_init() { // GC9A01_Screen_Load(0,0,240,240,pixels); GC9A01_Update(); + the_ctx = ctx_new_for_framebuffer( + ScreenBuff, + GC9A01_Width, + GC9A01_Height, + GC9A01_Width * 2, + CTX_FORMAT_RGB565_BYTESWAPPED + ); + /* display_queue = xQueueCreate(1, sizeof(display_cfg_t)); TaskHandle_t handle; diff --git a/components/badge23/include/badge23/display.h b/components/badge23/include/badge23/display.h index d923a061e8d10ebf6b215e920d578150bc24d93d..1fc0fca10b482bef8bbd870cb5d819249d3206cf 100644 --- a/components/badge23/include/badge23/display.h +++ b/components/badge23/include/badge23/display.h @@ -2,6 +2,7 @@ #include <stdbool.h> #include <stdint.h> +#include "../../usermodule/uctx/uctx/ctx.h" void display_init(); void display_draw_scope(); @@ -9,3 +10,5 @@ void display_update(); void display_draw_pixel(uint8_t x, uint8_t y, uint16_t col); uint16_t display_get_pixel(uint8_t x, uint8_t y); void display_fill(uint16_t col); + +extern Ctx *the_ctx; diff --git a/usermodule/mp_hardware.c b/usermodule/mp_hardware.c index 70522b900b83d984a9bcdf5f6f76d9a001ea6b0b..bcb37bff414bc55fe85f9bbb7863424f08279e8e 100644 --- a/usermodule/mp_hardware.c +++ b/usermodule/mp_hardware.c @@ -19,6 +19,9 @@ #include "badge23/espan.h" #include "badge23_hwconfig.h" +mp_obj_t mp_ctx_from_ctx(Ctx *ctx); +mp_obj_t mp_ctx = NULL; + STATIC mp_obj_t mp_init_done(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int(hardware_is_initialized()); } @@ -132,6 +135,15 @@ STATIC mp_obj_t mp_version(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_version_obj, mp_version); +STATIC mp_obj_t mp_get_ctx(size_t n_args, const mp_obj_t *args) { + if (mp_ctx == NULL) { + mp_ctx = mp_ctx_from_ctx(the_ctx); + } + return mp_ctx; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_ctx_obj, 0, 0, mp_get_ctx); + + STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio) }, { MP_ROM_QSTR(MP_QSTR_init_done), MP_ROM_PTR(&mp_init_done_obj) }, @@ -149,6 +161,7 @@ STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_display_get_pixel), MP_ROM_PTR(&mp_display_get_pixel_obj) }, { MP_ROM_QSTR(MP_QSTR_display_fill), MP_ROM_PTR(&mp_display_fill_obj) }, { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_version_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_ctx), MP_ROM_PTR(&mp_get_ctx_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_hardware_globals, mp_module_hardware_globals_table); diff --git a/usermodule/uctx/uctx/uctx.c b/usermodule/uctx/uctx/uctx.c index 77781d3df97b30ff71ac05a91dc9ca7b42c77cdf..cce8f8140e45659d9e242e63d56ccf83dd628bf3 100644 --- a/usermodule/uctx/uctx/uctx.c +++ b/usermodule/uctx/uctx/uctx.c @@ -99,39 +99,6 @@ /* we keep the ctx implementation here, this compilation taget changes less * than the micropython target */ -#define CTX_EXTERNAL_MALLOC - -static inline void *ctx_malloc (size_t size) -{ - return m_malloc (size); -} - -static inline void *ctx_calloc (size_t nmemb, size_t size) -{ - size_t byte_size = nmemb * size; - char *ret = (char *)m_malloc(byte_size); - for (size_t i = 0; i < byte_size; i++) - ret[i] = 0; - return ret; -} - -static inline void *ctx_realloc (void *ptr, size_t old_size, size_t new_size) -{ -#if MICROPY_MALLOC_USES_ALLOCATED_SIZE - return m_realloc(ptr, old_size, new_size); -#else - return m_realloc(ptr, new_size); -#endif -} - -static inline void ctx_free (void *ptr) -{ -#if MICROPY_MALLOC_USES_ALLOCATED_SIZE - return m_free(ptr, 0); // XXX ! -#else - return m_free(ptr); -#endif -} #define CTX_STATIC_FONT(font) \ ctx_load_font_ctx(ctx_font_##font##_name, \ @@ -1074,6 +1041,13 @@ static int mp_ctx_update_fb (Ctx *ctx, void *user_data) return 0; } +mp_obj_t mp_ctx_from_ctx(Ctx *ctx) { + mp_ctx_obj_t *o = m_new_obj(mp_ctx_obj_t); + o->base.type = &mp_ctx_type; + o->ctx = ctx; + return MP_OBJ_FROM_PTR(o); +} + static mp_obj_t mp_ctx_make_new( const mp_obj_type_t *type, size_t n_args,