Hi, this is probably the worst thing to say when looking for help but I am completely new to C, and micropython. I have been playing around with ESP8266's for a little while, and I began a project where I ended up way over my head, simply writing in the Arduino IDE. As I more or less know python to a useable level, I thought I would solve my problems by creating a micropython module that would suit my needs. This has been far more successful than my other attempts. But I have some data, as a struct that I would like my function to output.
This is the function, with a failed attempt:
STATIC mp_obj_t promisc_getpacket(void) {
mp_obj_t destination;
destination = packetbuffer1.destination;
return MP_OBJ_NEW_list(destination);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(promisc_getpacket_obj, promisc_getpacket);
(simple I know; the rest is handled in separate functions)
This was an attempt to output an array from the struct 'packetbuffer1' as defined earlier in the script:
struct packet{
uint8_t destination[6];
uint8_t source[6];
uint8_t channel;
};
struct packet packetbuffer1;
Ideally I would be able to return the strut, into python but every attempt I make seems to fail, either when I compile or crashes Micropython when I test it, rebooting the ESP8266.
Any help would be much appreciated in solving this issue!
Thanks, Peter!
Returning a struct or items from a struct (mp_obj_new_struct?!?)
Re: Returning a struct or items from a struct (mp_obj_new_struct?!?)
Progress has been made! I have used mp_obj_new_bytearray, however that means I can only output one item from the struct. I have fixed it with a potential workaround but it there is a way to return the items in the struct variable as a python dictionary(?), that would be ideal!
Re: Returning a struct or items from a struct (mp_obj_new_struct?!?)
This statement leads me to this question: are you sure you need to write this in C?
I was experienced in C and it took me time to acclimate to micropython's C-implementation so if you truly are new to C AND micropython, I firmly discourage you from trying to write C-code unless you absolutely need to.
What are you trying to do?
Are you sure there's not a library that does this already?
I'm glad to help you but first answer those two questions.
Re: Returning a struct or items from a struct (mp_obj_new_struct?!?)
I am trying to expose the SDK functions, essentially just promiscuous mode, to be able to read raw packets, from the ESP8266. I unfortunately haven't seen anyone try to do this with Micropython. Therefore I am building my own module. So far semi-successfully. I exaggerate on being completely new, but I am by no means expert, and I very much value the knowledge of people here!
So far I have just created separate functions to return each item from the struct, but this is by no means ideal. But I can then write the rest in Python which is always nice
So far I have just created separate functions to return each item from the struct, but this is by no means ideal. But I can then write the rest in Python which is always nice
Re: Returning a struct or items from a struct (mp_obj_new_struct?!?)
You need to return a python object. Now you could create a python object which has the C struct embedded in it. MicroPython functions (that are callable from python) all take objects as parameters and return objects.
A python object has to have an mp_obj_base_t as its first member and the remaining members can be arbitrary. Your python object could contain an embedded structure or a pointer to a structure. For example: https://github.com/micropython/micropyt ... #L124-L132
You could then create an mp_obj_type_t (like: https://github.com/micropython/micropyt ... 1266-L1272) which could have functions for manipulating/accessing the struct.
A python object has to have an mp_obj_base_t as its first member and the remaining members can be arbitrary. Your python object could contain an embedded structure or a pointer to a structure. For example: https://github.com/micropython/micropyt ... #L124-L132
You could then create an mp_obj_type_t (like: https://github.com/micropython/micropyt ... 1266-L1272) which could have functions for manipulating/accessing the struct.