added multiple games [broken]

This commit is contained in:
willem oldemans
2020-11-08 22:06:41 +01:00
parent a16ccf479d
commit 0123e4cc9f
20 changed files with 3951 additions and 73 deletions

112
src/buttons.cpp Normal file
View 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;
}