Improve CallBacks
This commit is contained in:
		| @@ -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; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|  | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -21,7 +21,7 @@ ButtonEventsHandler::ButtonEventsHandler() | ||||
|  | ||||
| } | ||||
|  | ||||
| void ButtonEventsHandler::startButtonSM(const XFEvent* ev) { | ||||
| void ButtonEventsHandler::startButtonSM() { | ||||
| 	buttonStateSm_[0].startBehavior(); | ||||
| 	buttonStateSm_[1].startBehavior(); | ||||
| 	buttonStateSm_[2].startBehavior(); | ||||
|   | ||||
| @@ -31,7 +31,7 @@ public: | ||||
|  | ||||
| protected: | ||||
| 	ButtonEventsHandler(); | ||||
| 	void startButtonSM(const XFEvent* ev); | ||||
| 	void startButtonSM(); | ||||
|  | ||||
| 	XFState stRun; | ||||
|  | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -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<transition> transitions_; | ||||
|  | ||||
| 	callback cbEntry_; | ||||
| 	callback cbExit_; | ||||
| 	callback cbState_ = nullptr; | ||||
| 	callback cbEntry_ = nullptr; | ||||
| 	callback cbExit_ = nullptr; | ||||
| 	callbackEv cbEvState_ = nullptr; | ||||
| 	callbackEv cbEvEntry_ = nullptr; | ||||
| 	callbackEv cbEvExit_ = nullptr; | ||||
|  | ||||
| }; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user