Skip to content

Commit 19ef417

Browse files
committed
src: Add simple example modules in C and Python.
Signed-off-by: Damien George <[email protected]>
1 parent 02f8291 commit 19ef417

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed

src/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In this directory are the custom modules that can be built into the firmware.
2+
There are examples of modules written in Python and C.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Create an INTERFACE library for our C module.
2+
add_library(usermod_cexample INTERFACE)
3+
4+
# Add our source files to the lib
5+
target_sources(usermod_cexample INTERFACE
6+
${CMAKE_CURRENT_LIST_DIR}/modcexample.c
7+
)
8+
9+
# Add the current directory as an include directory.
10+
target_include_directories(usermod_cexample INTERFACE
11+
${CMAKE_CURRENT_LIST_DIR}
12+
)
13+
14+
# Link our INTERFACE library to the usermod target.
15+
target_link_libraries(usermod INTERFACE usermod_cexample)

src/cmodules/cexample/micropython.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Add all C files to SRC_USERMOD.
2+
SRC_USERMOD += $(USERMOD_DIR)/modcexample.c

src/cmodules/cexample/modcexample.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Include MicroPython API.
2+
#include "py/runtime.h"
3+
4+
// This is the function which will be called from Python as cexample.add_ints(a, b).
5+
STATIC mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) {
6+
// Extract the ints from the micropython input objects.
7+
int a = mp_obj_get_int(a_obj);
8+
int b = mp_obj_get_int(b_obj);
9+
10+
// Calculate the addition and convert to MicroPython object.
11+
return mp_obj_new_int(a + b);
12+
}
13+
// Define a Python reference to the function above.
14+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints);
15+
16+
// Define all properties of the module.
17+
// Table entries are key/value pairs of the attribute name (a string)
18+
// and the MicroPython object reference.
19+
// All identifiers and strings are written as MP_QSTR_xxx and will be
20+
// optimized to word-sized integers by the build system (interned strings).
21+
STATIC const mp_rom_map_elem_t example_module_globals_table[] = {
22+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample) },
23+
{ MP_ROM_QSTR(MP_QSTR_add_ints), MP_ROM_PTR(&example_add_ints_obj) },
24+
};
25+
STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);
26+
27+
// Define module object.
28+
const mp_obj_module_t example_user_cmodule = {
29+
.base = { &mp_type_module },
30+
.globals = (mp_obj_dict_t *)&example_module_globals,
31+
};
32+
33+
// Register the module to make it available in Python.
34+
// Note: the "1" in the third argument means this module is always enabled.
35+
// This "1" can be optionally replaced with a macro like MODULE_CEXAMPLE_ENABLED
36+
// which can then be used to conditionally enable this module.
37+
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Create an INTERFACE library for our C module.
2+
add_library(usermod_cexample2 INTERFACE)
3+
4+
# Add our source files to the lib
5+
target_sources(usermod_cexample2 INTERFACE
6+
${CMAKE_CURRENT_LIST_DIR}/modcexample2.c
7+
)
8+
9+
# Add the current directory as an include directory.
10+
target_include_directories(usermod_cexample2 INTERFACE
11+
${CMAKE_CURRENT_LIST_DIR}
12+
)
13+
14+
# Link our INTERFACE library to the usermod target.
15+
target_link_libraries(usermod INTERFACE usermod_cexample2)

src/cmodules/cexample2/micropython.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Add all C files to SRC_USERMOD.
2+
SRC_USERMOD += $(USERMOD_DIR)/modcexample2.c

src/cmodules/cexample2/modcexample2.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "py/runtime.h"
2+
3+
STATIC mp_obj_t example_sub_ints(mp_obj_t a_obj, mp_obj_t b_obj) {
4+
int a = mp_obj_get_int(a_obj);
5+
int b = mp_obj_get_int(b_obj);
6+
return mp_obj_new_int(a - b);
7+
}
8+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_sub_ints_obj, example_sub_ints);
9+
10+
STATIC const mp_rom_map_elem_t example_module_globals_table[] = {
11+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample2) },
12+
{ MP_ROM_QSTR(MP_QSTR_sub_ints), MP_ROM_PTR(&example_sub_ints_obj) },
13+
};
14+
STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);
15+
16+
const mp_obj_module_t example_user_cmodule2 = {
17+
.base = { &mp_type_module },
18+
.globals = (mp_obj_dict_t *)&example_module_globals,
19+
};
20+
21+
MP_REGISTER_MODULE(MP_QSTR_cexample2, example_user_cmodule2, 1);

src/utils/xxd.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from machine import mem8, mem16
2+
3+
4+
def xxd(addr, num_bytes=64, width=16):
5+
for i in range((num_bytes + width - 1) // width):
6+
print("%08x:" % (addr + i * width), end="")
7+
for j in range(0, width, 2):
8+
word = mem16[addr + i * width + j]
9+
print(" %02x%02x" % (word & 0xFF, word >> 8), end="")
10+
print(" ", end="")
11+
for j in range(width):
12+
byte = mem8[addr + i * width + j]
13+
if 32 <= byte <= 126:
14+
print(chr(byte), end="")
15+
else:
16+
print(".", end="")
17+
print()

0 commit comments

Comments
 (0)