Skip to content

Commit 2569478

Browse files
committed
feat(repo): support MicroPython
1 parent 567f0b4 commit 2569478

24 files changed

+248
-47
lines changed

esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

examples/LVGL/v8/Porting/esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

examples/LVGL/v8/Rotation/esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

examples/Panel/PanelTest/esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

examples/PlatformIO/src/esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

examples/SquareLine/v8/Porting/esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

examples/SquareLine/v8/WiFiClock/esp_panel_board_custom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// *INDENT-OFF*
1010

1111
/* Set to 1 if using a custom board */
12-
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (1) // 0/1
12+
#define ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM (0) // 0/1
1313

1414
#if ESP_PANEL_BOARD_DEFAULT_USE_CUSTOM
1515

micropython.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is to be given as "make USER_C_MODULES=..." when building Micropython port
2+
3+
add_library(usermod_esp_display_panel INTERFACE)
4+
5+
# Find all source files in the `src` directory.
6+
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
7+
file(GLOB_RECURSE SRCS_C ${SRC_DIR}/*.c)
8+
file(GLOB_RECURSE SRCS_CXX ${SRC_DIR}/*.cpp)
9+
10+
# Find all source files in the `micropython` directory.
11+
set(MPY_DIR ${CMAKE_CURRENT_LIST_DIR}/mpy_support)
12+
file(GLOB_RECURSE MPY_C ${MPY_DIR}/*.c)
13+
file(GLOB_RECURSE MPY_CXX ${MPY_DIR}/*.cpp)
14+
15+
# Add source files to the library.
16+
target_sources(usermod_esp_display_panel INTERFACE ${SRCS_C} ${SRCS_CXX} ${MPY_C} ${MPY_CXX})
17+
18+
# Add the current directory as an include directory.
19+
target_include_directories(usermod_esp_display_panel INTERFACE ${SRC_DIR} ${MPY_DIR})
20+
21+
# Add compile options. Since the target is not created by `idf_component_register()`, we need to add the `ESP_PLATFORM` define manually.
22+
target_compile_options(usermod_esp_display_panel INTERFACE -Wno-missing-field-initializers -DESP_PLATFORM)
23+
24+
# Link INTERFACE library to the usermod target.
25+
target_link_libraries(usermod INTERFACE usermod_esp_display_panel)

mpy_support/esp_panel_mp_board.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "esp_panel_utils.h"
7+
#include "board/esp_panel_board.hpp"
8+
#include "esp_panel_mp_types.h"
9+
#include "esp_panel_mp_board.h"
10+
11+
namespace esp_panel {
12+
13+
/**
14+
* MicroPython Wrappers
15+
*
16+
*/
17+
// Object
18+
struct MP_Board {
19+
mp_obj_base_t base;
20+
std::shared_ptr<Board> board = nullptr;
21+
};
22+
23+
static mp_obj_t board_del(mp_obj_t self_in)
24+
{
25+
MP_Board *self = static_cast<MP_Board *>(MP_OBJ_TO_PTR(self_in));
26+
27+
self->board = nullptr;
28+
29+
return mp_const_none;
30+
}
31+
static MP_DEFINE_CONST_FUN_OBJ_1_CXX(board_del_func_obj, board_del);
32+
33+
static mp_obj_t board_init(mp_obj_t self_in)
34+
{
35+
MP_Board *self = static_cast<MP_Board *>(MP_OBJ_TO_PTR(self_in));
36+
37+
return mp_obj_new_bool(self->board->init());
38+
}
39+
static MP_DEFINE_CONST_FUN_OBJ_1_CXX(board_init_func_obj, board_init);
40+
41+
static mp_obj_t board_begin(mp_obj_t self_in)
42+
{
43+
MP_Board *self = static_cast<MP_Board *>(MP_OBJ_TO_PTR(self_in));
44+
45+
return mp_obj_new_bool(self->board->begin());
46+
}
47+
static MP_DEFINE_CONST_FUN_OBJ_1_CXX(board_begin_func_obj, board_begin);
48+
49+
static mp_obj_t board_deinit(mp_obj_t self_in)
50+
{
51+
MP_Board *self = static_cast<MP_Board *>(MP_OBJ_TO_PTR(self_in));
52+
53+
return mp_obj_new_bool(self->board->del());
54+
}
55+
static MP_DEFINE_CONST_FUN_OBJ_1_CXX(board_deinit_func_obj, board_deinit);
56+
57+
static mp_obj_t board_color_bar_test(mp_obj_t self_in)
58+
{
59+
MP_Board *self = static_cast<MP_Board *>(MP_OBJ_TO_PTR(self_in));
60+
61+
return mp_obj_new_bool(self->board->getLcd()->colorBarTest(480, 480));
62+
}
63+
static MP_DEFINE_CONST_FUN_OBJ_1_CXX(board_color_bar_test_func_obj, board_color_bar_test);
64+
65+
// Local dict
66+
static const mp_rom_map_elem_t board_locals_dict_table[] = {
67+
{ MP_ROM_QSTR(MP_QSTR__del__), MP_ROM_PTR(&board_del_func_obj) },
68+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&board_init_func_obj) },
69+
{ MP_ROM_QSTR(MP_QSTR_begin), MP_ROM_PTR(&board_begin_func_obj) },
70+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&board_deinit_func_obj) },
71+
{ MP_ROM_QSTR(MP_QSTR_color_bar_test), MP_ROM_PTR(&board_color_bar_test_func_obj) },
72+
};
73+
static MP_DEFINE_CONST_DICT(board_locals_dict, board_locals_dict_table);
74+
75+
// Constructor
76+
static mp_obj_t board_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
77+
{
78+
MP_Board *self = mp_obj_malloc(MP_Board, &MP_Boardype);
79+
self->board = esp_utils::make_shared<Board>();
80+
81+
return MP_OBJ_FROM_PTR(self);
82+
}
83+
84+
// Print
85+
static void board_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
86+
{
87+
mp_printf(print, "Board");
88+
}
89+
90+
} // namespace esp_panel
91+
92+
// Type
93+
MP_DEFINE_CONST_OBJ_TYPE(
94+
MP_Boardype,
95+
MP_QSTR_Board,
96+
MP_TYPE_FLAG_NONE,
97+
make_new, (const void *)esp_panel::board_make_new,
98+
print, (const void *)esp_panel::board_print,
99+
locals_dict, &esp_panel::board_locals_dict
100+
);

