Created
November 8, 2016 16:46
-
-
Save nadavmatalon/170d0889334798dd69fd6e4e8fb3578f to your computer and use it in GitHub Desktop.
Struct Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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