Files
Leo-led-truck/src/buttons.cpp
2021-10-21 16:25:15 +02:00

170 lines
2.6 KiB
C++

#include "buttons.h"
std::vector<c_button *> buttonlist;
c_button button1(SWITCH1, YELLOW, 1, type_switch);
c_button button2(SWITCH2, RED, 2, type_switch);
c_button button3(SWITCH3, GREEN, 3, type_switch);
c_button button4(SWITCH12, YELLOW2, 4, type_momentary);
c_button button5(SWITCH22, RED2, 5, type_momentary);
c_button button6(SWITCH32, GREEN2, 6, type_momentary);
void buttonbegin(c_button *thisbutton)
{
thisbutton->begin();
buttonlist.push_back(thisbutton);
}
void initButtons(void)
{
buttonbegin(&button1);
buttonbegin(&button2);
buttonbegin(&button3);
buttonbegin(&button4);
buttonbegin(&button5);
buttonbegin(&button6);
}
void handleButtons(void)
{
for (auto &&i : buttonlist)
{
i->read();
}
}
bool anybutton(void)
{
handleButtons();
for (auto &&i : buttonlist)
{
if (i->isPressed())
{
return true;
}
}
return false;
}
bool anyButtonChanged(void)
{
handleButtons();
for (auto &&i : buttonlist)
{
if (i->isChanged())
{
return true;
}
}
return false;
}
bool allButtons(void)
{
for (auto &&thisbutton : buttonlist)
{
if (thisbutton->getType() == type_switch)
{
if (!thisbutton->isPressed())
{
return false;
}
}
}
return true;
}
bool onlyButton(e_ledcolor color)
{
for (auto &&thisbutton : buttonlist)
{
if (thisbutton->isPressed() && thisbutton->getColor() != color)
{
return false;
}
}
return getButton(color)->isPressed();
}
bool buttonIsPressed(e_ledcolor index)
{
c_button *thisbutton = getButton(index);
if (thisbutton == NULL)
{
return false;
}
else
{
return thisbutton->isPressed();
}
}
bool buttonIsPressed(uint16_t index)
{
c_button *thisbutton = getButton(index);
if (thisbutton == NULL)
{
return false;
}
else
{
return thisbutton->isPressed();
}
}
bool buttonWasPressed(e_ledcolor index)
{
c_button *thisbutton = getButton(index);
if (thisbutton == NULL)
{
return false;
}
else
{
return thisbutton->wasPressed();
}
}
c_button *getButton(unsigned int index)
{
if (index > buttonlist.size())
{
return NULL;
}
return buttonlist[index - 1];
}
c_button *getButton(e_ledcolor color)
{
for (auto &&button : buttonlist)
{
if (button->getColor() == color)
{
return button;
}
}
return NULL;
}
std::vector<c_button *> *getButtonlist(void)
{
return &buttonlist;
}
uint8_t buttonPressedCount(void)
{
uint8_t count = 0;
for (auto &&button : buttonlist)
{
if (button != NULL)
{
if (button->isPressed())
{
count++;
}
}
}
return count;
}