To add a C module to MicroPython ...

Let's implement a simple function for the time being.

Everything can be done with micropython, so there is no need to implement it in C, Minimal confirmation in case you need to implement something in C in the future.

I couldn't find the documentation and I followed the micropython code on git and implemented it, so there may be another way ... (I hope you know it)

--Return with a string as an argument

STATIC mp_obj_t return_str(mp_obj_t str_obj)
{
    const char *str = mp_obj_str_get_str(str_obj);

    return mp_obj_new_str(str, strlen(str));
}

list operation

--Create

STATIC mp_obj_t list_new()
{
    mp_obj_t list_items[] = {
        mp_obj_new_int(123),
        mp_obj_new_float(456.789),
        mp_obj_new_str("hello", 5),
    };

    return mp_obj_new_list(3, list_items);
}
//When the item you want to add is an int type
STATIC mp_obj_t list_append(mp_obj_t list_obj, mp_obj_t item_int_obj)
{
    mp_int_t item_obj = mp_obj_get_int(item_int_obj);
    mp_obj_t item = mp_obj_new_int(item_obj);

    mp_obj_list_append(list_obj, item);

    return list_obj;
}
//When the item you want to delete is an int type
STATIC mp_obj_t list_remove(mp_obj_t list_obj, mp_obj_t item_int_obj)
{
    mp_int_t item_obj = mp_obj_get_int(item_int_obj);
    mp_obj_t item = mp_obj_new_int(item_obj);

    mp_obj_list_remove(list_obj, item);

    return list_obj;
}

dict operation

--Create

STATIC mp_obj_t dict_new()
{
    mp_obj_t dict_obj = mp_obj_new_dict(3);
    mp_obj_dict_store(dict_obj, mp_obj_new_str("test_key1", 9), mp_obj_new_int(123));
    mp_obj_dict_store(dict_obj, mp_obj_new_str("test_key2", 9), mp_obj_new_float(456.789));
    mp_obj_dict_store(dict_obj, mp_obj_new_str("test_key3", 9), mp_obj_new_str("hello", 5));

    return dict_obj;
}
//The element you want to add['str type': 'str type']in the case of
STATIC mp_obj_t dict_store(mp_obj_t dict_obj, mp_obj_t key_str_obj, mp_obj_t value_str_obj)
{
    mp_obj_dict_get_map(dict_obj);
    const char *key = mp_obj_str_get_str(key_str_obj);
    const char *val = mp_obj_str_get_str(value_str_obj);

    mp_obj_dict_store(dict_obj, mp_obj_new_str(key, strlen(key)), mp_obj_new_str(val, strlen(val)));

    return dict_obj;
}
//When the key you want to delete is str type
STATIC mp_obj_t dict_delete(mp_obj_t dict_obj, mp_obj_t key_str_obj)
{
    mp_obj_dict_get_map(dict_obj);
    const char *del_key = mp_obj_str_get_str(key_str_obj);

    mp_obj_dict_delete(dict_obj, mp_obj_new_str(del_key, strlen(del_key)));

    return dict_obj;
}

To be able to call from MicroPython ...

//Function to call
STATIC mp_obj_t return_str(mp_obj_t str_obj)
{
    const char *str = mp_obj_str_get_str(str_obj);

    return mp_obj_new_str(str, strlen(str));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(return_str_obj, return_str);

//Defined as a module
STATIC const mp_rom_map_elem_t example_module_globals_table[] = {
    //Name when importing with micropython
    {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_example)}, 
  //Name when calling a function from micropython
    {MP_ROM_QSTR(MP_QSTR_return_str), MP_ROM_PTR(&return_str_obj)},
};

STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);

const mp_obj_module_t example_cmodule = {
    .base = {&mp_type_module},
    .globals = (mp_obj_dict_t *)&example_module_globals,
};

Create .C as above and add it to the micropython source to build. From micropython

import example
example.return_str()

Use it like this.

Recommended Posts

To add a C module to MicroPython ...
How to add a Python module search path
To add a module to python put in Julialang
Add a dictionary to MeCab
Try to make a Python module in C language
[C language] [Linux] Try to create a simple Linux command * Just add! !!
Add a GPIO board to your computer. (1)
How to add a package with PyCharm
How to add python module to anaconda environment
Add a Python virtual environment to VSCode
Add a command to mark similar files together
I tried adding a Python3 module in C
How to uninstall a module installed using setup.py
[Wagtail] Add a login page to the Wagtail project
I made a module in C language to filter images loaded by Python
Add convolution to MNIST
Create a Python module
Created a module to monitor file and URL updates
Introduction to EV3 / MicroPython
I made a Python module to translate comment outs
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
Brown coder tried to solve Panasonic Contest 2020A ~ C
[Morphological analysis] How to add a new dictionary to Mecab
I want to add my own structure to the structure created by Python's C extension module!
Generate a bash script to add Datadog monitor settings
[Python] How to call a c function from python (ctypes)
[Django] A pattern to add related records after creating a record
Add an extension to build a more comfortable Jupyter environment
How to import NoteBook as a module in Jupyter (IPython)
Add a list of numpy library functions little by little --c
Add a function to heat transfer + heat input by temperature to heatrapy
A handy function to add a column anywhere in a Pandas DataFrame
The usual way to add a Kernel with Jupyter Notebook
How to import NoteBook as a module in Jupyter (IPython)
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
A quick introduction to pytest-mock
A road to intermediate Python
A super introduction to Linux
How to call a function
ABC129 A, B, C commentary
Add page number to PDF
Upload a file to Dropbox
Send a signal to subprocess
How to hack a terminal
Try to select a language
Add System to pyenv-win versions
Add user dictionary to MeCab
[Python] How to add rows and columns to a table (pandas DataFrame)
[C language] How to create, avoid, and make a zombie process
Let's stop copying. Introducing flati, a module to flatten with Python
Pass a list by reference from Python to C ++ with pybind11
Attempt to extend a function in the library (add copy function to pathlib)