mpy_support/esp_panel_mp_board.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
#include "py/runtime.h"
13+
#include "py/obj.h"
14+
15+
extern const mp_obj_type_t esp_panel_mp_board_type;
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

mpy_support/esp_panel_mp_module.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "py/runtime.h"
7+
#include "esp_panel_mp_board.h"
8+
9+
// Define all attributes of the module.
10+
// Table entries are key/value pairs of the attribute name (a string)
11+
// and the MicroPython object reference.
12+
// All identifiers and strings are written as MP_QSTR_xxx and will be
13+
// optimized to word-sized integers by the build system (interned strings).
14+
static const mp_rom_map_elem_t module_globals_table[] = {
15+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp_panel) },
16+
{ MP_ROM_QSTR(MP_QSTR_Board), MP_ROM_PTR(&esp_panel_mp_board_type) },
17+
};
18+
static MP_DEFINE_CONST_DICT(module_globals, module_globals_table);
19+
20+
// Define module object.
21+
const mp_obj_module_t esp_panel_mp_module = {
22+
.base = { &mp_type_module },
23+
.globals = (mp_obj_dict_t *) &module_globals,
24+
};
25+
26+
// Register the module to make it available in Python.
27+
MP_REGISTER_MODULE(MP_QSTR_esp_panel, esp_panel_mp_module);

