From ee792db6a379c3e239d2630457359da6b466b643 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Wed, 1 Nov 2023 20:12:18 -0300 Subject: [PATCH] Add Micropython example --- Makefile | 2 +- examples/micropython/Makefile | 13 ++++++++ examples/micropython/README.md | 9 ++++++ examples/micropython/main.py | 5 ++++ examples/micropython/mongoose/main.c | 29 ++++++++++++++++++ .../micropython/mongoose/micropython.cmake | 17 +++++++++++ examples/micropython/mongoose/micropython.mk | 9 ++++++ examples/micropython/mongoose/module.c | 30 +++++++++++++++++++ examples/micropython/mongoose/mongoose.c | 1 + examples/micropython/mongoose/mongoose.h | 1 + 10 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 examples/micropython/Makefile create mode 100644 examples/micropython/README.md create mode 100644 examples/micropython/main.py create mode 100644 examples/micropython/mongoose/main.c create mode 100644 examples/micropython/mongoose/micropython.cmake create mode 100644 examples/micropython/mongoose/micropython.mk create mode 100644 examples/micropython/mongoose/module.c create mode 120000 examples/micropython/mongoose/mongoose.c create mode 120000 examples/micropython/mongoose/mongoose.h diff --git a/Makefile b/Makefile index bf2b9310..caebdcfc 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ IPV6 ?= 1 ASAN ?= -fsanitize=address,undefined,alignment -fno-sanitize-recover=all -fno-omit-frame-pointer -fno-common ASAN_OPTIONS ?= detect_leaks=1 EXAMPLES := $(dir $(wildcard examples/*/Makefile)) -EXAMPLES_MAC := $(filter-out examples/mip-pcap/ examples/mip-tap/, $(EXAMPLES)) +EXAMPLES_MAC := $(filter-out examples/mip-pcap/ examples/mip-tap/ examples/micropython/, $(EXAMPLES)) EXAMPLES_WIN := $(dir $(wildcard examples/device-dashboard/Makefile) $(wildcard examples/file-*/Makefile) $(wildcard examples/http-*/Makefile) $(wildcard examples/mqtt-*/Makefile) $(wildcard examples/websocket-*/Makefile) $(wildcard examples/webui-*/Makefile)) EXAMPLES_EMBEDDED := $(filter-out $(wildcard examples/zephyr/*/), $(dir $(wildcard examples/*/*/Makefile))) PREFIX ?= /usr/local diff --git a/examples/micropython/Makefile b/examples/micropython/Makefile new file mode 100644 index 00000000..dc1aacc8 --- /dev/null +++ b/examples/micropython/Makefile @@ -0,0 +1,13 @@ + +all: example + micropython/ports/unix/build-standard/micropython main.py + +example: micropython + $(MAKE) -C micropython/ports/unix submodules + $(MAKE) -C micropython/ports/unix USER_C_MODULES=../../.. + +micropython: + git clone https://github.com/micropython/micropython.git + +clean: + rm -rf micropython diff --git a/examples/micropython/README.md b/examples/micropython/README.md new file mode 100644 index 00000000..1d03963b --- /dev/null +++ b/examples/micropython/README.md @@ -0,0 +1,9 @@ +# Mongoose as a MicroPython user module + +The most basic usage is in `main.py`. + +You'll have to configure Mongoose for your desired platform, this example runs on the `unix` port and Mongoose detects it as a known architecture and hence configures itself to use sockets and time base. For other platforms, you'll have to do your homework (there's plenty of Mongoose examples for several platforms); also check respective MicroPython build instructions. + +Check [_thread](https://docs.micropython.org/en/latest/library/_thread.html) support for more advanced usage + +https://docs.micropython.org/en/latest/develop/cmodules.html diff --git a/examples/micropython/main.py b/examples/micropython/main.py new file mode 100644 index 00000000..1be141f0 --- /dev/null +++ b/examples/micropython/main.py @@ -0,0 +1,5 @@ +import mongoose + +mongoose.init() +while True: + mongoose.poll(1000) diff --git a/examples/micropython/mongoose/main.c b/examples/micropython/mongoose/main.c new file mode 100644 index 00000000..c600b2ab --- /dev/null +++ b/examples/micropython/mongoose/main.c @@ -0,0 +1,29 @@ +// Copyright (c) 2022 Cesanta Software Limited +// All rights reserved + +#include "mongoose.h" + +static void t_fn(void *arg) { // Tick periodically + MG_INFO(("tick")); + (void) arg; +} + +static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "hi\n"); + (void) fn_data; + (void) ev_data; +} + +static struct mg_mgr mgr; + +void mgmp_init(void) { + mg_mgr_init(&mgr); // Mongoose event manager + mg_log_set(MG_LL_DEBUG); // Set log level + + mg_timer_add(&mgr, 1000, MG_TIMER_REPEAT, t_fn, &mgr); + mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr); +} + +void mgmp_poll(int t) { + mg_mgr_poll(&mgr, t); +} diff --git a/examples/micropython/mongoose/micropython.cmake b/examples/micropython/mongoose/micropython.cmake new file mode 100644 index 00000000..92f56b13 --- /dev/null +++ b/examples/micropython/mongoose/micropython.cmake @@ -0,0 +1,17 @@ +# Create an INTERFACE library for our C module. +add_library(usermod_mongoose INTERFACE) + +# Add our source files to the lib +target_sources(usermod_mongoose INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/module.c + ${CMAKE_CURRENT_LIST_DIR}/mongoose.c + ${CMAKE_CURRENT_LIST_DIR}/main.c +) + +# Add the current directory as an include directory. +target_include_directories(usermod_mongoose INTERFACE + ${CMAKE_CURRENT_LIST_DIR} +) + +# Link our INTERFACE library to the usermod target. +target_link_libraries(usermod INTERFACE usermod_mongoose) diff --git a/examples/micropython/mongoose/micropython.mk b/examples/micropython/mongoose/micropython.mk new file mode 100644 index 00000000..7e90e1cf --- /dev/null +++ b/examples/micropython/mongoose/micropython.mk @@ -0,0 +1,9 @@ +CEXAMPLE_MOD_DIR := $(USERMOD_DIR) + +# Add all C files to SRC_USERMOD. +SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/module.c +SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/mongoose.c +SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/main.c + +# add our module folder to include paths +CFLAGS_USERMOD += -I$(CEXAMPLE_MOD_DIR) diff --git a/examples/micropython/mongoose/module.c b/examples/micropython/mongoose/module.c new file mode 100644 index 00000000..7d38a2ce --- /dev/null +++ b/examples/micropython/mongoose/module.c @@ -0,0 +1,30 @@ +#include "py/runtime.h" + +extern void mgmp_init(void); +extern void mgmp_poll(int); + +STATIC mp_obj_t mongoose_init(void) { + mgmp_init(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mongoose_init_obj, mongoose_init); + +STATIC mp_obj_t mongoose_poll(mp_obj_t t_obj) { + mgmp_poll(mp_obj_get_int(t_obj)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mongoose_poll_obj, mongoose_poll); + +STATIC const mp_rom_map_elem_t mongoose_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_mongoose) }, + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mongoose_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mongoose_poll_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mongoose_module_globals, mongoose_module_globals_table); + +const mp_obj_module_t mongoose_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&mongoose_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_mongoose, mongoose_module); diff --git a/examples/micropython/mongoose/mongoose.c b/examples/micropython/mongoose/mongoose.c new file mode 120000 index 00000000..5e522bbc --- /dev/null +++ b/examples/micropython/mongoose/mongoose.c @@ -0,0 +1 @@ +../../../mongoose.c \ No newline at end of file diff --git a/examples/micropython/mongoose/mongoose.h b/examples/micropython/mongoose/mongoose.h new file mode 120000 index 00000000..ee4ac823 --- /dev/null +++ b/examples/micropython/mongoose/mongoose.h @@ -0,0 +1 @@ +../../../mongoose.h \ No newline at end of file