NerfStatTrak/Button.h
Mark van Renswoude ba58eb93c0 Moved away from proof of concepts, started prototyping for the final software
- Button library
- State handling and transitions
- Default state: dual counter display
- User unknown state: static for now
- Lost the game state: static for now
2016-12-04 19:53:43 +01:00

37 lines
1.0 KiB
C++

#ifndef Button_h
#define Button_h
#include "Arduino.h"
class Button
{
public:
void init(uint8_t pin, int offState = HIGH, bool usePullUp = true);
void update(unsigned long referenceTime = 0);
void block() { if (lastState != offState) blocked = true; }
bool changed() { return !blocked && lastChanged; }
bool pressed() { return !blocked && lastState != offState; }
unsigned long pressedMillis(unsigned long referenceTime = 0) { return getTime(referenceTime) - lastPressedTime; }
unsigned long getDebounceMillis() { return debounceMillis; }
void setDebounceMillis(unsigned long value) { debounceMillis = value; }
protected:
inline unsigned long getTime(unsigned long referenceTime) { return referenceTime == 0 ? millis() : referenceTime; };
private:
uint8_t pin = 1;
int offState = HIGH;
unsigned long debounceMillis = 50;
unsigned long lastChangedTime = 0;
unsigned long lastPressedTime = 0;
int lastState;
bool lastChanged;
bool blocked = false;
};
#endif