This is a library for the Particle dev kits.
Ported from the Arduino Finite State Machine library by Gustavo Gonnet.
All credit goes to the original author: Alexander Brevig.
Why FSMs? Please read this write-up
A FSM serves as a manager that organizes a set of states, or behaviors.
It manages the transition between states, and the state itself. The transitions might be caused by the user, a wall or something other that is external, and they might be caused by some internal logic. Transitions either happen immediately, or they are deferred to the next update. The latter is the most common. It's used because that way you'll know that all code related to the current state, will have executed on the same state.
You can read more about finite state machines in this write-up.
This library will help you get rid of all your custom switch-case statements.
It is created to help organizing and standardizing the way an FSM could be implemented.
The library expects all functionality to be coded in the sketch, and the State class will simply use callbacks to simulate functionality.
All States expect an enter function, an update function and an exit function. These functions are being called according to this scheme:
current.exit(); //exit current state
next.enter(); //enter next state
current = next;
while no transition
current.update();
FiniteStateMachine(State& current)
FSM(State& current)
Example:
FSM ethernetStateMachine = FSM(ethernetDebugState);
Initializes an FSM object with the current state equal to ethernetDebugState.
State( enterFunction , updateFunction , exitFunction )
Example:
State ethernetDebugState = State( connectToHost , debug , closeConnectionToHost );
void enter() : This function gets called whenever this state is entered
void update() : This function gets called whenever the state machine updates while in this state
void exit() : This function gets called whenever this state is exited
void update() : This function will trigger update on the current State
void transitionTo(State& next) : This function will schedule a state change, the change itself will occur at the beginning of the next update
void immediateTransitionTo(State& next) : This function will instantly change the current state to next state
State& getCurrentState() : Returns the current state
boolean isInState( State &state ) : Check if state is equal to the current state of the FSM
To avoid mistakes mixing states from different states machines together, use the strongly typed state variant. The compiler will throw an error if you mix states.
Use DECLARE_STATE
to create a named State
class for each of your state machines and pass that class when creating the state machine. All the methods of the named State
class and the typed state machine FSMT
are the same as the generic variant of State
and FSM
.
FiniteStateMachineTyped(StateType& current)
FSMT(StateType& current)
Example:
DECLARE_STATE(ConnectivityState);
ConnectivityState disconnectedState(waitForConnection);
ConnectivityState connectedState(waitForDisconnection);
FSMT<ConnectivityState> connectivityStateMachine(disconnectedState);
// Assuming brewCoffeState is a CoffeeState from a different state machine this would fail with a compiler error
connectivityStateMachine.transitionTo(brewCoffeState);
// error: no matching function for call to 'FiniteStateMachineTyped<ConnectivityState>::transitionTo(CoffeeState&)'
We will implement a state machine for an LED.
From a design point of view we want to make the led go on and off, as well as fade in and out. This translates directly to the states for our example:
On Off FadeIn FadeOut
The states describe themselves:
Every 5 seconds the FSM will advance to the next State in the diagram above.
This FSM translates into this sketch:
#include <FiniteStateMachine.h>
//how many states are we cycling through?
const byte NUMBER_OF_STATES = 4;
//utility functions
void ledOn() { /*action to turn the led on*/ }
void ledOff() { /*action to turn the led off*/ }
void ledFadeIn() { /*action to fade in the led*/ }
void ledFadeOut() { /*action to fade out the led*/ }
//end utility functions
// initialize states
State On = State(ledOn);
State Off = State(ledOff);
State FadeIn = State(ledFadeIn);
State FadeOut = State(ledFadeOut);
// initialize state machine, start in state: On
FSM ledStateMachine = FSM(On);
// counter variable
byte counter = 0;
void setup()
{ /* nothing to setup */ }
void loop()
{
// increment counter and constrain it to [ 0, 1, 2, 3 ]
counter = ++counter % NUMBER_OF_STATES;
switch (counter){
case 0: ledStateMachine.transitionTo(On); break;
case 1: ledStateMachine.transitionTo(Off); break;
case 2: ledStateMachine.transitionTo(FadeIn); break;
case 3: ledStateMachine.transitionTo(FadeOut); break;
}
ledStateMachine.update();
// advance to next state every 5 seconds
delay(5000);
}
You can refer to this pool and sauna controller.