audiomixer/controller/mixercontroller_w5100_pio/src/button.cpp

51 lines
1.6 KiB
C++
Raw Normal View History

2020-04-01 09:37:09 +00:00
#include "button.h"
#define DEBOUNCETIME 20 //time to not check for inputs after key press
#define BUTTONTIMEHOLD 750 //time for button hold
#define BUTTONTIMEHOLDLONG 3000 //time for button long hold
Button::Button() {
}
void Button::update(long millis, boolean state)
{
//example: state=!digitalRead(PIN_BTNLEFT)
//Short press (true when button short pressed, on release)
_button=false;
//long press (true when button is held down for BUTTONTIMEHOLD, on time elapsed)
_button_hold=false;
if (millis-_millis_lastinput>DEBOUNCETIME) //Button debouncing
{
if (_timebuttonpressed == 0 && state){ //first time pressed down. (low when pressed)
_timebuttonpressed=millis; //set time of button press
_millis_lastinput=millis; //for debouncing
}else if(_timebuttonpressed != 0 && !state){ //button released (was pressed)
if (millis-_timebuttonpressed < BUTTONTIMEHOLD){ //short press
_button=true;
}
_timebuttonpressed=0; //re-enable after short press and release from hold
_millis_lastinput=millis; //for debouncing
}else if(millis-_timebuttonpressed >= BUTTONTIMEHOLD && _timebuttonpressed>0){ //held down long enough and not already hold triggered
_button_hold=true;
_timebuttonpressed=-1; //-1 as flag for hold triggered
}
}
if ( _button ) {
_millis_lastinput=millis; //for debouncing
}
}
boolean Button::buttonPressed()
{
return _button;
}
boolean Button::buttonHold()
{
return _button_hold;
}