https://github.com/gusgonnet/particle-fsm
Port of the Arduino Finite State Machine library here: http://playground.arduino.cc/Code/FiniteStateMachine
https://github.com/gusgonnet/particle-fsm
Last synced: 3 months ago
JSON representation
Port of the Arduino Finite State Machine library here: http://playground.arduino.cc/Code/FiniteStateMachine
- Host: GitHub
- URL: https://github.com/gusgonnet/particle-fsm
- Owner: gusgonnet
- License: gpl-2.0
- Created: 2016-01-07T19:26:55.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-11-06T19:38:36.000Z (over 5 years ago)
- Last Synced: 2025-03-26T22:45:36.155Z (4 months ago)
- Language: C++
- Homepage:
- Size: 61.5 KB
- Stars: 27
- Watchers: 7
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-particle - Particle Finite State Machine (FSM)
README
# particle-fsm
This is a library for the [Particle dev kits](https://www.particle.io/).
Ported from the [Arduino Finite State Machine library](http://playground.arduino.cc/Code/FiniteStateMachine) by [Gustavo Gonnet]([email protected]).
All credit goes to the original author: [Alexander Brevig]([email protected]).
Why FSMs? [Please read this write-up](https://www.hackster.io/gusgonnet/using-finite-state-machines-fdba04)
# Finite State Machine Description
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](https://www.hackster.io/gusgonnet/using-finite-state-machines-fdba04).
# FSM Library Description
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();
```
---# Creation
## Finite State Machine Creation
_FiniteStateMachine(State& current)_
_FSM(State& current)_
Example:
```FSM ethernetStateMachine = FSM(ethernetDebugState);```
Initializes an FSM object with the current state equal to ethernetDebugState.
## State Creation
_State( enterFunction , updateFunction , exitFunction )_
Example:
```State ethernetDebugState = State( connectToHost , debug , closeConnectionToHost );```
---
# Functions
## State Functions_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## Finite State Machine Functions
_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---
## Typed States
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 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::transitionTo(CoffeeState&)'
```# Example
## LED Finite State Machine
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
FadeOutThe 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//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);}
```
---
# Real-world implementationsYou can refer to this [pool and sauna controller](https://www.hackster.io/gusgonnet/pool-and-sauna-controller-b24a9a?team=34278).