Arduino/ESP library to simplify setting up and running a state machine.
This library allows you to quickly setup a State Machine. Read here what a state machine is and why a state machine is neat for hardware projects.
It has been tested with Arduino, ESP8266 and ESP32 devices.
⚠️ To see the latest changes to the library please take a look at the Changelog.
If you find this library helpful please consider giving it a ⭐️ at GitHub and/or buy me a ☕️. Thanks!
Graphviz
source file of your state machine definitionLittleFS
or SD card storage (TBD)⚠️ To see the latest changes to the library please take a look at the Changelog.
run()
function in your loop State(
String name,
CallbackFunction on_enter,
CallbackFunction on_state = NULL,
CallbackFunction on_exit = NULL,
bool is_final = false
);
Most states will have the entry handler
The easiest way to define states is creating using an array, e.g. as shown in MixedTransitions.ino:
State s[] = {
State("red", on_red),
State("green", on_green),
State("BTN", on_button_press)
};
The inital state must of the state machine must be defined – either via the constructor or the setup()
function or via the setInitialState()
function
None, one, or multiple states can be defined as an end state via setAsFinal()
For the full State
class definition see State.h
Transition
and TimedTransition
. Transition(
State* from,
State* to,
int event_id,
CallbackFunction on_run = NULL,
String name = "",
GuardCondition guard = NULL
);
Regular transitions must have a a set of states and a trigger (ID):
Transition transitions[] = {
Transition(&s[0], &s[1], light_switch_flipped),
Transition(&s[1], &s[0], light_switch_flipped)
};
Define the triggers for your state machine in an enum (and set the first enum value to 1, to be safe):
enum triggers {
light_switch_flipped = 1
};
To call a trigger use the trigger()
function:
fsm.trigger(light_switch_flipped);
See SimpleTransitions.ino and SimpleTransitionWithButtons.ino for more details
TimedTransition(
State* from,
State* to,
int interval,
CallbackFunction on_run = NULL,
String name = "",
GuardCondition guard = NULL
);
Timed transitions are automatically executed after a certain amount of time has passed
The interval is defined in milliseconds:
TimedTransition timedTransitions[] = {
TimedTransition(&s[0], &s[1], 6000),
TimedTransition(&s[1], &s[0], 4000),
TimedTransition(&s[2], &s[1], 2000)
};
See TimedTransitions.ino for more details
Guard conditions are evaluated before a transition takes places
Only if the guard condition is true the transition will be executed
Both regular and timed transitions can have guard conditions
You define guard conditions as functions:
TimedTransition timedTransitions[] = {
TimedTransition(&s[0], &s[1], 1000, NULL, "", zero_yet),
TimedTransition(&s[0], &s[0], 1000, NULL, "", not_zero_yet)
};
bool not_zero_yet() {
return countdown != 0;
}
bool zero_yet() {
return countdown == 0;
}
See Guards.ino for a complete example
State(String name, CallbackFunction on_enter, CallbackFunction on_state = NULL, CallbackFunction on_exit = NULL, bool is_final = false);
To define how frequent the on_state
event is called, pass an interval (in ms) to the run()
function in your main loop()
:
void run(int interval = 1000, CallbackFunction tick_cb = NULL);
After this interval has passed the on_state
function of the active state will be called (if it defined)
Also the tick_cb
callback function will be called (if defined) in the run()
call
SimpleFSM provides a few functions to check on the state of the machine:
State* getState() const;
State* getPreviousState() const;
bool isInState(State* state) const;
int lastTransitionedAt() const;
bool isFinished() const;
Use the function getDotDefinition()
to get your state machine definition in the GraphViz dot format
Here the output for the MixedTransitions.ino example:
digraph G {
rankdir=LR; pad=0.5
node [shape=circle fixedsize=true width=1.5];
"red light" -> "green light" [label=" (6000ms)"];
"green light" -> "red light" [label=" (4000ms)"];
"button pressed" -> "green light" [label=" (2000ms)"];
"red light" -> "button pressed" [label=" (ID=1)"];
"red light" [style=filled fontcolor=white fillcolor=black];
}
You can use this visualize your state machine:\
If the machine is running, the current state will be highlighted
Currently guard functions and end states are not shown in the graph
See MixedTransitionsBrowser.ino to learn how to run a webserver to show the Graphviz diagram of your state machine
Open the Arduino IDE choose "Sketch > Include Library" and search for "SimpleFSM". Or download the ZIP archive (https://github.com/lennarthennigs/SimpleFSM/zipball/master), and choose "Sketch > Include Library > Add .ZIP Library..." and select the downloaded file.
MIT License
Copyright (c) 2023 Lennart Hennigs
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.