diff --git a/SwitchController/inc/SwitchController.h b/SwitchController/inc/SwitchController.h index cdede07..3af237a 100644 --- a/SwitchController/inc/SwitchController.h +++ b/SwitchController/inc/SwitchController.h @@ -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_ */ diff --git a/SwitchController/src/SwitchController.cpp b/SwitchController/src/SwitchController.cpp index 53fac52..e294417 100644 --- a/SwitchController/src/SwitchController.cpp +++ b/SwitchController/src/SwitchController.cpp @@ -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 (); } -} \ No newline at end of file + /** 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); +} diff --git a/Timer/src/Timer.cpp b/Timer/src/Timer.cpp index e41a10d..a1ae8dc 100644 --- a/Timer/src/Timer.cpp +++ b/Timer/src/Timer.cpp @@ -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); }