Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Created February 19, 2012 01:57
Show Gist options
  • Save tkojitu/1861598 to your computer and use it in GitHub Desktop.
Save tkojitu/1861598 to your computer and use it in GitHub Desktop.
How to use class in Arduino
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