uctypes declaration example?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
DoctorG
Posts: 9
Joined: Wed Nov 16, 2016 12:38 am

uctypes declaration example?

Post by DoctorG » Fri Nov 18, 2016 5:40 pm

I am having trouble grocking how to declare a uctype to match a C structure.

a simple example might help me.

how would you declare one for this:

Code: Select all

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;
Last edited by DoctorG on Sun Nov 20, 2016 2:12 am, edited 1 time in total.

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: uctyupes declaration

Post by pagano.paganino » Fri Nov 18, 2016 6:38 pm

Hi DoctorG,
try this:

Code: Select all

book = {
    "title": (uctypes.ARRAY | 0, uctypes.UINT8 | 50),
    "author": (uctypes.ARRAY | 50, uctypes.UINT8 | 50),
    "subject": (uctypes.ARRAY | 100, uctypes.UINT8 | 100),
    "book_id": (uctypes.INT32 | 100) 
}

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: uctyupes declaration

Post by pagano.paganino » Fri Nov 18, 2016 6:56 pm

But u can use a more pythonic way:

Code: Select all

>>> from ucollections import namedtuple
>>> book = namedtuple("book", ["title", "author", "subject", "book_id"])
>>> book("La divina commedia", "Dante Alighieri", "", 10)
book(title='La divina commedia', author='Dante Alighieri', subject='', book_id=10)
>>>

DoctorG
Posts: 9
Joined: Wed Nov 16, 2016 12:38 am

Re: uctyupes declaration

Post by DoctorG » Sun Nov 20, 2016 2:03 am

Perfect, this is a great help.

Now further in the documentation is says to use this struct to map it to the "C" foreign data with:

Code: Select all

class uctypes.struct(addr, descriptor, layout_type=NATIVE)
Instantiate a “foreign data structure” object based on structure address in memory, descriptor (encoded as a dictionary), and layout type (see below).
But how does one get the address of the foreign data structure. The documenation is a bit vague on FFIs, and I can't find anything about them with search.
Given a structure descriptor dictionary and its layout type, you can instantiate a specific structure instance at a given memory address using uctypes.struct() constructor. Memory address usually comes from following sources:
  • Predefined address, when accessing hardware registers on a baremetal system. Lookup these addresses in datasheet for a particular MCU/SoC.
    As a return value from a call to some FFI (Foreign Function Interface) function.
    From uctypes.addressof(), when you want to pass arguments to an FFI function, or alternatively, to access some data for I/O (for example, data read from a file or network socket).

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

Re: uctypes declaration example?

Post by dhylands » Sun Nov 20, 2016 2:19 am

If you allocated the structure in MicroPython then you can use the uctypes.addressof function to get the address to pass into an FFI function
http://docs.micropython.org/en/latest/p ... .addressof

If the C side allocated the data, then you need to call some C function to get the address of the data.

Post Reply