added multiple games [broken]
This commit is contained in:
112
src/buttons.cpp
Normal file
112
src/buttons.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "buttons.h"
|
||||
#include <vector>
|
||||
#include "Arduino.h"
|
||||
|
||||
std::vector<buttons *> buttonlist;
|
||||
|
||||
buttons::buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index):
|
||||
_buttonIndex(index), _buttonPin(pin)
|
||||
{
|
||||
_buttonDelayShort = shortpress;
|
||||
_buttonDelayLong = longpress;
|
||||
_buttonState = INVALID;
|
||||
_lastState = INVALID;
|
||||
buttonlist.push_back(this);
|
||||
}
|
||||
|
||||
void buttons::begin()
|
||||
{
|
||||
pinMode(_buttonPin, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
bool anybutton(void)
|
||||
{
|
||||
handleButtons();
|
||||
for (auto &&i : buttonlist)
|
||||
{
|
||||
if (i->raw())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
buttons* getButton(unsigned int index)
|
||||
{
|
||||
for (auto &&i : buttonlist)
|
||||
{
|
||||
if( i->index() == index)
|
||||
return i;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user