Skip to content

Instantly share code, notes, and snippets.

@ZanzyTHEbar
Last active September 23, 2022 12:59
Show Gist options
  • Save ZanzyTHEbar/d838262959979fb807086ca7fefc2f46 to your computer and use it in GitHub Desktop.
Save ZanzyTHEbar/d838262959979fb807086ca7fefc2f46 to your computer and use it in GitHub Desktop.
#include <StateManager.hpp>
void updateState(){
bool state = digitalRead(19);
StateManager_Buttons.setState(state ? ButtonState_e::Buttons_ON : ButtonState_e::Buttons_OFF);
}
void handleState(){
if (StateManager_Buttons.getCurrentState() == ButtonState_e::Buttons_ON){
Serial.println("Button has been pressed");
}
}
void setup(){}
void loop(){
updateState();
handleState();
}
#include "StateManager.hpp"
StateManager<State_e> StateManager_Device;
StateManager<ButtonState_e> StateManager_Buttons;
StateManager<MotorState_e> StateManager_Motor;
#ifndef STATEMANAGER_HPP
#define STATEMANAGER_HPP
#include <Arduino.h>
/*
* StateManager
* All Project States are managed here
*/
struct ProgramStates
{
struct DeviceStates
{
enum State_e
{
Starting,
Started,
Stopping,
Stopped,
Error
};
enum ButtonState_e
{
Buttons_OFF,
Buttons_ON,
Buttons_PLUS,
Buttons_MINUS,
Buttons_ManAut,
Buttons_Error
};
enum MotorState_e
{
MOTOR_OFF,
MOTOR_ON,
MOTOR_MANUAL,
MOTOR_AUTOMATIC,
MOTOR_SERIAL_REPORT,
MOTOR_Error
};
};
};
/*
* EventManager
* All Project Events are managed here
* TODO: Implement Event Manager
*/
template <class T>
class StateManager
{
public:
StateManager() {}
virtual ~StateManager() {}
void setState(T state)
{
_current_state = state;
}
/*
* Get States
* Returns the current state of the device
*/
T getCurrentState()
{
return _current_state;
}
private:
T _current_state;
};
typedef ProgramStates::DeviceStates::State_e State_e;
typedef ProgramStates::DeviceStates::ButtonState_e ButtonState_e;
typedef ProgramStates::DeviceStates::MotorState_e MotorState_e;
extern StateManager<State_e> StateManager_Device;
extern StateManager<ButtonState_e> StateManager_Buttons;
extern StateManager<MotorState_e> StateManager_Motor;
#endif // STATEMANAGER_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment