examples/embedding-full: Add example user C module.

This works out of the box in the embed port.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
pull/11430/head
Christian Walther 2023-05-03 22:45:43 +02:00
rodzic d3656e97fe
commit 605493d508
5 zmienionych plików z 32 dodań i 1 usunięć

Wyświetl plik

@ -14,7 +14,7 @@ CFLAGS += -I$(EMBED_DIR)
CFLAGS += -I$(EMBED_DIR)/port
CFLAGS += -Wall -Og -fno-common
SRC += main.c mphal.c
SRC += main.c mphal.c modules/c_hello/modc_hello.c
SRC += $(wildcard $(EMBED_DIR)/*/*.c)
# Filter out lib because the files in there cannot be compiled separately, they
# are #included by other .c files.

Wyświetl plik

@ -37,6 +37,9 @@ static const char *example_2 =
"import frozenhello\n"
"help(frozenhello)\n"
"print('frozenhello.hello():', frozenhello.hello())\n"
"import c_hello\n"
"help(c_hello)\n"
"print('c_hello.hello():', c_hello.hello())\n"
"\n"
"print('finish')\n"
;

Wyświetl plik

@ -11,5 +11,8 @@ EMBED_EXTRA = extmod
# Freeze Python modules.
FROZEN_MANIFEST ?= manifest.py
# Add C modules.
USER_C_MODULES = modules
# Include the main makefile fragment to build the MicroPython component.
include $(MICROPYTHON_TOP)/ports/embed/embed.mk

Wyświetl plik

@ -0,0 +1,2 @@
C_HELLO_MOD_DIR := $(USERMOD_DIR)
SRC_USERMOD_C += $(C_HELLO_MOD_DIR)/modc_hello.c

Wyświetl plik

@ -0,0 +1,23 @@
// Check examples/usercmodule for more info about how this works.
#include "py/runtime.h"
#include "py/objstr.h"
static mp_obj_t c_hello_hello() {
static const MP_DEFINE_STR_OBJ(hello_str, "Hello from C!");
return MP_OBJ_FROM_PTR(&hello_str);
}
static MP_DEFINE_CONST_FUN_OBJ_0(c_hello_hello_obj, c_hello_hello);
static const mp_rom_map_elem_t c_hello_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_c_hello) },
{ MP_ROM_QSTR(MP_QSTR_hello), MP_ROM_PTR(&c_hello_hello_obj) },
};
static MP_DEFINE_CONST_DICT(c_hello_module_globals, c_hello_module_globals_table);
const mp_obj_module_t c_hello_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&c_hello_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_c_hello, c_hello_module);