switch-controller: add basic switch listener

This commit is contained in:
Vasily Davydov 2022-10-17 09:15:21 +03:00
parent c0c85df36f
commit 0bf20552cf
2 changed files with 19 additions and 3 deletions

View File

@ -15,7 +15,8 @@
class SwitchController class SwitchController
{ {
public: public:
SwitchController (DigitalIoPin *button, Timer *timer, StateHandler *handler); SwitchController (DigitalIoPin *button, Timer *timer, StateHandler *handler,
int button_mode);
virtual ~SwitchController (); virtual ~SwitchController ();
/** Listen to switch button /** Listen to switch button
*/ */
@ -26,6 +27,7 @@ private:
Timer *t; Timer *t;
StateHandler *h; StateHandler *h;
bool b_state; bool b_state;
int b_mode;
}; };
#endif /* SWITCHCONTROLLER_H_ */ #endif /* SWITCHCONTROLLER_H_ */

View File

@ -8,9 +8,14 @@
#include <SwitchController.h> #include <SwitchController.h>
SwitchController::SwitchController (DigitalIoPin *button, Timer *timer, 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 () SwitchController::~SwitchController ()
@ -21,4 +26,13 @@ SwitchController::~SwitchController ()
void void
SwitchController::listen () SwitchController::listen ()
{ {
if (b->read ())
{
b_state = true;
}
if (!b->read () && b_state)
{
h->HandleState (Event (Event::eKey, b_mode));
b_state = false;
}
} }