Improve CallBacks
This commit is contained in:
		| @@ -9,8 +9,13 @@ | |||||||
| #include "trace/trace.h" | #include "trace/trace.h" | ||||||
| #include "ButtonEvent.h" | #include "ButtonEvent.h" | ||||||
|  |  | ||||||
| ButtonEventsLogger::ButtonEventsLogger() { | ButtonEventsLogger::ButtonEventsLogger() | ||||||
| 	curentState_ = state::initial; | 	: 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) { | void ButtonEventsLogger::onButtonShortPressed(ButtonIndex buttonIndex) { | ||||||
| @@ -21,62 +26,13 @@ void ButtonEventsLogger::onButtonLongPressed(ButtonIndex buttonIndex) { | |||||||
| 	GEN(ButtonEvent(event::evButtonLongPressed, this, buttonIndex)); | 	GEN(ButtonEvent(event::evButtonLongPressed, this, buttonIndex)); | ||||||
| } | } | ||||||
|  |  | ||||||
| XFEventStatus ButtonEventsLogger::processEvent() { | void ButtonEventsLogger::logOnTrace(const XFEvent* ev) { | ||||||
| 	eEventStatus eventStatus = XFEventStatus::NotConsumed; | 	ButtonEvent* buttonEvent = (ButtonEvent*)ev; | ||||||
|  | 	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", ev->getButtonId()); | 			Trace::out("ButtonEventLogger: Button %d short pressed", buttonEvent->getButtonId()); | ||||||
| 					curentState_ = state::waitButtonPressed; |  | ||||||
| 					changeXFState = true; |  | ||||||
| 					eventStatus = XFEventStatus::Consumed; |  | ||||||
| 	} | 	} | ||||||
| 	if(evid == event::evButtonLongPressed){ | 	if(evid == event::evButtonLongPressed){ | ||||||
| 					Trace::out("ButtonEventLogger: Button %d long pressed", ev->getButtonId()); | 			Trace::out("ButtonEventLogger: Button %d long pressed", buttonEvent->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; |  | ||||||
| } |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -21,20 +21,14 @@ public: | |||||||
|  |  | ||||||
|  |  | ||||||
| protected: | protected: | ||||||
| 	XFEventStatus processEvent() override; | 	void logOnTrace(const XFEvent* ev); | ||||||
|  |  | ||||||
| 	typedef enum { | 	typedef enum { | ||||||
| 		evButtonShortPressed = 1, | 		evButtonShortPressed = 10, | ||||||
| 		evButtonLongPressed | 		evButtonLongPressed | ||||||
| 	} event; | 	} event; | ||||||
|  |  | ||||||
| 	typedef enum { | 	XFState stWait; | ||||||
| 		initial, |  | ||||||
| 		waitButtonPressed |  | ||||||
| 	} state; |  | ||||||
|  |  | ||||||
| 	state curentState_; |  | ||||||
| 	state oldState_; |  | ||||||
|  |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -21,7 +21,7 @@ ButtonEventsHandler::ButtonEventsHandler() | |||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void ButtonEventsHandler::startButtonSM(const XFEvent* ev) { | void ButtonEventsHandler::startButtonSM() { | ||||||
| 	buttonStateSm_[0].startBehavior(); | 	buttonStateSm_[0].startBehavior(); | ||||||
| 	buttonStateSm_[1].startBehavior(); | 	buttonStateSm_[1].startBehavior(); | ||||||
| 	buttonStateSm_[2].startBehavior(); | 	buttonStateSm_[2].startBehavior(); | ||||||
|   | |||||||
| @@ -31,7 +31,7 @@ public: | |||||||
|  |  | ||||||
| protected: | protected: | ||||||
| 	ButtonEventsHandler(); | 	ButtonEventsHandler(); | ||||||
| 	void startButtonSM(const XFEvent* ev); | 	void startButtonSM(); | ||||||
|  |  | ||||||
| 	XFState stRun; | 	XFState stRun; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -45,7 +45,7 @@ bool ButtonsController::registerCallback( | |||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| void ButtonsController::fctCheckButtons(const XFEvent* ev) { | void ButtonsController::fctCheckButtons() { | ||||||
| 	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); | ||||||
|   | |||||||
| @@ -30,8 +30,7 @@ protected: | |||||||
| 	bool newState[4]; | 	bool newState[4]; | ||||||
| 	bool buttonState[4]; | 	bool buttonState[4]; | ||||||
|  |  | ||||||
| 	void fctCheckButtons(const XFEvent* ev); | 	void fctCheckButtons(); | ||||||
| 	void fctScheduleTimeout(const XFEvent* ev); |  | ||||||
|  |  | ||||||
| 	XFState stCheckButtons; | 	XFState stCheckButtons; | ||||||
| 	XFState stDebounce; | 	XFState stDebounce; | ||||||
|   | |||||||
| @@ -31,6 +31,13 @@ XFEventStatus XFState::onState(const XFEvent* ev) { | |||||||
| 	assert(ev != nullptr); | 	assert(ev != nullptr); | ||||||
| 	assert(pBehavior != nullptr); | 	assert(pBehavior != nullptr); | ||||||
|  |  | ||||||
|  | 	if(cbState_ != nullptr) { | ||||||
|  | 		(pBehavior->*cbState_)(); | ||||||
|  | 	} | ||||||
|  | 	if(cbEvState_ != nullptr) { | ||||||
|  | 		(pBehavior->*cbEvState_)(ev); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	for(transition t : transitions_) { | 	for(transition t : transitions_) { | ||||||
| 		assert(t.nextState != nullptr); | 		assert(t.nextState != nullptr); | ||||||
|  |  | ||||||
| @@ -59,7 +66,10 @@ 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_)(ev); | 		(pBehavior->*cbEntry_)(); | ||||||
|  | 	} | ||||||
|  | 	if(cbEvEntry_ != nullptr) { | ||||||
|  | 		(pBehavior->*cbEvEntry_)(ev); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for(transition t : transitions_) { | 	for(transition t : transitions_) { | ||||||
| @@ -73,6 +83,9 @@ 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_)(ev); | 		(pBehavior->*cbExit_)(); | ||||||
|  | 	} | ||||||
|  | 	if(cbEvExit_ != nullptr) { | ||||||
|  | 		(pBehavior->*cbEvExit_)(ev); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -28,9 +28,14 @@ public: | |||||||
| 	inline void setNextState(const int evid, XFState* state) { setNextState(evid, XFEvent::XFEventType::Event, state); }; | 	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); }; | 	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 registerOnEntry(callback cbEntry) { cbEntry_ = cbEntry; }; | ||||||
| 	inline void registerOnExit(callback cbExit) { cbExit_ = cbExit; }; | 	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: | protected: | ||||||
| 	inline void setNextState(const int evid, XFEvent::XFEventType type, XFState* state) {setNextState(evid, 0, type, state); }; | 	inline void setNextState(const int evid, XFEvent::XFEventType type, XFState* state) {setNextState(evid, 0, type, state); }; | ||||||
| @@ -50,8 +55,12 @@ protected: | |||||||
| 	} transition; | 	} transition; | ||||||
| 	std::list<transition> transitions_; | 	std::list<transition> transitions_; | ||||||
|  |  | ||||||
| 	callback cbEntry_; | 	callback cbState_ = nullptr; | ||||||
| 	callback cbExit_; | 	callback cbEntry_ = nullptr; | ||||||
|  | 	callback cbExit_ = nullptr; | ||||||
|  | 	callbackEv cbEvState_ = nullptr; | ||||||
|  | 	callbackEv cbEvEntry_ = nullptr; | ||||||
|  | 	callbackEv cbEvExit_ = nullptr; | ||||||
|  |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user