Compilable but untested statemachine implementation including a lot of clean up.

This commit is contained in:
dario
2023-10-23 23:50:08 +02:00
parent 67f3e9aa15
commit 4efc0382f8
6 changed files with 107 additions and 35 deletions

View File

@@ -20,14 +20,18 @@
# 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
// State transition happened because of
#define STA_TACOS_STATE_CHANGE_NORMAL_FLAG ( 0x1U )
#define STA_TACOS_STATE_CHANGE_FORCED_FLAG ( 0x1U << 1)
#define STA_TACOS_STATE_CHANGE_TIMEOUT ( 0x1U << 2)
#define STA_TACOS_STATE_CHANGE_STARTUP_FLAG ( 0x1U << 3)
#define STA_TACOS_STATE_CHANGE_ALL_FLAG ( 0x15U )
#include <sta/tacos/thread.hpp>
#include <sta/rtos/timer.hpp>
#include <sta/rtos/event.hpp>
#include <sta/event.hpp>
#include <sta/debug/assert.hpp>
#include <functional>
@@ -37,40 +41,76 @@ namespace sta
{
namespace tacos
{
typedef std::function<uint16_t(uint16_t)> state_fn;
typedef std::function<void(uint16_t, uint16_t, uint16_t)> timer_fn;
class Statemachine : public TacosThread
{
public:
static RtosEvent* stateChangeEvent;
enum EventFlags
{
NORMAL = 0x1U,
FORCED = 0x1U << 1,
TIMEOUT = 0x1U << 2,
STARTUP = 0x1U << 3,
ALL = NORMAL | FORCED | TIMEOUT | STARTUP
};
/**
* @brief The global event signaling a state change.
*/
static RtosEvent stateChangeEvent;
/**
* @brief Getter function for the singleton instance.
*/
static Statemachine* instance()
{
static CGuard g;
if (!_instance)
{
// Create a the manager singleton instance.
// Create 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.
* @brief Registers a new state transition function. This is a function for the user to implement state transition rules.
*/
void setStateTransitionFunction(std::function<uint16_t(uint16_t)> function);
void setStateFunction(state_fn func);
/**
* @brief Registers a new timer function. This is a function for the user to set the timers after a state change.
*/
void setTimerFunction(timer_fn func);
/**
* @brief Starts the failsafe timer for the desired duration and the desired next state.
*/
void setFailsafeTimer(uint32_t millis, uint16_t nextState);
/**
* @brief Starts the lockoutTimer for the desired duration.
*/
void setLockoutTimer(uint32_t millis);
/**
* @brief this function call allows forced state transitions from external and internal sources.
*/
void forceStateTransition(uint16_t state);
void init() override;
void func() override;
private:
static Statemachine * _instance;
@@ -82,9 +122,7 @@ namespace sta
if( NULL != Statemachine::_instance )
{
delete Statemachine::_instance;
delete Statemachine::stateChangeEvent;
Statemachine::_instance = NULL;
Statemachine::stateChangeEvent = NULL;
}
}
};
@@ -94,11 +132,15 @@ namespace sta
Statemachine(const Statemachine&);
~Statemachine() {}
private:
uint16_t currentState_;
RtosTimer lockoutTimer_;
RtosTimer failsafeTimer_;
std::function<uint16_t(uint16_t)> transitionFunc_;
state_fn transitionFunc_;
timer_fn timerFunc_;
};
} // namespace tacos
} // namespace sta