Created
February 19, 2012 01:57
-
-
Save tkojitu/1861598 to your computer and use it in GitHub Desktop.
How to use class 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
class Control { | |
public: | |
int pin; | |
Control(int pin) : pin(pin) {} | |
~Control() {} | |
void pinMode(int mode) { | |
::pinMode(pin, mode); | |
} | |
int digitalRead() { | |
return ::digitalRead(pin); | |
} | |
void digitalWrite(int value) { | |
::digitalWrite(pin, value); | |
} | |
bool isOn() { | |
return digitalRead() == HIGH; | |
} | |
bool isOff() { | |
return !isOn(); | |
} | |
void beOn() { | |
digitalWrite(HIGH); | |
} | |
void beOff() { | |
digitalWrite(LOW); | |
} | |
}; | |
void* gButton; | |
void* gLed; | |
bool gOldState; | |
bool gState; | |
void setupButton() { | |
Control* button = new Control(7); | |
button->pinMode(INPUT); | |
gButton = button; | |
} | |
void setupLed() { | |
Control* led = new Control(13); | |
led->pinMode(OUTPUT); | |
gLed = led; | |
} | |
void setup() { | |
setupButton(); | |
setupLed(); | |
} | |
void loop() { | |
Control* button = (Control*)gButton; | |
Control* led = (Control*)gLed; | |
bool val = button->isOn(); | |
if (val && !gOldState) { | |
gState = !gState; | |
delay(500); | |
} | |
gOldState = val; | |
if (gState) { | |
led->beOn(); | |
} else { | |
led->beOff(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment