Created
February 19, 2012 01:46
-
-
Save tkojitu/1861572 to your computer and use it in GitHub Desktop.
How to use struct in Arduino
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 Control { | |
int pin; | |
}; | |
void ctrlPinMode(void* control, int mode) { | |
Control* ctrl = (Control*)control; | |
pinMode(ctrl->pin, mode); | |
} | |
int ctrlDigitalRead(void* control) { | |
Control* ctrl = (Control*)control; | |
return digitalRead(ctrl->pin); | |
} | |
void ctrlDigitalWrite(void* control, int value) { | |
Control* ctrl = (Control*)control; | |
digitalWrite(ctrl->pin, value); | |
} | |
void* gLed; | |
void* gButton; | |
void setupLed() { | |
Control* ctrl = new Control(); | |
ctrl->pin = 13; | |
ctrlPinMode(ctrl, OUTPUT); | |
gLed = ctrl; | |
} | |
void setupButton() { | |
Control* ctrl = new Control(); | |
ctrl->pin = 7; | |
ctrlPinMode(ctrl, INPUT); | |
gButton = ctrl; | |
} | |
void setup() { | |
setupLed(); | |
setupButton(); | |
} | |
void loop() { | |
int val = ctrlDigitalRead(gButton); | |
if (val == HIGH) { | |
ctrlDigitalWrite(gLed, HIGH); | |
} else { | |
ctrlDigitalWrite(gLed, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment