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
Using the C API to pack a binary file into MPY
Re: Using the C API to pack a binary file into MPY
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: 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
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);
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
Re: Using the C API to pack a binary file into MPY
Hi
Got it to work with
thak you for your help
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);
Re: Using the C API to pack a binary file into MPY
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.