Mark van Renswoude
ba58eb93c0
- Button library - State handling and transitions - Default state: dual counter display - User unknown state: static for now - Lost the game state: static for now
48 lines
934 B
C++
48 lines
934 B
C++
#include "Button.h"
|
|
|
|
|
|
void Button::init(uint8_t pin, int offState, bool usePullUp)
|
|
{
|
|
this->pin = pin;
|
|
this->offState = offState;
|
|
this->lastState = offState;
|
|
|
|
if (offState == HIGH && usePullUp)
|
|
pinMode(pin, INPUT_PULLUP);
|
|
else
|
|
pinMode(pin, INPUT);
|
|
}
|
|
|
|
|
|
void Button::update(unsigned long referenceTime)
|
|
{
|
|
unsigned long currentTime = getTime(referenceTime);
|
|
int currentState = digitalRead(pin);
|
|
|
|
if (lastChangedTime != 0)
|
|
{
|
|
if (currentState != lastState && (currentTime - lastChangedTime) > debounceMillis)
|
|
{
|
|
lastChanged = true;
|
|
|
|
if (currentState != offState)
|
|
lastPressedTime = currentTime;
|
|
else if (blocked)
|
|
{
|
|
blocked = false;
|
|
lastChanged = false;
|
|
}
|
|
|
|
lastChangedTime = currentTime;
|
|
lastState = currentState;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lastChangedTime = currentTime;
|
|
lastState = currentState;
|
|
}
|
|
|
|
lastChanged = false;
|
|
} |