TACOS/Tacos/include/sta/tacos/statemachine.hpp
2023-10-15 22:37:04 +02:00

109 lines
2.1 KiB
C++

/*
* statemachine.hpp
*
* Created on: Sep 14, 2023
* Author: Dario
*/
#ifndef INCLUDE_TACOS_STATEMACHINE_HPP_
#define INCLUDE_TACOS_STATEMACHINE_HPP_
#include <sta/config.hpp>
#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 <sta/tacos/thread.hpp>
#include <sta/rtos/timer.hpp>
#include <sta/rtos/event.hpp>
#include <sta/debug/assert.hpp>
#include <functional>
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<uint16_t(uint16_t)> 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<uint16_t(uint16_t)> transitionFunc_;
};
} // namespace tacos
} // namespace sta
#endif // STA_TACOS_NUM_STATES
#endif /* INCLUDE_TACOS_STATEMACHINE_HPP_ */