From b8780772a1f2f50c8f6aca8207156156bf403e0d Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Mon, 17 Oct 2022 13:36:08 +0300 Subject: [PATCH] switch-controller: resolve #21 --- SwitchController/inc/SwitchController.h | 2 ++ SwitchController/src/SwitchController.cpp | 44 +++++++++++++++++++++-- Timer/src/Timer.cpp | 4 +-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/SwitchController/inc/SwitchController.h b/SwitchController/inc/SwitchController.h index cdede07..5a9b147 100644 --- a/SwitchController/inc/SwitchController.h +++ b/SwitchController/inc/SwitchController.h @@ -28,6 +28,8 @@ private: StateHandler *h; bool b_state; int b_mode; + void buttonOnHold (); + void buttonInLoop (); }; #endif /* SWITCHCONTROLLER_H_ */ diff --git a/SwitchController/src/SwitchController.cpp b/SwitchController/src/SwitchController.cpp index 53fac52..e0e75e0 100644 --- a/SwitchController/src/SwitchController.cpp +++ b/SwitchController/src/SwitchController.cpp @@ -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 (); } -} \ No newline at end of file + /** 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); +} 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); }