Skip to content

Instantly share code, notes, and snippets.

@nadavmatalon
Created November 8, 2016 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadavmatalon/170d0889334798dd69fd6e4e8fb3578f to your computer and use it in GitHub Desktop.
Save nadavmatalon/170d0889334798dd69fd6e4e8fb3578f to your computer and use it in GitHub Desktop.
Struct Example
struct hours_byte {
unsigned int format: 1; // range: 0-1, 0=24h 1=12h
unsigned int ampm: 1; // range: 0-1, 0=AM 1=PM
unsigned int tenshours: 2; // range: 0-2
unsigned int hours: 4; // range: 0-15 (guessing it never goes over 9)
};
void pHour(hours_byte when) {
Serial.print(when.tenshours, DEC);
Serial.print(when.hours, DEC);
Serial.print(":00");
if (when.format) Serial.print((char*)(when.ampm? "pm" : "am"));
Serial.print(" == ");
Serial.println(*(byte*)(hours_byte*)&when, BIN);
}
hours_byte now;
void setup() {
// four in the afternoon, in 24hr format.
now.tenshours = 1;
now.hours = 6;
now.format = 0;
now.ampm = 0;
pHour(now);
// four in the afternoon, in 12hr format.
now.tenshours = 0;
now.hours = 4;
now.format = 1;
now.ampm = 1;
pHour(now);
// five in the morning, in 12hr format.
now.tenshours = 0;
now.hours = 5;
now.format = 1;
now.ampm = 0;
pHour(now);
}
void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment