2025-12-13 20:23:28 +01:00
|
|
|
#pragma once
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
// Singleton-instance
|
|
|
|
|
class StateMachine
|
|
|
|
|
{
|
2025-12-15 20:13:05 +01:00
|
|
|
public:
|
|
|
|
|
enum class STATE {
|
2025-12-13 20:23:28 +01:00
|
|
|
INITIALIZING,
|
|
|
|
|
IDLING,
|
|
|
|
|
MOVING,
|
|
|
|
|
ERROR
|
|
|
|
|
};
|
2025-12-15 20:13:05 +01:00
|
|
|
|
2025-12-14 12:43:31 +01:00
|
|
|
StateMachine(const StateMachine&) = delete;
|
|
|
|
|
StateMachine(StateMachine&&) = delete;
|
|
|
|
|
static StateMachine& getInstance() noexcept;
|
|
|
|
|
|
2025-12-15 20:13:05 +01:00
|
|
|
STATE getState() const noexcept { return state; };
|
2025-12-13 20:23:28 +01:00
|
|
|
String getLastError() const noexcept { return lastError; };
|
|
|
|
|
// May fail if a state transition from state a to b is not valid
|
2025-12-15 20:13:05 +01:00
|
|
|
bool setState(const STATE newState) noexcept;
|
2025-12-13 20:23:28 +01:00
|
|
|
bool raiseError(const String& reason) noexcept;
|
2025-12-15 20:13:05 +01:00
|
|
|
bool canSwitchToState(const STATE newState) const noexcept;
|
2025-12-13 20:23:28 +01:00
|
|
|
|
|
|
|
|
private:
|
2025-12-14 18:57:28 +01:00
|
|
|
StateMachine() noexcept;
|
2025-12-13 20:23:28 +01:00
|
|
|
|
2025-12-15 20:13:05 +01:00
|
|
|
STATE state = STATE::INITIALIZING;
|
2025-12-13 20:23:28 +01:00
|
|
|
String lastError = "";
|
|
|
|
|
};
|