Using the C API to pack a binary file into MPY

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Ephreal
Posts: 10
Joined: Tue Jan 15, 2019 1:41 pm

Using the C API to pack a binary file into MPY

Post by Ephreal » Mon Feb 25, 2019 6:06 pm

Hi

I know there is a similar post out there but it does not clearly answer my question.

I have been trying to get a binary file packed into my micropython build so that i could load it on my Nios cpu. I have not had much success in this.

So now Im moving in another direction.
Is it possible to make a .c file with a array containing this binary data so that I would be able to read it in REPL mode and from other scripts modules. And would i make sense to do it that way. And does anyone have a simple example on who to do this. Have not had much successs in finding API documentation.

Regards

User avatar
dhylands
Posts: 3457
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API to pack a binary file into MPY

Post by dhylands » Mon Feb 25, 2019 7:31 pm

It looks like all you need to do is write a C function that returns a pointer and another function that returns a size.

You can then use uctypes.bytearray_at to get a bytearray. You can then access individual bytes of the bytearray or create memoryviews if you want to just work with sections. Using a memoryview won't allocate new memory for the data, whereas creating a slice of the bytearray will.

You C functions will look something like:

Code: Select all

STATIC uint8_t data[] = {0x11, 0x22, 0x33, 0x44, 0x55};
 
STATIC mp_obj_t data_address(void) {
    return MP_OBJ_NEW_SMALL_INT((intptr_t)data);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(data_address_obj, data_address);

STATIC mp_obj_t data_size(void) {
    return MP_OBJ_NEW_SMALL_INT(sizeof data);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(data_size_obj, data_size);
You'll need to rest of the code for creating a module (which any of the C examples should provide).

If you want your data to come from a memzip, then you could instead create a python interface to memzip_locate which would return a bytearray using code very similar to uctypes.bytearray_at

Ephreal
Posts: 10
Joined: Tue Jan 15, 2019 1:41 pm

Re: Using the C API to pack a binary file into MPY

Post by Ephreal » Fri Mar 01, 2019 3:30 pm

Hi

Got it to work with

Code: Select all

STATIC mp_obj_t file_open(mp_obj_t filename) {
    void* data;
    size_t len;
    memzip_locate(mp_obj_str_get_str(filename),&data,&len);
    return mp_obj_new_str(data, len, false);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_open_obj, file_open);
thak you for your help

User avatar
dhylands
Posts: 3457
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API to pack a binary file into MPY

Post by dhylands » Fri Mar 01, 2019 4:34 pm

You probably want to use mp_obj_new_bytes rather than new_str. A str will be interpreted as UTF8 which means that some binary sequences will cause you problems.

Ephreal
Posts: 10
Joined: Tue Jan 15, 2019 1:41 pm

Re: Using the C API to pack a binary file into MPY

Post by Ephreal » Fri Mar 01, 2019 7:37 pm

dhylands wrote:
Fri Mar 01, 2019 4:34 pm
You probably want to use mp_obj_new_bytes rather than new_str. A str will be interpreted as UTF8 which means that some binary sequences will cause you problems.
Thank you. I have no overview on what macros are available.

Post Reply