Compare commits

..

No commits in common. "dac64a2d64fed7343799772c0704f01dd9de9a16" and "0d07b98cfdf48850727ef10361cd711a83f9dd8a" have entirely different histories.

11 changed files with 202 additions and 163 deletions

View File

@ -9,13 +9,8 @@
#include "trace/trace.h" #include "trace/trace.h"
#include "ButtonEvent.h" #include "ButtonEvent.h"
ButtonEventsLogger::ButtonEventsLogger() ButtonEventsLogger::ButtonEventsLogger() {
: stWait(this) { curentState_ = state::initial;
stInitial.addInitialTransition(&stWait);
stWait.addTransition(event::evButtonShortPressed, &stWait);
stWait.addTransition(event::evButtonLongPressed, &stWait);
stWait.registerOnEntry((XFState::callbackEv)&ButtonEventsLogger::logOnTrace);
} }
void ButtonEventsLogger::onButtonShortPressed(ButtonIndex buttonIndex) { void ButtonEventsLogger::onButtonShortPressed(ButtonIndex buttonIndex) {
@ -26,13 +21,62 @@ void ButtonEventsLogger::onButtonLongPressed(ButtonIndex buttonIndex) {
GEN(ButtonEvent(event::evButtonLongPressed, this, buttonIndex)); GEN(ButtonEvent(event::evButtonLongPressed, this, buttonIndex));
} }
void ButtonEventsLogger::logOnTrace(const XFEvent* ev) { XFEventStatus ButtonEventsLogger::processEvent() {
ButtonEvent* buttonEvent = (ButtonEvent*)ev; eEventStatus eventStatus = XFEventStatus::NotConsumed;
int evid = buttonEvent->getId();
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){ if(evid == event::evButtonShortPressed){
Trace::out("ButtonEventLogger: Button %d short pressed", buttonEvent->getButtonId()); Trace::out("ButtonEventLogger: Button %d short pressed", ev->getButtonId());
curentState_ = state::waitButtonPressed;
changeXFState = true;
eventStatus = XFEventStatus::Consumed;
} }
if(evid == event::evButtonLongPressed){ if(evid == event::evButtonLongPressed){
Trace::out("ButtonEventLogger: Button %d long pressed", buttonEvent->getButtonId()); Trace::out("ButtonEventLogger: Button %d long pressed", ev->getButtonId());
curentState_ = state::waitButtonPressed;
changeXFState = true;
eventStatus = XFEventStatus::Consumed;
} }
break;
} }
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;
}
return eventStatus;
}

View File

@ -21,14 +21,20 @@ public:
protected: protected:
void logOnTrace(const XFEvent* ev); XFEventStatus processEvent() override;
typedef enum { typedef enum {
evButtonShortPressed = 10, evButtonShortPressed = 1,
evButtonLongPressed evButtonLongPressed
} event; } event;
XFState stWait; typedef enum {
initial,
waitButtonPressed
} state;
state curentState_;
state oldState_;
}; };

View File

