Changed to JC_button lib

This commit is contained in:
willem oldemans
2020-11-19 08:27:52 +01:00
parent 8c9176ccbe
commit d184f1785c
8 changed files with 135 additions and 167 deletions

View File

@@ -1,90 +1,42 @@
#include "buttons.h"
#include "board.h"
#include <vector>
#include "Arduino.h"
#include "JC_Button.h"
std::vector<buttons *> buttonlist;
buttons::buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index):
_buttonIndex(index), _buttonPin(pin)
std::vector<ToggleButton *> buttonlist;
ToggleButton button1(SWITCH12);
ToggleButton button2(SWITCH22);
ToggleButton button3(SWITCH32);
ToggleButton button4(SWITCH1);
ToggleButton button5(SWITCH2);
ToggleButton button6(SWITCH3);
void buttonbegin( ToggleButton *thisbutton )
{
_buttonDelayShort = shortpress;
_buttonDelayLong = longpress;
_buttonState = INVALID;
_lastState = INVALID;
buttonlist.push_back(this);
thisbutton->begin();
buttonlist.push_back(thisbutton);
}
void buttons::begin()
void initButtons( void )
{
pinMode(_buttonPin, INPUT_PULLUP);
buttonbegin(&button1);
buttonbegin(&button2);
buttonbegin(&button3);
buttonbegin(&button4);
buttonbegin(&button5);
buttonbegin(&button6);
}
buttonState_t buttons::state()
{
return _buttonState;
}
buttonState_t buttons::lastState(void)
{
return _lastState;
}
bool buttons::raw(void)
{
return _buttonFlag;
}
void buttons::update(void)
{
unsigned long currentMillis = millis();
_buttonFlag = !digitalRead(_buttonPin);
if (_buttonFlag)
{
if (_buttonState == RELEASED)
{
//button not detected yet, check timer
if ((currentMillis - _buttonTimer) >= _buttonDelayShort)
{
_buttonState = SHORT;
_lastState = SHORT;
}
}
else if (_buttonState == SHORT)
{
if ((currentMillis - _buttonTimer) >= _buttonDelayLong)
{
_buttonState = LONG;
_lastState = LONG;
}
}
}
else
{
//button is not pressed, keep updating the timer
_buttonState = RELEASED;
_buttonTimer = millis();
}
}
unsigned int buttons::index( void )
{
return _buttonIndex;
}
void initbuttons(void)
{
for (auto &&i : buttonlist)
{
i->begin();
}
}
void handleButtons(void)
{
for (auto &&i : buttonlist)
{
i->update();
i->read();
}
}
@@ -93,7 +45,7 @@ bool anybutton(void)
handleButtons();
for (auto &&i : buttonlist)
{
if (i->raw())
if (i->isPressed())
{
return true;
}
@@ -101,12 +53,11 @@ bool anybutton(void)
return false;
}
buttons* getButton(unsigned int index)
ToggleButton* getButton(unsigned int index)
{
for (auto &&i : buttonlist)
if(index > buttonlist.size())
{
if( i->index() == index)
return i;
return NULL;
}
return NULL;
return buttonlist[index-1];
}