Bit field members – Echelon LonTal Stack User Manual
Page 109
LonTalk Stack Developer’s Guide
97
typedef LON_STRUCT_BEGIN(SNVT_switch){
ncuInt value;
ncsInt state;
} LON_STRUCT_END(SNVT_switch);
Type definitions for structures assume a padding of 0 (zero) bytes and a packing
of 1 byte. The LON_STRUCT_BEGIN and LON_STRUCT_END macros
enforce platform-specific byte packing and padding. These macros are defined in
the LonPlatform.h file, which allows you to adjust them for your compiler.
Bit Field Members
For portability, none of the types that the LonTalk Interface Developer utility
generates use bit fields. Instead, the utility defines bit fields with their enclosing
bytes, and provides macros to extract or manipulate the bit field information.
By using macros to work directly with the bytes of the bit field, your code is
portable to both big-endian and little-endian platforms (that is, platforms that
represent the most-significant bit in the left-most position and platforms that
represent the most-significant bit in the right-most position). The macros also
reduce the need for anonymous bit fields to achieve the correct alignment and
padding.
Example: The following macros and structure define a simple bit field of two
flags, a 1-bit flag alpha and a 4-bit flag beta:
typedef LON_STRUCT_BEGIN(Example) {
LonByte flags_1;
// contains alpha, beta
} LON_STRUCT_END(Example);
#define LON_ALPHA_MASK 0x80
#define LON_ALPHA_SHIFT 7
#define LON_ALPHA_FIELD flags_1
#define LON_BETA_MASK 0x70
#define LON_BETA_SHIFT 4
#define LON_BETA_FIELD flags_1
When your program refers to the flags_1 structure member, it can use the bit
mask macros (LON_ALPHA_MASK and LON_BETA_MASK), along with the
bit shift values (LON_ALPHA_SHIFT and LON_BETA_SHIFT), to retrieve the
two flag values. These macros are defined in the LonNvTypes.h file. The
LON_STRUCT_* macros enforce platform-specific byte packing.
To read the alpha flag, use the following example assignment:
Example var;
alpha_flag = (var.LON_ALPHA_FIELD & var.LON_ALPHA_MASK) >>
var.LON_ALPHA_SHIFT;
You can also use the LON_GET_ATTRIBUTE() and
LON_SET_ATTRIBUTE() macros to access flag values. For example, for a
variable named var, you can use these macros to get or set the attributes:
alpha_flag = LON_GET_ATTRIBUTE(var, LON_ALPHA);
…
LON_SET_ATTRIBUTE(var, LON_ALPHA, alpha_flag);
These macros are defined in the FtxlTypes.h file.