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;
Code: Select all
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
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)
}
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)
>>>
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).
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).