Union/Struct/Bits

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
DHembree
Experienced Member
Posts: 75
Joined: Fri Apr 15, 2016 6:46 pm

Union/Struct/Bits

Post by DHembree »

In previous programming exploits I have used the following sample of code to manipulate individual bits in a byte-sized variable that would subsequently be written to a device (i2c):

union User_Switches_A
{
struct {
bool IN1_Sw :1; //
bool IN2_Sw :1; //
bool IN3_Sw :1; //
bool MON1_Sw :1; //
bool MON2_Sw :1; //
bool MON3_Sw :1; //
bool MUTE_L :1; //
bool MUTE_R :1; //
}Bits;
byte Byte;
} Switches_A;

The Xtime compiler does not like this. Is there any way to alter this to make it compatible with XC, or is there another method I can use to do the same task?

Thanks!
Dan


robertxmos
XCore Addict
Posts: 169
Joined: Fri Oct 23, 2015 10:23 am

Post by robertxmos »

Hi Dan,

Unfortunately for you the XC language does not support bit-fields or a 1-bit boolean type.
The work around is either to write your bit-field code in C/C++
or manually write access functions (which use bit wise operators).

The access functions may be in-lined, and hence produce similar code to if you had been allowed to express your intent directly.
Sorry for the extra layer of complexity that is necessary with both suggestions... unwelcome encapsulation & abstraction!

robert
Post Reply