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));
}
--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;
}
--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;
}
//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