Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SimpleTransitionsWithMap.ino #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions examples/SimpleTransitionsWithMap/SimpleTransitionsWithMap.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/////////////////////////////////////////////////////////////////
/*
This emulates an blinken light.
Every four seconds will the light turn on or off.
While the light is "on", a "." will be shown.
Text labels are used to simplify access to states.
*/

/////////////////////////////////////////////////////////////////
#include <map>
#include "SimpleFSM.h"

/////////////////////////////////////////////////////////////////

SimpleFSM fsm;

/////////////////////////////////////////////////////////////////

void light_on() {
Serial.println("Entering State: ON");
}

void light_off() {
Serial.println("Entering State: OFF");
}

void exit_light_on() {
Serial.println("\nLeaving State: ON ");
}

void exit_light_off() {
Serial.println("\nLeaving State: OFF");
}

void on_to_off() {
Serial.println("ON -> OFF");
}

void off_to_on() {
Serial.println("OFF -> ON");
}

void ongoing() {
Serial.print(".");
}

/////////////////////////////////////////////////////////////////

std::map<std::string, State> states;

enum triggers {
light_switch_flipped = 1
};

Transition transitions[] = {
Transition(&states["on"], &states["off"], light_switch_flipped, on_to_off),
Transition(&states["off"], &states["on"], light_switch_flipped, off_to_on)
};

int num_transitions = sizeof(transitions) / sizeof(Transition);

/////////////////////////////////////////////////////////////////

void setup() {

Serial.begin(9600);
while (!Serial) {
delay(300);
}
Serial.println();
Serial.println();
Serial.println("SimpleFSM - Simple Transitions (Light Switch)\n");

states["on"] = State("on", light_on, ongoing, exit_light_on);
states["off"] = State("off", light_off, ongoing, exit_light_off);

fsm.add(transitions, num_transitions);
fsm.setInitialState(&states["off"]);
}

/////////////////////////////////////////////////////////////////

void loop() {
fsm.run();
// flip the switch every 4 seconds
// better than using delay() as this won't block the loop()
if (fsm.lastTransitioned() > 4000) {
fsm.trigger(light_switch_flipped);
}
}

/////////////////////////////////////////////////////////////////