mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-18 11:28:13 +08:00
Merge pull request #2450 from cesanta/micropython
Add Micropython example
This commit is contained in:
commit
351bd42bc4
2
Makefile
2
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
|
||||
|
13
examples/micropython/Makefile
Normal file
13
examples/micropython/Makefile
Normal file
@ -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
|
9
examples/micropython/README.md
Normal file
9
examples/micropython/README.md
Normal file
@ -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
|
5
examples/micropython/main.py
Normal file
5
examples/micropython/main.py
Normal file
@ -0,0 +1,5 @@
|
||||
import mongoose
|
||||
|
||||
mongoose.init()
|
||||
while True:
|
||||
mongoose.poll(1000)
|
29
examples/micropython/mongoose/main.c
Normal file
29
examples/micropython/mongoose/main.c
Normal file
@ -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);
|
||||
}
|
17
examples/micropython/mongoose/micropython.cmake
Normal file
17
examples/micropython/mongoose/micropython.cmake
Normal file
@ -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)
|
9
examples/micropython/mongoose/micropython.mk
Normal file
9
examples/micropython/mongoose/micropython.mk
Normal file
@ -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)
|
30
examples/micropython/mongoose/module.c
Normal file
30
examples/micropython/mongoose/module.c
Normal file
@ -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);
|
1
examples/micropython/mongoose/mongoose.c
Symbolic link
1
examples/micropython/mongoose/mongoose.c
Symbolic link
@ -0,0 +1 @@
|
||||
../../../mongoose.c
|
1
examples/micropython/mongoose/mongoose.h
Symbolic link
1
examples/micropython/mongoose/mongoose.h
Symbolic link
@ -0,0 +1 @@
|
||||
../../../mongoose.h
|
Loading…
Reference in New Issue
Block a user