2016-12-04 18:53:43 +00:00
|
|
|
#include "Button.h"
|
|
|
|
|
|
|
|
|
|
|
|
void Button::init(uint8_t pin, int offState, bool usePullUp)
|
|
|
|
{
|
|
|
|
this->pin = pin;
|
|
|
|
this->offState = offState;
|
|
|
|
this->lastState = offState;
|
|
|
|
|
2016-12-05 22:42:20 +00:00
|
|
|
if (usePullUp)
|
2016-12-04 18:53:43 +00:00
|
|
|
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;
|
|
|
|
}
|