mpy_support/esp_panel_mp_types.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
#include "py/obj.h"
13+
14+
#define MP_DEFINE_CONST_FUN_OBJ_1_CXX(obj_name, fun_name) \
15+
const mp_obj_fun_builtin_fixed_t obj_name = { .base = &mp_type_fun_builtin_1, .fun = {._1 = fun_name }}
16+
17+
#define MP_DEFINE_CONST_FUN_OBJ_2_CXX(obj_name, fun_name) \
18+
const mp_obj_fun_builtin_fixed_t obj_name = { .base = &mp_type_fun_builtin_2, .fun = {._2 = fun_name }}
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif

src/board/esp_panel_board.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Board::Board():
2424
_flags.use_default_config = 1;
2525
#if ESP_PANEL_BOARD_BACKLIGHT_TYPE == ESP_PANEL_BACKLIGHT_TYPE_CUSTOM
2626
using BacklightConfig = drivers::BacklightCustom::Config;
27-
BacklightConfig &config = std::get<BacklightConfig>(_config.backlight.variant_config);
27+
BacklightConfig &config = std::get<BacklightConfig>(_config.backlight.config);
2828
config.user_data = this;
2929
#endif // ESP_PANEL_BOARD_BACKLIGHT_TYPE
3030
}

src/drivers/backlight/esp_panel_backlight_base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Backlight {
2222
*
2323
*/
2424
struct Attributes {
25-
std::string name = "Unknown"; /*<! The backlight device name. Default is `"Unknown"` */
25+
const char *name = nullptr; /*<! The backlight device name. Default is `nullptr` */
2626
};
2727

2828
/**

src/drivers/bus/esp_panel_bus_base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Bus {
2525
*/
2626
struct Attributes {
2727
int type = -1; /*<! The bus type. Default is `-1` */
28-
std::string name = "Unknown"; /*<! The bus name. Default is `"Unknown"` */
28+
const char *name = nullptr; /*<! The bus name. Default is `nullptr` */
2929
};
3030

3131
/**

src/drivers/lcd/esp_panel_lcd.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ namespace esp_panel::drivers {
1414
{LCD_Factory::Conrtoller::controller, LCD_ ##controller::ATTRIBUTES_DEFAULT.name}
1515
#define CASE_CONTROLLER(controller) \
1616
case Conrtoller::controller: \
17+
{ \
1718
ESP_UTILS_CHECK_EXCEPTION_RETURN( \
1819
(device = esp_utils::make_shared<LCD_ ## controller>(bus, config)), nullptr, "Create %s failed", \
1920
getControllerNameString(controller).c_str() \
2021
); \
21-
break
22+
break; \
23+
} \
2224

2325
const std::map<LCD_Factory::Conrtoller, std::string> LCD_Factory::_controller_name_map = {
2426
ITEM_CONTROLLER(AXS15231B),

src/drivers/lcd/esp_panel_lcd_base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class LCD {
9292
bool disable_display_on_off = 0;
9393
uint8_t x_coord_align = 1;
9494
uint8_t y_coord_align = 1;
95-
std::string name = "Unknown";
95+
const char *name = nullptr;
9696
};
9797

9898
struct State {

src/drivers/touch/esp_panel_touch_base.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ bool Touch::del(void)
5353
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
5454

5555
if (checkIsBegun()) {
56-
ESP_UTILS_CHECK_ERROR_RETURN(
57-
esp_lcd_touch_del(touch_handle), false, "Delete touch panel(@%p) failed", touch_handle
58-
);
56+
// ESP_UTILS_CHECK_ERROR_RETURN(
57+
// esp_lcd_touch_del(touch_handle), false, "Delete touch panel(@%p) failed", touch_handle
58+
// );
5959
ESP_UTILS_LOGD("Touch panel(@%p) deleted", touch_handle);
6060
touch_handle = nullptr;
6161
}

src/drivers/touch/esp_panel_touch_base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Touch {
8787
};
8888

8989
struct Attributes {
90-
std::string name = "Unknown";
90+
const char *name = nullptr;
9191
};
9292

9393
struct State {

src/drivers/touch/port/esp_lcd_touch_ft5x06.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ esp_err_t esp_lcd_touch_new_i2c_ft5x06(const esp_lcd_panel_io_handle_t io, const
109109
assert(out_touch != NULL);
110110

111111
/* Prepare main structure */
112-
esp_lcd_touch_handle_t esp_lcd_touch_ft5x06 = heap_caps_calloc(1, sizeof(esp_lcd_touch_t), MALLOC_CAP_DEFAULT);
112+
// Use `calloc` instead of `heap_caps_calloc` for MicroPython compatibility
113+
esp_lcd_touch_handle_t esp_lcd_touch_ft5x06 = calloc(1, sizeof(esp_lcd_touch_t));
113114
ESP_GOTO_ON_FALSE(esp_lcd_touch_ft5x06, ESP_ERR_NO_MEM, err, TAG, "no mem for FT5x06 controller");
114115

115116
/* Communication interface */

src/drivers/touch/port/esp_lcd_touch_gt911.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ esp_err_t esp_lcd_touch_new_i2c_gt911(const esp_lcd_panel_io_handle_t io, const
7171
assert(out_touch != NULL);
7272

7373
/* Prepare main structure */
74-
esp_lcd_touch_handle_t esp_lcd_touch_gt911 = heap_caps_calloc(1, sizeof(esp_lcd_touch_t), MALLOC_CAP_DEFAULT);
74+
// Use `calloc` instead of `heap_caps_calloc` for MicroPython compatibility
75+
esp_lcd_touch_handle_t esp_lcd_touch_gt911 = calloc(1, sizeof(esp_lcd_touch_t));
7576
ESP_GOTO_ON_FALSE(esp_lcd_touch_gt911, ESP_ERR_NO_MEM, err, TAG, "no mem for GT911 controller");
7677

7778
/* Communication interface */

src/drivers/touch/port/esp_lcd_touch_stmpe610.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ esp_err_t esp_lcd_touch_new_spi_stmpe610(const esp_lcd_panel_io_handle_t io, con
9999
assert(out_touch != NULL);
100100

101101
/* Prepare main structure */
102-
esp_lcd_touch_handle_t esp_lcd_touch_stmpe610 = heap_caps_calloc(1, sizeof(esp_lcd_touch_t), MALLOC_CAP_DEFAULT);
102+
// Use `calloc` instead of `heap_caps_calloc` for MicroPython compatibility
103+
esp_lcd_touch_handle_t esp_lcd_touch_stmpe610 = calloc(1, sizeof(esp_lcd_touch_t));
103104
ESP_GOTO_ON_FALSE(esp_lcd_touch_stmpe610, ESP_ERR_NO_MEM, err, TAG, "no mem for STMPE610 controller");
104105

105106
/* Communication interface */

src/drivers/touch/port/esp_lcd_touch_tt21100.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ esp_err_t esp_lcd_touch_new_i2c_tt21100(const esp_lcd_panel_io_handle_t io, cons
6060
assert(out_touch != NULL);
6161

6262
/* Prepare main structure */
63-
esp_lcd_touch_handle_t esp_lcd_touch_tt21100 = heap_caps_calloc(1, sizeof(esp_lcd_touch_t), MALLOC_CAP_DEFAULT);
63+
// Use `calloc` instead of `heap_caps_calloc` for MicroPython compatibility
64+
esp_lcd_touch_handle_t esp_lcd_touch_tt21100 = calloc(1, sizeof(esp_lcd_touch_t));
6465
ESP_GOTO_ON_FALSE(esp_lcd_touch_tt21100, ESP_ERR_NO_MEM, err, TAG, "no mem for TT21100 controller");
6566

6667
/* Communication interface */

0 commit comments

Comments
 (0)