/* * statemachine.hpp * * Created on: Sep 14, 2023 * Author: Dario */ #ifndef INCLUDE_TACOS_STATEMACHINE_HPP_ #define INCLUDE_TACOS_STATEMACHINE_HPP_ #include #ifndef STA_TACOS_NUM_STATES # error "Number of states wasn't defined in config.hpp" #else #ifndef STA_TACOS_INITIAL_STATE # define STA_TACOS_INITIAL_STATE 0 #endif #define TACOS_STATE_TRANSITION_FLAG 0x00000001U #define TACOS_STATE_CHANGE_FORCED_FLAG 0x00000002U #define TACOS_STATE_CHANGE_ALL_FLAG 0x00000003U #include #include #include #include #include namespace sta { namespace tacos { class Statemachine : public TacosThread { public: static RtosEvent* stateChangeEvent; static Statemachine* instance() { static CGuard g; if (!_instance) { // Create a the manager singleton instance. Statemachine::_instance = new Statemachine(); Statemachine::stateChangeEvent = new RtosEvent(); } return _instance; } void init() override; void func() override; /** * @brief Returns the statemachine's current state. */ uint16_t getCurrentState() const; /** * @brief Registers a new state transition function. */ void setStateTransitionFunction(std::function function); void forceStateTransition(uint16_t state); private: static Statemachine * _instance; class CGuard { public: ~CGuard() { if( NULL != Statemachine::_instance ) { delete Statemachine::_instance; delete Statemachine::stateChangeEvent; Statemachine::_instance = NULL; Statemachine::stateChangeEvent = NULL; } } }; Statemachine(); Statemachine(const Statemachine&); ~Statemachine() {} private: uint16_t currentState_; RtosTimer lockoutTimer_; RtosTimer failsafeTimer_; std::function transitionFunc_; }; } // namespace tacos } // namespace sta #endif // STA_TACOS_NUM_STATES #endif /* INCLUDE_TACOS_STATEMACHINE_HPP_ */