@ -16,12 +16,12 @@ ButtonEventsHandler::ButtonEventsHandler()
observer_[i] = nullptr; observer_[i] = nullptr;
} }
stInitial.addInitialTransition(&stRun); stInitial.setNextState(&stRun);
stRun.registerOnEntry((XFState::callback)&ButtonEventsHandler::startButtonSM); stRun.registerOnEntry((XFState::callback)&ButtonEventsHandler::startButtonSM);
} }
void ButtonEventsHandler::startButtonSM() { void ButtonEventsHandler::startButtonSM(const XFEvent* ev) {
buttonStateSm_[0].startBehavior(); buttonStateSm_[0].startBehavior();
buttonStateSm_[1].startBehavior(); buttonStateSm_[1].startBehavior();
buttonStateSm_[2].startBehavior(); buttonStateSm_[2].startBehavior();

View File

@ -31,7 +31,7 @@ public:
protected: protected:
ButtonEventsHandler(); ButtonEventsHandler();
void startButtonSM(); void startButtonSM(const XFEvent* ev);
XFState stRun; XFState stRun;

View File

@ -8,27 +8,10 @@
#include <button/ButtonStateSm.h> #include <button/ButtonStateSm.h>
#include "mdw/button/ButtonEventsHandler.h" #include "mdw/button/ButtonEventsHandler.h"
//TODO start State Machine. For that, turn ButtonEventsHandler to a State Machine (Initial, run)
ButtonStateSm::ButtonStateSm() ButtonStateSm::ButtonStateSm()
: buttonIndex_(getNbInstance()), : buttonIndex_(getNbInstance()) {
stWaitButtonPressed(this), currentState_ = state::initial;
stButtonPressed(this),
stButtonShortPressed(this),
stButtonLongPressed(this) {
stInitial.addInitialTransition(&stWaitButtonPressed);
stWaitButtonPressed.addTransition(event::evButtonPressed, &stButtonPressed);
// TODO bug if setTimeout is used before addTransition !!!
stButtonPressed.addTransition(event::evButtonReleased, &stButtonShortPressed);
stButtonPressed.setTimeout(1000, &stButtonLongPressed);
stButtonPressed.registerOnExit((XFState::callback)&ButtonStateSm::genNullTransition);
stButtonShortPressed.registerOnEntry((XFState::callback)&ButtonStateSm::notifyShortPress);
stButtonShortPressed.addTransition(event::evReturn, &stWaitButtonPressed);
stButtonLongPressed.registerOnEntry((XFState::callback)&ButtonStateSm::notifyLongPress);
stButtonLongPressed.addTransition(event::evReturn, &stWaitButtonPressed);
} }
uint8_t ButtonStateSm::getNbInstance(){ uint8_t ButtonStateSm::getNbInstance(){
@ -43,14 +26,102 @@ void ButtonStateSm::genButtonReleased() {
GEN(XFEvent(XFEvent::Event, event::evButtonReleased, this)); GEN(XFEvent(XFEvent::Event, event::evButtonReleased, this));
} }
void ButtonStateSm::notifyShortPress() { XFEventStatus ButtonStateSm::processEvent() {
eEventStatus eventStatus = XFEventStatus::Unknown;
const XFEvent* ev = getCurrentEvent();
XFEvent::XFEventType evType = ev->getEventType();
int evid = ev->getId();
oldState_ = currentState_;
changeState_ = false;
switch (currentState_) {
case state::initial:
if(evType == XFEvent::Initial) {
currentState_ = state::waitButtonPressed;
changeState_ = true;
eventStatus = XFEventStatus::Consumed;
}
break;
case state::waitButtonPressed:
if(evid == event::evButtonPressed) {
currentState_ = state::buttonPressed;
changeState_ = true;
eventStatus = XFEventStatus::Consumed;
scheduleTimeout(event::evTimeout, 1000);
}
break;
case state::buttonPressed:
if(evid == event::evButtonReleased) {
currentState_ = state::buttonShortPressed;
changeState_ = true;
eventStatus = XFEventStatus::Consumed;
unscheduleTimeout(event::evTimeout);
}
if(evid == event::evTimeout) {
currentState_ = state::buttonLongPressed;
changeState_ = true;
eventStatus = XFEventStatus::Consumed;
}
GEN(XFNullTransition(this));
break;
case state::buttonShortPressed:
if(evType == XFEvent::XFEventType::NullTransition) {
currentState_ = state::waitButtonPressed;
changeState_ = true;
eventStatus = XFEventStatus::Consumed;
}
break;
case state::buttonLongPressed:
if(evType == XFEvent::XFEventType::NullTransition) {
currentState_ = state::waitButtonPressed;
changeState_ = true;
eventStatus = XFEventStatus::Consumed;
}
break;
}
if(changeState_) {
switch (oldState_) { // onExit
case state::initial:
break;
case state::waitButtonPressed:
break;
case state::buttonPressed:
break;
case state::buttonShortPressed:
break;
case state::buttonLongPressed:
break;
}
switch (currentState_) {
case state::initial:
break;
case state::waitButtonPressed:
break;
case state::buttonPressed:
break;
case state::buttonShortPressed:
ButtonEventsHandler::getInstance()->notifyButtonShortPressed(buttonIndex_); ButtonEventsHandler::getInstance()->notifyButtonShortPressed(buttonIndex_);
} break;
void ButtonStateSm::notifyLongPress() { case state::buttonLongPressed:
ButtonEventsHandler::getInstance()->notifyButtonLongPressed(buttonIndex_); ButtonEventsHandler::getInstance()->notifyButtonLongPressed(buttonIndex_);
break;
} }
}
void ButtonStateSm::genNullTransition() { return eventStatus;
GEN(XFEvent(XFEvent::Event, event::evReturn, this));
} }

View File

@ -18,23 +18,26 @@ public:
void genButtonReleased(); void genButtonReleased();
protected: protected:
void notifyShortPress(); XFEventStatus processEvent();
void notifyLongPress();
void genNullTransition();
typedef enum { typedef enum {
evButtonPressed = 10, evButtonPressed,
evButtonReleased, evButtonReleased,
evTimeout, evTimeout
evReturn
} event; } event;
const uint8_t buttonIndex_; typedef enum {
initial,
waitButtonPressed,
buttonPressed,
buttonShortPressed,
buttonLongPressed
} state;
XFState stWaitButtonPressed; state currentState_;
XFState stButtonPressed; state oldState_;
XFState stButtonShortPressed; bool changeState_;
XFState stButtonLongPressed; const uint8_t buttonIndex_;
private: private:
static uint8_t getNbInstance(); static uint8_t getNbInstance();

View File

@ -16,8 +16,8 @@ ButtonsController::ButtonsController()
cbProvider = nullptr; cbProvider = nullptr;
cbMethod = nullptr; cbMethod = nullptr;
stInitial.addInitialTransition(&stCheckButtons); stInitial.setNextState(&stCheckButtons);
stCheckButtons.addTransition(event::evButtonIrq, &stDebounce); stCheckButtons.setNextState(event::evButtonIrq, &stDebounce);
stCheckButtons.registerOnEntry((XFState::callback)&ButtonsController::fctCheckButtons); stCheckButtons.registerOnEntry((XFState::callback)&ButtonsController::fctCheckButtons);
stDebounce.setTimeout(20, &stCheckButtons); stDebounce.setTimeout(20, &stCheckButtons);
@ -45,7 +45,7 @@ bool ButtonsController::registerCallback(
return true; return true;
} }
void ButtonsController::fctCheckButtons() { void ButtonsController::fctCheckButtons(const XFEvent* ev) {
if(cbProvider != nullptr) { if(cbProvider != nullptr) {
newState[0] = HAL_GPIO_ReadPin(BUTTON0_GPIO_Port, BUTTON0_Pin); newState[0] = HAL_GPIO_ReadPin(BUTTON0_GPIO_Port, BUTTON0_Pin);

View File

@ -30,7 +30,8 @@ protected:
bool newState[4]; bool newState[4];
bool buttonState[4]; bool buttonState[4];
void fctCheckButtons(); void fctCheckButtons(const XFEvent* ev);
void fctScheduleTimeout(const XFEvent* ev);
XFState stCheckButtons; XFState stCheckButtons;
XFState stDebounce; XFState stDebounce;

View File

@ -14,15 +14,9 @@
XFState::XFState(XFBehavior* behavior) XFState::XFState(XFBehavior* behavior)
: pBehavior(behavior) { : pBehavior(behavior) {
assert(behavior != nullptr); assert(behavior != nullptr);
cbState_ = nullptr;
cbEntry_ = nullptr;
cbExit_ = nullptr;
cbEvState_ = nullptr;
cbEvEntry_ = nullptr;
cbEvExit_ = nullptr;
} }
void XFState::addTransition(XFEvent::XFEventType type, const int evid, const int time, XFState* state) { void XFState::setNextState(const int evid, const int time, XFEvent::XFEventType type, XFState* state) {
assert(state != nullptr); assert(state != nullptr);
transition t; transition t;
t.evid = evid; t.evid = evid;
@ -34,14 +28,11 @@ void XFState::addTransition(XFEvent::XFEventType type, const int evid, const int
XFEventStatus XFState::onState(const XFEvent* ev) { XFEventStatus XFState::onState(const XFEvent* ev) {
if(cbState_ != nullptr) { assert(ev != nullptr);
(pBehavior->*cbState_)(); assert(pBehavior != nullptr);
}
if(cbEvState_ != nullptr) {
(pBehavior->*cbEvState_)(ev);
}
for(transition t : transitions_) { for(transition t : transitions_) {
assert(t.nextState != nullptr);
if(t.evType == XFEvent::XFEventType::Initial){ if(t.evType == XFEvent::XFEventType::Initial){
pBehavior->curentXFState_ = t.nextState; pBehavior->curentXFState_ = t.nextState;
@ -68,14 +59,11 @@ XFEventStatus XFState::onState(const XFEvent* ev) {
void XFState::onEntry(const XFEvent* ev) { void XFState::onEntry(const XFEvent* ev) {
if(cbEntry_ != nullptr) { if(cbEntry_ != nullptr) {
(pBehavior->*cbEntry_)(); (pBehavior->*cbEntry_)(ev);
}
if(cbEvEntry_ != nullptr) {
(pBehavior->*cbEvEntry_)(ev);
} }
for(transition t : transitions_) { for(transition t : transitions_) {
assert(t.nextState != nullptr);
if(t.evType == XFEvent::XFEventType::Timeout) { if(t.evType == XFEvent::XFEventType::Timeout) {
pBehavior->scheduleTimeout(XFEvent::Timeout, t.time); pBehavior->scheduleTimeout(XFEvent::Timeout, t.time);
@ -85,19 +73,6 @@ void XFState::onEntry(const XFEvent* ev) {
} }
void XFState::onExit(const XFEvent* ev) { void XFState::onExit(const XFEvent* ev) {
if(cbExit_ != nullptr) { if(cbExit_ != nullptr) {
(pBehavior->*cbExit_)(); (pBehavior->*cbExit_)(ev);
}
if(cbEvExit_ != nullptr) {
(pBehavior->*cbEvExit_)(ev);
}
for(transition t: transitions_) {
if(t.evType == XFEvent::XFEventType::Timeout) {
if(ev->getEventType() != XFEvent::XFEventType::Timeout) {
pBehavior->unscheduleTimeout(XFEvent::Timeout);
}
}
} }
} }

View File

@ -21,66 +21,20 @@ class XFBehavior;
class XFState { class XFState {
friend class XFBehavior; friend class XFBehavior;
public: public:
/**
* Create and initialise a State for the behavior.
* Have to be initialized in the constructor of the behavior.
* @param behavior Pointer to the behavior class. Use the keyword this
*/
XFState(XFBehavior* behavior); XFState(XFBehavior* behavior);
~XFState() = default; ~XFState() = default;
/** inline void setNextState(XFState* state) { setNextState(XFEvent::Initial, XFEvent::XFEventType::Initial, state); };
* Set the conditional state of an custom event. inline void setNextState(const int evid, XFState* state) { setNextState(evid, XFEvent::XFEventType::Event, state); };
* @param evid Id of the event for set the next State
* @param state Pointer to the next state
*/
inline void addTransition(const int evid, XFState* state) { addTransition(XFEvent::XFEventType::Event, evid, 0, state); };
/** inline void setTimeout(int time, XFState* nextState) { setNextState(XFEvent::Timeout, time, XFEvent::XFEventType::Timeout, nextState); };
* Set a new timer with a certain duration on every entry of this state. typedef void (XFBehavior::*callback)(const XFEvent* ev);
* Set next state after
* @param time Time duration of the timeout
* @param nextState Pointer to the next state
*/
inline void setTimeout(int time, XFState* nextState) { addTransition(XFEvent::XFEventType::Timeout, XFEvent::Timeout, time, nextState); };
// Callback typedef for callback without parameter
typedef void (XFBehavior::*callback)();
//inline void registerOnState(callback cbState) { cbState_ = cbState; };
/**
* Register the method for the entry callback without parameter
* @param cbEntry Callback automatically called when the state is onEntry
*/
inline void registerOnEntry(callback cbEntry) { cbEntry_ = cbEntry; }; inline void registerOnEntry(callback cbEntry) { cbEntry_ = cbEntry; };
/**
* Register the method for the exit callback without parameter
* @param cbExit Callback automatically called when the state is onExit
*/
inline void registerOnExit(callback cbExit) { cbExit_ = cbExit; }; inline void registerOnExit(callback cbExit) { cbExit_ = cbExit; };
//Callback typedef for callback with event in parameter
typedef void (XFBehavior::*callbackEv)(const XFEvent* ev);
//inline void registerOnState(callbackEv cbState) { cbEvState_ = cbState; };
/**
* Register the method for the entry callback with event in parameter.
* @param cbEntry Callback automatically called when the state is onEntry
*/
inline void registerOnEntry(callbackEv cbEntry) { cbEvEntry_ = cbEntry; };
/**
* Register the method for the exit callback with event in parameter
* @param cbExit Callback automatically called when the state is onExit
*/
inline void registerOnExit(callbackEv cbExit) { cbEvExit_ = cbExit; };
protected: protected:
void addTransition(XFEvent::XFEventType type, const int evid, const int time, XFState* state); inline void setNextState(const int evid, XFEvent::XFEventType type, XFState* state) {setNextState(evid, 0, type, state); };
void setNextState(const int evid, const int time, XFEvent::XFEventType type, XFState* state);
XFEventStatus onState(const XFEvent* ev); XFEventStatus onState(const XFEvent* ev);
void onEntry(const XFEvent* ev); void onEntry(const XFEvent* ev);
@ -96,24 +50,9 @@ protected:
} transition; } transition;
std::list<transition> transitions_; std::list<transition> transitions_;
callback cbState_;
callback cbEntry_; callback cbEntry_;
callback cbExit_; callback cbExit_;
callbackEv cbEvState_;
callbackEv cbEvEntry_;
callbackEv cbEvExit_;
}; };
class XFInitialState : public XFState {
public:
/**
* Use for set next state of the Initial State.
* @param state Pointer to the next state
*/
inline XFInitialState(XFBehavior* behavior) : XFState(behavior) {};
~XFInitialState() = default;
inline void addInitialTransition(XFState* state) { addTransition(XFEvent::XFEventType::Initial, XFEvent::Initial, 0, state); };
};
#endif /* XFSTATE_H_ */ #endif /* XFSTATE_H_ */

View File

@ -100,7 +100,7 @@ protected:
const XFEvent * pCurrentEvent_; ///< Reference to actually processed event. const XFEvent * pCurrentEvent_; ///< Reference to actually processed event.
protected: protected:
XFInitialState stInitial; XFState stInitial;
XFState* curentXFState_ = &stInitial; XFState* curentXFState_ = &stInitial;
XFState* oldXFState_ = &stInitial; XFState* oldXFState_ = &stInitial;
bool changeXFState = false; bool changeXFState = false;