48 lines
914 B
C++
48 lines
914 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 (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;
|
|
} |