Files
EggDuino/button.h
cocktailyogi 6505252c20 Major update
imported some improvements from user "bartebor"
- fixed some timing issues
- added button-support via #ifdefine
- worked on penarm-shaking-bug
- restructured some codesegments
2015-05-05 22:27:01 +02:00

60 lines
767 B
C++

/*
* button.h
*
* Created: 04.05.2015 21:38:42
* Author: Yogi
*/
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include <Arduino.h>
#define debounceDelay 50
typedef void (*ActionCb)(void);
class Button
{
private:
long debounce;
byte state:1;
byte lastState:1;
byte pin;
ActionCb action;
Button( const Button &c );
Button& operator=( const Button &c );
public:
Button(byte p, ActionCb a): debounce(0), state(1), lastState(1), action(a), pin(p) {} ;
void check() {
byte b = digitalRead(pin);
long t = millis();
if (b != lastState) {
debounce = t;
}
if ((t - debounce) > debounceDelay) {
if (b != state) {
state = b;
if (!state) {
(*action)();
}
}
}
lastState = b;
};
}; //button
#endif //__BUTTON_H__