Bitfield in cython

139 views
Skip to first unread message

Aditya Ganesh

unread,
Mar 21, 2021, 4:36:21 AM3/21/21
to cython-users
union HitachiProtocol
{

    uint8_t raw[kHitachiAc1StateLength]; ///< The state in native code.
    struct
    {
        // Byte 0~2
        uint8_t pad[3];
        // Byte 3
        uint8_t : 6;
        uint8_t Model : 2;
        // Byte 4
        uint8_t : 8;
        // Byte 5
        uint8_t Fan : 4;
        uint8_t Mode : 4;
        // Byte 6
        uint8_t : 2;
        uint8_t Temp : 5; // stored in LSB order.
        uint8_t : 1;
        // Byte 7
        uint8_t OffTimerLow : 8; // nr. of minutes
        // Byte 8
        uint8_t OffTimerHigh : 8; // & in LSB order.
        // Byte 9
        uint8_t OnTimerLow : 8; // nr. of minutes
        // Byte 10
        uint8_t OnTimerHigh : 8; // & in LSB order.
        // Byte 11
        uint8_t SwingToggle : 1;
        uint8_t Sleep : 3;
        uint8_t PowerToggle : 1;
        uint8_t Power : 1;
        uint8_t SwingV : 1;
        uint8_t SwingH : 1;
        // Byte 12
        uint8_t Sum : 8;
    };
};

I want to use this bitfield structure in my cython code but Iam unable to find resources to show me how to create a bitfield in cython and how to access bitfield in cython

Golden Rockefeller

unread,
Mar 21, 2021, 2:12:16 PM3/21/21
to cython-users
This is what I have come up with after following the language basics guide. You can also try looking at Interfacing with External C Code guide.

cdef enum:
     kHitachiAc1StateLength = 100
cdef struct HitachiProtocol_inner:
        char pad[3]
        char b3
        char Model
        char b4
        char Fan
        char Mode
        char b6
        char Temp
        char b7
        char OffTimerLow
        char OffTimerHigh
        char OnTimerLow
        char OnTimerHigh
        char SwingToggle
        char Sleep
        char PowerToggle
        char Power
        char SwingV
        char SwingH
        char Sum

cdef union HitachiProtocol:
    char raw[kHitachiAc1StateLength]
    HitachiProtocol_inner  inner
    
cdef HitachiProtocol h
h.inner.b6 = 6
print(h.inner.b6)

Aditya Ganesh

unread,
Mar 21, 2021, 3:45:30 PM3/21/21
to cython-users
Thank You for the reply I just had one doubt, in my bitfield structure used in my header file I am assigning the number of bits the variable corresponds to for example : uint8_t Temp:5 means that my Temp variable in structure will always store any data in 5 bits so in your given cython code when u write h.inner.b6 = 6 is it allocating 6 bits to the b6 variable or is 6 the data you want to store in b6's memory location I apologize in advance as I don't have enough experience in cython and this might be a very dumb doubt I will also try the above code you have provided

Aditya Ganesh

unread,
Mar 21, 2021, 3:45:36 PM3/21/21
to cython-users
Thank You for the reply I will try the above code once but I had a doubt when you write h.inner.b6 = 6 I am assuming it assings the variable b6 with value of 6 and what I really want to achieve with bitfield is to be able to tell the variable b6 that it needs to store any variable only using 3 bits for example in my above code I have written uint8_t Temp:5 which means Temp will always store any data assigned to it using only 5 bits 

On Sunday, March 21, 2021 at 11:42:16 PM UTC+5:30 whiteg...@gmail.com wrote:

Peter Schay

unread,
Mar 21, 2021, 4:01:47 PM3/21/21
to cython-users

I want to use this bitfield structure in my cython code but Iam unable to find resources to show me how to create a bitfield in cython and how to access bitfield in cython

Hi,
If it is not performance-intensive inner loop code, you could use python properties to get and set the bitfields, with C code inside each get and set function to do it properly.
If it is performance intensive, you could implement inline cdef methods for getting and setting.
In both cases, if you have a lot of bitfields it probably makes sense to generate the code from a table (i.e. loop through a python list with metadata about each bitfield and generate the getters and setters).
There might be a better way; this is just what came to mind!

Regards,
Pete


 

Golden Rockefeller

unread,
Mar 21, 2021, 6:43:26 PM3/21/21
to cython-users
Sorry, I didn't know about this feature in C. Looking at this stackoverflow query. This is what I have.


cdef extern from *:
    """
    const unsigned int kHitachiAc1StateLength = 100;
    cdef const unsigned int kHitachiAc1StateLength

    cdef union HitachiProtocol:
        char raw[kHitachiAc1StateLength]
        char pad[3]
        char Model
        char Fan
        char Mode
        char Temp
        char OffTimerLow
        char OffTimerHigh
        char OnTimerLow
        char OnTimerHigh
        char SwingToggle
        char Sleep
        char PowerToggle
        char Power
        char SwingV
        char SwingH
        char Sum

cdef HitachiProtocol h
h.Power = 1
h.SwingV = 1
print(h.Power)
print(h.SwingV)
h.raw[11] = 0
print(h.Power)
print(h.SwingV)

Aditya Ganesh

unread,
Mar 22, 2021, 10:19:48 AM3/22/21
to cython-users
Thank you for the reply I will try out your suggestions and get back to you if you had a bit more info on how to use python to store metadata and get and set using that it would be a great help
Reply all
Reply to author
Forward
0 new messages