From 0bf20552cfd85f30e77b6b78e27c85c08e775d31 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Mon, 17 Oct 2022 09:15:21 +0300 Subject: [PATCH] switch-controller: add basic switch listener --- SwitchController/inc/SwitchController.h | 4 +++- SwitchController/src/SwitchController.cpp | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/SwitchController/inc/SwitchController.h b/SwitchController/inc/SwitchController.h index e5ba894..cdede07 100644 --- a/SwitchController/inc/SwitchController.h +++ b/SwitchController/inc/SwitchController.h @@ -15,7 +15,8 @@ class SwitchController { public: - SwitchController (DigitalIoPin *button, Timer *timer, StateHandler *handler); + SwitchController (DigitalIoPin *button, Timer *timer, StateHandler *handler, + int button_mode); virtual ~SwitchController (); /** Listen to switch button */ @@ -26,6 +27,7 @@ private: Timer *t; StateHandler *h; bool b_state; + int b_mode; }; #endif /* SWITCHCONTROLLER_H_ */ diff --git a/SwitchController/src/SwitchController.cpp b/SwitchController/src/SwitchController.cpp index c0fab75..3beb520 100644 --- a/SwitchController/src/SwitchController.cpp +++ b/SwitchController/src/SwitchController.cpp @@ -8,9 +8,14 @@ #include SwitchController::SwitchController (DigitalIoPin *button, Timer *timer, - StateHandler *handler) + StateHandler *handler, int button_mode) { - // TODO Auto-generated constructor stub + b = button; + t = timer; + h = handler; + b_state = false; + b_mode = button_mode; + t->resetCounter (); } SwitchController::~SwitchController () @@ -21,4 +26,13 @@ SwitchController::~SwitchController () void SwitchController::listen () { + if (b->read ()) + { + b_state = true; + } + if (!b->read () && b_state) + { + h->HandleState (Event (Event::eKey, b_mode)); + b_state = false; + } } \ No newline at end of file