I have the following structs in a master unit used to compress a lot of information into as few bytes as possible.
struct fourZoneStruct {
unsigned char zone1 : 2;
unsigned char zone2 : 2;
unsigned char zone3 : 2;
unsigned char zone4 : 2;
};
struct bellEventStruct {
unsigned char startHour : 5;
unsigned char startMinute : 6;
unsigned char dowSunday : 1;
unsigned char dowMonday : 1;
unsigned char dowTuesday : 1;
unsigned char dowWednesday : 1;
unsigned char dowThursday : 1;
unsigned char dowFriday : 1;
unsigned char dowSaturday : 1;
fourZoneStruct zones1to4;
} bellEvent[50];
Each "Event" has a given start time in hours and minutes within a 24 hour day and can happen on any of 7 days of a week and affect 4 "zones". Each zone can take 1 of 4 actions, with action 0 being no action.
This is used in a master unit that keeps track of the time, date and master schedule. When there is an event upcoming, the master unit needs to send a serial command to a secondary unit, which keeps time of day and can take action on the "zone" within that 24-hour day. The secondary unit stores the same information in the following struct.
struct fourZoneStruct {
unsigned char zone1 : 2;
unsigned char zone2 : 2;
unsigned char zone3 : 2;
unsigned char zone4 : 2;
};
struct bellEventStruct {
unsigned char startHour : 5;
unsigned char startMinute : 6;
fourZoneStruct zones1to4;
} bellEvent[50];
I would like the protocol for this communication to be 4 bytes, the first being ASCII 'E' to signify the start of the message, the second being the hour value. The third being the minute value and the 4th containing the zone data. It is the 4th byte I am having difficulty with and would like some help.
How can I Serial.write() the byte stored inbellEvent[i].zones1to4
?
When I have sent it, how can I set the value of
bellEvent[i].zones1to4
to the value returned by Serial.read() in the secondary unit so that the zone information may be communicated in 1 byte and the bit fields preserved?