Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8026

SDK • Re: Trying to just get started- native machine code into mpy file

$
0
0
Here's a working Native C Extension to get you started -

micropython.cmake

Code:

target_link_libraries(usermod INTERFACE OWN_MODULES)add_library(OWN_MODULES INTERFACE)target_include_directories(OWN_MODULES INTERFACE ${CMAKE_CURRENT_LIST_DIR})target_sources(OWN_MODULES INTERFACE    ${CMAKE_CURRENT_LIST_DIR}/modsecret.c)
modsecret.c

Code:

// **************************************************************************// *                                                                        *// *    PicoPython 'secret' module                                          *// *                                                                        *// **************************************************************************// *                                                                        *// *    string = secret.encode(string)          Encode a string             *// *    string = secret.decode(string)          Decode an encoded string    *// *                                                                        *// **************************************************************************// MIT Licensed : Copyright 2025, 'hippy'#include "py/runtime.h"#include "py/objstr.h"#include "py/obj.h"#include "py/mphal.h"// .------------------------------------------------------------------------.// |    string = secret.encode(string)          Encode a string             |// `------------------------------------------------------------------------'static mp_obj_t secret_encode(mp_obj_t string_obj) {    const char * string     = mp_obj_str_get_str(string_obj);    int          length     = strlen(string);    mp_obj_t     output_obj = mp_obj_new_str(string, length);    char *       output     = (char *) mp_obj_str_get_str(output_obj);    for(int n=0; n < length; n++) {        char c = output[n];        if (c >= 0x20 && c <= 0x7E) {            c += 1;            if (c <= 0x20 || c >= 0x7F) { c = c - 0x7F + 0x20; }            output[n] = c;        }    }    return output_obj;}MP_DEFINE_CONST_FUN_OBJ_1(secret_encode_obj,                          secret_encode);// .------------------------------------------------------------------------.// |    string = secret.decode(string)          Decode an encoded string    |// `------------------------------------------------------------------------'static mp_obj_t secret_decode(mp_obj_t string_obj) {    const char * string     = mp_obj_str_get_str(string_obj);    int          length     = strlen(string);    mp_obj_t     output_obj = mp_obj_new_str(string, length);    char *       output     = (char *) mp_obj_str_get_str(output_obj);    for(int n=0; n < length; n++) {        char c = output[n];        if (c >= 0x20 && c <= 0x7E) {            c -= 1;            if (c <= 0x1F || c >= 0x7E) { c = c - 0x1F + 0x7E; }            output[n] = c;        }    }    return output_obj;}MP_DEFINE_CONST_FUN_OBJ_1(secret_decode_obj,                          secret_decode);// **************************************************************************// *                                                                        *// *    MicroPython module definition                                       *// *                                                                        *// **************************************************************************static const mp_rom_map_elem_t module_secret_globals_table[] = {    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_secret)    },    { MP_ROM_QSTR(MP_QSTR_encode),   MP_ROM_PTR(&secret_encode_obj) },    { MP_ROM_QSTR(MP_QSTR_decode),   MP_ROM_PTR(&secret_decode_obj) },};static MP_DEFINE_CONST_DICT(module_secret_globals,                            module_secret_globals_table);// .------------------------------------------------------------------------.// |    MicroPython integration                                             |// `------------------------------------------------------------------------'const mp_obj_module_t module_secret = {    .base = { &mp_type_module },    .globals = (mp_obj_dict_t *)&module_secret_globals,};MP_REGISTER_MODULE(MP_QSTR_secret, module_secret);// **************************************************************************// *                                                                        *// *    End of PicoPython 'secret' module                                   *// *                                                                        *// **************************************************************************

Code:

>>> import secret>>> s = "It's a kind of magic">>> e = secret.encode(s)>>> d = secret.decode(e)>>> print(s)It's a kind of magic>>> print(e)Ju(t!b!ljoe!pg!nbhjd>>> print(d)It's a kind of magic>>>

Statistics: Posted by hippy — Thu Mar 27, 2025 1:04 pm



Viewing all articles
Browse latest Browse all 8026

Trending Articles