I am trying to write a program to mimic a BLE bicycle speed and cadence sensor. It requires a message with an unusual structure - basically an 11 byte message with octets that conform to a structure of:
byte
32 bit unsigned integer
16 bit unsigned integer
16 bit unsigned integer
16 bit unsigned integer
I created a union based on a struct with the appropriate data structure but when I look at the way the union actually holds the data it doesn't make sense to me. Below my debugging code creates the data structure with an 11 byte array as the union data, initializes with values that should let me see how the bytes are held, and then Serial.println for me to review that data.
//declare a union of type csc_data which points to a struct with data appropriate types
union csc_data{
struct {
uint8_t flags;
uint32_t cumWheelRevolution;
uint16_t lastWheelEventTime;
uint16_t cumCrankRevolution;
uint16_t lastCrankEventTime;
};
byte cscValues[11];
};
//instantiate a union of type csc_data
union csc_data myCSCdata;
void setup() {
Serial.begin(115200);
Serial.println("Start");
myCSCdata.flags = 111; //initialize flags to indicate will have both wheel and crank revolution data
myCSCdata.cumWheelRevolution = 67305985; //hex 04030201
myCSCdata.lastWheelEventTime = 1541; //hex 0605
myCSCdata.cumCrankRevolution = 2055; //hex 0807
myCSCdata.lastCrankEventTime = 2569; //hex 0a09
Serial.println(myCSCdata.cscValues[0]);
Serial.println(myCSCdata.cscValues[1]);
Serial.println(myCSCdata.cscValues[2]);
Serial.println(myCSCdata.cscValues[3]);
Serial.println(myCSCdata.cscValues[4]);
Serial.println(myCSCdata.cscValues[5]);
Serial.println(myCSCdata.cscValues[6]);
Serial.println(myCSCdata.cscValues[7]);
Serial.println(myCSCdata.cscValues[8]);
Serial.println(myCSCdata.cscValues[9]);
Serial.println(myCSCdata.cscValues[10]);
}
What I'm EXPECTING is serial output that looks like:
111
1
2
3
4
5
6
7
8
9
10
What I'm getting is:
111
0
0
0
1
2
3
4
5
6
7
If I change the code to create a 14 element array:
//declare a union of type csc_data which points to a struct with data appropriate types
union csc_data{
struct {
uint8_t flags;
uint32_t cumWheelRevolution;
uint16_t lastWheelEventTime;
uint16_t cumCrankRevolution;
uint16_t lastCrankEventTime;
};
byte cscValues[14];
};
//instantiate a union of type csc_data
union csc_data myCSCdata;
void setup() {
Serial.begin(115200);
Serial.println("Start");
myCSCdata.flags = 111; //initialize flags to indicate will have both wheel and crank revolution data
myCSCdata.cumWheelRevolution = 67305985; //hex 04030201
myCSCdata.lastWheelEventTime = 1541; //hex 0605
myCSCdata.cumCrankRevolution = 2055; //hex 0807
myCSCdata.lastCrankEventTime = 2569; //hex 0a09
Serial.println(myCSCdata.cscValues[0]);
Serial.println(myCSCdata.cscValues[1]);
Serial.println(myCSCdata.cscValues[2]);
Serial.println(myCSCdata.cscValues[3]);
Serial.println(myCSCdata.cscValues[4]);
Serial.println(myCSCdata.cscValues[5]);
Serial.println(myCSCdata.cscValues[6]);
Serial.println(myCSCdata.cscValues[7]);
Serial.println(myCSCdata.cscValues[8]);
Serial.println(myCSCdata.cscValues[9]);
Serial.println(myCSCdata.cscValues[10]);
Serial.println(myCSCdata.cscValues[11]);
Serial.println(myCSCdata.cscValues[12]);
Serial.println(myCSCdata.cscValues[13]);
}
I get the following:
111
0
0
0
1
2
3
4
5
6
7
8
9
10
I do not understand why I have three empty bytes after the first byte (which is holding the correct data).
Thanks for any insight.
AGS