From 0ccfd522ee9ae8e9509c73e1ac885edb8e60ea58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Mon, 27 Nov 2023 08:06:53 +0100 Subject: [PATCH 1/2] Improve CallBacks --- src/app/buttoneventslogger.cpp | 72 ++++--------------- src/app/buttoneventslogger.h | 12 +--- src/mdw/button/ButtonEventsHandler.cpp | 2 +- src/mdw/button/ButtonEventsHandler.h | 2 +- .../f7-disco-gcc/board/ButtonsController.cpp | 2 +- .../f7-disco-gcc/board/ButtonsController.h | 3 +- src/xf/core/XFState.cpp | 17 ++++- src/xf/include/xf/XFState.h | 15 +++- 8 files changed, 48 insertions(+), 77 deletions(-) diff --git a/src/app/buttoneventslogger.cpp b/src/app/buttoneventslogger.cpp index 98a0312..c6d92c3 100644 --- a/src/app/buttoneventslogger.cpp +++ b/src/app/buttoneventslogger.cpp @@ -9,8 +9,13 @@ #include "trace/trace.h" #include "ButtonEvent.h" -ButtonEventsLogger::ButtonEventsLogger() { - curentState_ = state::initial; +ButtonEventsLogger::ButtonEventsLogger() + : stWait(this) { + + stInitial.setNextState(&stWait); + stWait.setNextState(event::evButtonShortPressed, &stWait); + stWait.setNextState(event::evButtonLongPressed, &stWait); + stWait.registerOnEntry((XFState::callbackEv)&ButtonEventsLogger::logOnTrace); } void ButtonEventsLogger::onButtonShortPressed(ButtonIndex buttonIndex) { @@ -21,62 +26,13 @@ void ButtonEventsLogger::onButtonLongPressed(ButtonIndex buttonIndex) { GEN(ButtonEvent(event::evButtonLongPressed, this, buttonIndex)); } -XFEventStatus ButtonEventsLogger::processEvent() { - eEventStatus eventStatus = XFEventStatus::NotConsumed; - - ButtonEvent* ev = (ButtonEvent*)getCurrentEvent(); - XFEvent::XFEventType evType = ev->getEventType(); - int evid = ev->getId(); - - oldState_ = curentState_; - changeXFState = false; - - - - switch (curentState_) { //on state - case state::initial: - if(evType == XFEvent::Initial) { - curentState_ = state::waitButtonPressed; - changeXFState = true; - eventStatus = XFEventStatus::Consumed; - } - break; - - case state::waitButtonPressed: - if(evid == event::evButtonShortPressed){ - Trace::out("ButtonEventLogger: Button %d short pressed", ev->getButtonId()); - curentState_ = state::waitButtonPressed; - changeXFState = true; - eventStatus = XFEventStatus::Consumed; - } - if(evid == event::evButtonLongPressed){ - Trace::out("ButtonEventLogger: Button %d long pressed", ev->getButtonId()); - curentState_ = state::waitButtonPressed; - changeXFState = true; - eventStatus = XFEventStatus::Consumed; - } - break; +void ButtonEventsLogger::logOnTrace(const XFEvent* ev) { + ButtonEvent* buttonEvent = (ButtonEvent*)ev; + int evid = buttonEvent->getId(); + if(evid == event::evButtonShortPressed){ + Trace::out("ButtonEventLogger: Button %d short pressed", buttonEvent->getButtonId()); } - - if(changeXFState) { - switch (oldState_) { // onExit - case state::initial: - break; - - case state::waitButtonPressed: - break; - } - - switch (curentState_) { // onEntry - case state::initial: - break; - - case state::waitButtonPressed: - break; - - } - changeXFState = false; + if(evid == event::evButtonLongPressed){ + Trace::out("ButtonEventLogger: Button %d long pressed", buttonEvent->getButtonId()); } - return eventStatus; } - diff --git a/src/app/buttoneventslogger.h b/src/app/buttoneventslogger.h index 73e99e8..3f533a3 100644 --- a/src/app/buttoneventslogger.h +++ b/src/app/buttoneventslogger.h @@ -21,20 +21,14 @@ public: protected: - XFEventStatus processEvent() override; + void logOnTrace(const XFEvent* ev); typedef enum { - evButtonShortPressed = 1, + evButtonShortPressed = 10, evButtonLongPressed } event; - typedef enum { - initial, - waitButtonPressed - } state; - - state curentState_; - state oldState_; + XFState stWait; }; diff --git a/src/mdw/button/ButtonEventsHandler.cpp b/src/mdw/button/ButtonEventsHandler.cpp index b6a88c3..47ca17a 100644 --- a/src/mdw/button/ButtonEventsHandler.cpp +++ b/src/mdw/button/ButtonEventsHandler.cpp @@ -21,7 +21,7 @@ ButtonEventsHandler::ButtonEventsHandler() } -void ButtonEventsHandler::startButtonSM(const XFEvent* ev) { +void ButtonEventsHandler::startButtonSM() { buttonStateSm_[0].startBehavior(); buttonStateSm_[1].startBehavior(); buttonStateSm_[2].startBehavior(); diff --git a/src/mdw/button/ButtonEventsHandler.h b/src/mdw/button/ButtonEventsHandler.h index db58690..00ee768 100644 --- a/src/mdw/button/ButtonEventsHandler.h +++ b/src/mdw/button/ButtonEventsHandler.h @@ -31,7 +31,7 @@ public: protected: ButtonEventsHandler(); - void startButtonSM(const XFEvent* ev); + void startButtonSM(); XFState stRun; diff --git a/src/platform/f7-disco-gcc/board/ButtonsController.cpp b/src/platform/f7-disco-gcc/board/ButtonsController.cpp index 09ad8ff..d6e822c 100644 --- a/src/platform/f7-disco-gcc/board/ButtonsController.cpp +++ b/src/platform/f7-disco-gcc/board/ButtonsController.cpp @@ -45,7 +45,7 @@ bool ButtonsController::registerCallback( return true; } -void ButtonsController::fctCheckButtons(const XFEvent* ev) { +void ButtonsController::fctCheckButtons() { if(cbProvider != nullptr) { newState[0] = HAL_GPIO_ReadPin(BUTTON0_GPIO_Port, BUTTON0_Pin); diff --git a/src/platform/f7-disco-gcc/board/ButtonsController.h b/src/platform/f7-disco-gcc/board/ButtonsController.h index 2dd4c68..3f425fd 100644 --- a/src/platform/f7-disco-gcc/board/ButtonsController.h +++ b/src/platform/f7-disco-gcc/board/ButtonsController.h @@ -30,8 +30,7 @@ protected: bool newState[4]; bool buttonState[4]; - void fctCheckButtons(const XFEvent* ev); - void fctScheduleTimeout(const XFEvent* ev); + void fctCheckButtons(); XFState stCheckButtons; XFState stDebounce; diff --git a/src/xf/core/XFState.cpp b/src/xf/core/XFState.cpp index 5f257c6..3b22dd3 100644 --- a/src/xf/core/XFState.cpp +++ b/src/xf/core/XFState.cpp @@ -31,6 +31,13 @@ XFEventStatus XFState::onState(const XFEvent* ev) { assert(ev != nullptr); assert(pBehavior != nullptr); + if(cbState_ != nullptr) { + (pBehavior->*cbState_)(); + } + if(cbEvState_ != nullptr) { + (pBehavior->*cbEvState_)(ev); + } + for(transition t : transitions_) { assert(t.nextState != nullptr); @@ -59,7 +66,10 @@ XFEventStatus XFState::onState(const XFEvent* ev) { void XFState::onEntry(const XFEvent* ev) { if(cbEntry_ != nullptr) { - (pBehavior->*cbEntry_)(ev); + (pBehavior->*cbEntry_)(); + } + if(cbEvEntry_ != nullptr) { + (pBehavior->*cbEvEntry_)(ev); } for(transition t : transitions_) { @@ -73,6 +83,9 @@ void XFState::onEntry(const XFEvent* ev) { } void XFState::onExit(const XFEvent* ev) { if(cbExit_ != nullptr) { - (pBehavior->*cbExit_)(ev); + (pBehavior->*cbExit_)(); + } + if(cbEvExit_ != nullptr) { + (pBehavior->*cbEvExit_)(ev); } } diff --git a/src/xf/include/xf/XFState.h b/src/xf/include/xf/XFState.h index d10eed0..695d0f4 100644 --- a/src/xf/include/xf/XFState.h +++ b/src/xf/include/xf/XFState.h @@ -28,9 +28,14 @@ public: inline void setNextState(const int evid, XFState* state) { setNextState(evid, XFEvent::XFEventType::Event, state); }; inline void setTimeout(int time, XFState* nextState) { setNextState(XFEvent::Timeout, time, XFEvent::XFEventType::Timeout, nextState); }; - typedef void (XFBehavior::*callback)(const XFEvent* ev); + typedef void (XFBehavior::*callback)(); + typedef void (XFBehavior::*callbackEv)(const XFEvent* ev); + //inline void registerOnState(callback cbState) { cbState_ = cbState; }; inline void registerOnEntry(callback cbEntry) { cbEntry_ = cbEntry; }; inline void registerOnExit(callback cbExit) { cbExit_ = cbExit; }; + //inline void registerOnState(callbackEv cbState) { cbEvState_ = cbState; }; + inline void registerOnEntry(callbackEv cbEntry) { cbEvEntry_ = cbEntry; }; + inline void registerOnExit(callbackEv cbExit) { cbEvExit_ = cbExit; }; protected: inline void setNextState(const int evid, XFEvent::XFEventType type, XFState* state) {setNextState(evid, 0, type, state); }; @@ -50,8 +55,12 @@ protected: } transition; std::list transitions_; - callback cbEntry_; - callback cbExit_; + callback cbState_ = nullptr; + callback cbEntry_ = nullptr; + callback cbExit_ = nullptr; + callbackEv cbEvState_ = nullptr; + callbackEv cbEvEntry_ = nullptr; + callbackEv cbEvExit_ = nullptr; }; From 2c5fa52b9f7dbf2ec111a0a7d126a9b9c4b58c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Mon, 27 Nov 2023 17:07:15 +0100 Subject: [PATCH 2/2] Implement unscheduleTimeout & XFInitialState --- src/app/buttoneventslogger.cpp | 6 +- src/mdw/button/ButtonEventsHandler.cpp | 2 +- src/mdw/button/ButtonStateSm.cpp | 131 ++++-------------- src/mdw/button/ButtonStateSm.h | 25 ++-- .../f7-disco-gcc/board/ButtonsController.cpp | 4 +- src/xf/core/XFState.cpp | 24 +++- src/xf/include/xf/XFState.h | 27 ++-- src/xf/include/xf/behavior.h | 2 +- 8 files changed, 82 insertions(+), 139 deletions(-) diff --git a/src/app/buttoneventslogger.cpp b/src/app/buttoneventslogger.cpp index c6d92c3..0ec8c3f 100644 --- a/src/app/buttoneventslogger.cpp +++ b/src/app/buttoneventslogger.cpp @@ -12,9 +12,9 @@ ButtonEventsLogger::ButtonEventsLogger() : stWait(this) { - stInitial.setNextState(&stWait); - stWait.setNextState(event::evButtonShortPressed, &stWait); - stWait.setNextState(event::evButtonLongPressed, &stWait); + stInitial.addInitialTransition(&stWait); + stWait.addTransition(event::evButtonShortPressed, &stWait); + stWait.addTransition(event::evButtonLongPressed, &stWait); stWait.registerOnEntry((XFState::callbackEv)&ButtonEventsLogger::logOnTrace); } diff --git a/src/mdw/button/ButtonEventsHandler.cpp b/src/mdw/button/ButtonEventsHandler.cpp index 47ca17a..6bdede2 100644 --- a/src/mdw/button/ButtonEventsHandler.cpp +++ b/src/mdw/button/ButtonEventsHandler.cpp @@ -16,7 +16,7 @@ ButtonEventsHandler::ButtonEventsHandler() observer_[i] = nullptr; } - stInitial.setNextState(&stRun); + stInitial.addInitialTransition(&stRun); stRun.registerOnEntry((XFState::callback)&ButtonEventsHandler::startButtonSM); } diff --git a/src/mdw/button/ButtonStateSm.cpp b/src/mdw/button/ButtonStateSm.cpp index e190eb7..20a5564 100644 --- a/src/mdw/button/ButtonStateSm.cpp +++ b/src/mdw/button/ButtonStateSm.cpp @@ -8,10 +8,27 @@ #include