Comments for modified part of XF

This commit is contained in:
Rémi Heredero 2023-11-28 07:54:55 +01:00
parent 821df428e9
commit a141b01380
6 changed files with 59 additions and 30 deletions

View File

@ -15,7 +15,6 @@ Factory::Factory() {
void Factory::initialize() { void Factory::initialize() {
Trace::initialize(); Trace::initialize();
// TODO: Initialize factory attributes here
#if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0) #if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0)
@ -27,24 +26,21 @@ void Factory::initialize() {
void Factory::build() { void Factory::build() {
Trace::out("Factory: Creating app components..."); Trace::out("Factory: Creating app components...");
// Start state machine(s) // Start SM of ButtonEventLogger & ButtonsController
buttonEventLogger_.startBehavior(); buttonEventLogger_.startBehavior();
ButtonsController::getInstance()->startBehavior(); ButtonsController::getInstance()->startBehavior();
// Register CallBack of ButtonController with himself for the provider and onButtonChangeState for the CallBack Method
ButtonsController::getInstance()->registerCallback ( ButtonsController::getInstance()->registerCallback (
ButtonEventsHandler::getInstance(), ButtonEventsHandler::getInstance(),
(interface::ButtonsControllerCallbackProvider::CallbackMethod) &ButtonEventsHandler::onButtonChangeState (interface::ButtonsControllerCallbackProvider::CallbackMethod) &ButtonEventsHandler::onButtonChangeState
); );
buttonEventLogger_.startBehavior(); // Subscribe ButtonEventLogger to ButtonEventsHandler & start SM of ButtonEventsHandler
ButtonEventsHandler::getInstance()->subscribe(&buttonEventLogger_); ButtonEventsHandler::getInstance()->subscribe(&buttonEventLogger_);
ButtonEventsHandler::getInstance()->startBehavior(); ButtonEventsHandler::getInstance()->startBehavior();
// TODO: Start state-machines here
#if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0) #if defined(TOUCHGFX_ENABLED) && (TOUCHGFX_ENABLED != 0)
getTouchGfxTask().start(); getTouchGfxTask().start();
#endif #endif

View File

@ -11,8 +11,6 @@
#include "platform/f7-disco-gcc/board/ButtonsController.h" #include "platform/f7-disco-gcc/board/ButtonsController.h"
#include "mdw/button/ButtonEventsHandler.h" #include "mdw/button/ButtonEventsHandler.h"
// TODO: Add C++ specific includes here
namespace app { namespace app {
/** /**
@ -30,7 +28,6 @@ public:
#endif #endif
protected: protected:
// TODO: Add static attributes here
static ButtonEventsLogger buttonEventLogger_; static ButtonEventsLogger buttonEventLogger_;
}; };

View File

@ -13,40 +13,52 @@
XFState::XFState(XFBehavior* behavior) XFState::XFState(XFBehavior* behavior)
: pBehavior(behavior) { : pBehavior(behavior) {
/*
* Initialise behavior of this state on the constructor.
* Can't be null !
*/
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::addTransition(XFEvent::XFEventType type, const int evid, const int time, XFState* state) {
assert(state != nullptr); assert(state != nullptr);
/*
* Create a transition with his Type, ID, time if Timeout and the state to be set when this transition happen
* Push the transition on the list of transition of the event
* Pointer State can't be null
*/
transition t; transition t;
t.evType = type;
t.evid = evid; t.evid = evid;
t.time = time; t.time = time;
t.evType = type;
t.nextState = state; t.nextState = state;
transitions_.push_back(t); transitions_.push_back(t);
} }
XFEventStatus XFState::changeState(XFState* state) { XFEventStatus XFState::changeState(XFState* state) {
pBehavior->curentXFState_ = state; // Change current state of the behavior
pBehavior->currentXFState_ = state;
// Set state is changed
pBehavior->changeXFState = true; pBehavior->changeXFState = true;
// Return the event status as consumed
return XFEventStatus::Consumed; return XFEventStatus::Consumed;
} }
XFEventStatus XFState::onState(const XFEvent* ev) { XFEventStatus XFState::onState(const XFEvent* ev) {
// Execute onState callBack without parameter if it's defined
if(cbState_ != nullptr) { if(cbState_ != nullptr) {
(pBehavior->*cbState_)(); (pBehavior->*cbState_)();
} }
// Execute onState callBack with event as parameter if it's defined
if(cbEvState_ != nullptr) { if(cbEvState_ != nullptr) {
(pBehavior->*cbEvState_)(ev); (pBehavior->*cbEvState_)(ev);
} }
// Test each transition and change state if one is actual transition
for(transition t : transitions_) { for(transition t : transitions_) {
switch(ev->getEventType()) { switch(ev->getEventType()) {
@ -71,21 +83,25 @@ XFEventStatus XFState::onState(const XFEvent* ev) {
} }
} }
// Return event status as notConsumed if actual event isn't on the list of transitions
return XFEventStatus::NotConsumed; return XFEventStatus::NotConsumed;
} }
void XFState::onEntry(const XFEvent* ev) { void XFState::onEntry(const XFEvent* ev) {
// Execute onEntry callBack without parameter if it's defined
if(cbEntry_ != nullptr) { if(cbEntry_ != nullptr) {
(pBehavior->*cbEntry_)(); (pBehavior->*cbEntry_)();
} }
// Execute onExit callBack with event as parameter if it's defined
if(cbEvEntry_ != nullptr) { if(cbEvEntry_ != nullptr) {
(pBehavior->*cbEvEntry_)(ev); (pBehavior->*cbEvEntry_)(ev);
} }
// If a timeout is on the list of transition start it with is own delay
for(transition t : transitions_) { for(transition t : transitions_) {
if(t.evType == XFEvent::XFEventType::Timeout) { if(t.evType == XFEvent::XFEventType::Timeout) {
@ -95,14 +111,18 @@ void XFState::onEntry(const XFEvent* ev) {
} }
} }
void XFState::onExit(const XFEvent* ev) { void XFState::onExit(const XFEvent* ev) {
// Execute onExit callBack without parameter if it's defined
if(cbExit_ != nullptr) { if(cbExit_ != nullptr) {
(pBehavior->*cbExit_)(); (pBehavior->*cbExit_)();
} }
// Execute onExit callBack with event as parameter if it's defined
if(cbEvExit_ != nullptr) { if(cbEvExit_ != nullptr) {
(pBehavior->*cbEvExit_)(ev); (pBehavior->*cbEvExit_)(ev);
} }
// If a timeout is on the list of transition and isn't the actual event, stop it
for(transition t: transitions_) { for(transition t: transitions_) {
if(t.evType == XFEvent::XFEventType::Timeout) { if(t.evType == XFEvent::XFEventType::Timeout) {

View File

@ -84,16 +84,21 @@ XFBehavior::TerminateBehavior XFBehavior::process(const XFEvent * pEvent)
} }
XFEventStatus XFBehavior::processEvent(){ XFEventStatus XFBehavior::processEvent(){
/*
* Basic double switch pattern but not switch on an enum but on Object XFState
*/
XFEventStatus eventStatus = XFEventStatus::Unknown; XFEventStatus eventStatus = XFEventStatus::Unknown;
const XFEvent * ev = getCurrentEvent(); const XFEvent * ev = getCurrentEvent();
oldXFState_ = curentXFState_; oldXFState_ = currentXFState_;
eventStatus = curentXFState_->onState(ev); eventStatus = currentXFState_->onState(ev);
if(changeXFState){ if(changeXFState){
oldXFState_->onExit(ev); oldXFState_->onExit(ev);
curentXFState_->onEntry(ev); currentXFState_->onEntry(ev);
} }
return eventStatus; return eventStatus;

View File

@ -97,12 +97,12 @@ protected:
} transition; } transition;
std::list<transition> transitions_; std::list<transition> transitions_;
callback cbState_; callback cbState_ = nullptr;
callback cbEntry_; callback cbEntry_ = nullptr;
callback cbExit_; callback cbExit_ = nullptr;
callbackEv cbEvState_; callbackEv cbEvState_ = nullptr;
callbackEv cbEvEntry_; callbackEv cbEvEntry_ = nullptr;
callbackEv cbEvExit_; callbackEv cbEvExit_ = nullptr;
}; };

View File

@ -55,7 +55,7 @@ public:
protected: protected:
/** /**
* Executes the current event in its implemented behavior. * Executes the current event in its implemented behavior.
* This method needs to be overridden to implement the * This method can be overridden to implement the
* behavior (i.e. state machine) needed. * behavior (i.e. state machine) needed.
*/ */
virtual XFEventStatus processEvent(); virtual XFEventStatus processEvent();
@ -100,9 +100,20 @@ protected:
const XFEvent * pCurrentEvent_; ///< Reference to actually processed event. const XFEvent * pCurrentEvent_; ///< Reference to actually processed event.
protected: protected:
/**
* Behavior have at least an Initial State.
*/
XFInitialState stInitial; XFInitialState stInitial;
XFState* curentXFState_ = &stInitial;
/**
* Pointer for the curent and old state of this behavior
*/
XFState* currentXFState_ = &stInitial;
XFState* oldXFState_ = &stInitial; XFState* oldXFState_ = &stInitial;
/**
* Keep in trace when state is changed (have to be set to true in every processEvent
*/
bool changeXFState = false; bool changeXFState = false;
}; };