switch-controller: resolve #21

This commit is contained in:
Vasily Davydov 2022-10-17 13:36:08 +03:00
parent 9358ede4df
commit b8780772a1
3 changed files with 45 additions and 5 deletions

View File

@ -28,6 +28,8 @@ private:
StateHandler *h;
bool b_state;
int b_mode;
void buttonOnHold ();
void buttonInLoop ();
};
#endif /* SWITCHCONTROLLER_H_ */

View File

@ -25,13 +25,51 @@ SwitchController::~SwitchController ()
void
SwitchController::listen ()
{
if (b->read ())
int timer = t->getCounter ();
/** Button is pressed for the first time*/
if (b->read () && !b_state)
{
t->resetCounter ();
b_state = true;
}
if (!b->read () && b_state)
/** Button is released before 2 seconds*/
if (!b->read () && b_state && timer < 2000)
{
h->HandleState (Event (Event::eKey, b_mode));
b_state = false;
t->resetCounter ();
}
/** Button is pressed after 2 seconds*/
if (b->read () && b_state && timer >= 2000)
{
buttonOnHold ();
}
}
void
SwitchController::buttonOnHold ()
{
t->resetCounter ();
while (b->read ())
{
buttonInLoop ();
}
if (b_mode == BUTTON_CONTROL_TOG_MODE)
{
h->HandleState (Event (Event::eKey, b_mode));
}
b_state = false;
t->resetCounter ();
}
void
SwitchController::buttonInLoop ()
{
if (t->getCounter () > 50 && b_mode != BUTTON_CONTROL_TOG_MODE)
{
h->HandleState (Event (Event::eKey, b_mode));
h->HandleState (Event (Event::eTick));
t->resetCounter ();
}
t->tickCounter (2);
}

View File

@ -55,11 +55,11 @@ Timer::Sleep (int ms)
int
Timer::getCounter ()
{
return counter;
return counter.load (std::memory_order_relaxed);
}
void
Timer::resetCounter ()
{
counter = 0;
counter.store (0, std::memory_order_relaxed);
}