switch-controller: resolve #21
This commit is contained in:
commit
39c3fcd202
@ -26,8 +26,10 @@ private:
|
||||
DigitalIoPin *b;
|
||||
Timer *t;
|
||||
StateHandler *h;
|
||||
bool b_state;
|
||||
bool b_pressed;
|
||||
int b_mode;
|
||||
void buttonOnHold ();
|
||||
void buttonInLoop ();
|
||||
};
|
||||
|
||||
#endif /* SWITCHCONTROLLER_H_ */
|
||||
|
||||
@ -13,7 +13,7 @@ SwitchController::SwitchController (DigitalIoPin *button, Timer *timer,
|
||||
b = button;
|
||||
t = timer;
|
||||
h = handler;
|
||||
b_state = false;
|
||||
b_pressed = false;
|
||||
b_mode = button_mode;
|
||||
}
|
||||
|
||||
@ -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_pressed)
|
||||
{
|
||||
b_state = true;
|
||||
t->resetCounter ();
|
||||
b_pressed = true;
|
||||
}
|
||||
if (!b->read () && b_state)
|
||||
/** Button is released before 2 seconds*/
|
||||
if (!b->read () && b_pressed && timer < 2000)
|
||||
{
|
||||
h->HandleState (Event (Event::eKey, b_mode));
|
||||
b_state = false;
|
||||
b_pressed = false;
|
||||
t->resetCounter ();
|
||||
}
|
||||
}
|
||||
/** Button is pressed after 2 seconds*/
|
||||
if (b->read () && b_pressed && 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_pressed = 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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user