/*/////////////////////////////////////////////////////////////////
This is a soft on\off power switch statesoft_switch_flippedCOUNTING_LED_PIN machine test. The button
click will turn on/off the device. While the soft-switch is "on",
a "." will be shown.
*//////////////////////////////////////////////////////////////////
#include "SimpleFSM.h" // https://github.com/LennartHennigs/SimpleFSM/
#include "Button2.h" // https://github.com/LennartHennigs/Button2
/////////////////////////////////////////////////////////////////
#define BUTTON_PIN 13
#define POWER_LED_PIN 12
#define COUNTING_LED_PIN 14
#define SERIAL_BAUD 115200
/////////////////////////////////////////////////////////////////
SimpleFSM fsm;
Button2 btn;
/////////////////////////////////////////////////////////////////
void soft_on() {
Serial.println("soft_on() - Entering State: ON");
digitalWrite(POWER_LED_PIN, 1);
}
void soft_off() {
Serial.println("soft_off() - Entering State: OFF");
digitalWrite(POWER_LED_PIN, 0);
}
void exit_soft_on() {
Serial.println("exit_soft_on() - Leaving State: ON ");
}
void exit_soft_off() {
Serial.println("exit_soft_off() - Leaving State: OFF");
}
void on_to_off() {
Serial.println("on_to_off()");
}
void off_to_on() {
Serial.println("off_to_on()");
}
void idle_state() {
Serial.print(".");
}
void on_to_counting() {
Serial.println("on_to_counting()");
digitalWrite(COUNTING_LED_PIN, 1);
}
void counting_state() {
Serial.print("|");
}
void counting_to_on() {
Serial.println("counting_to_on()");
digitalWrite(COUNTING_LED_PIN, 0);
}
void counting_to_off() {
Serial.println("counting_to_off()");
digitalWrite(COUNTING_LED_PIN, 0);
}
/////////////////////////////////////////////////////////////////
// State(String name, CallbackFunction on_enter, CallbackFunction on_state = NULL, CallbackFunction on_exit = NULL, bool is_final = false);
State s[] = {
State("ON to OFF", soft_on, idle_state, exit_soft_on),
State("OFF to ON", soft_off, NULL, exit_soft_off),
State("ON to COUNTING", on_to_counting, counting_state),
State("COUNTING to OFF", soft_off, NULL)
};
enum triggers {
soft_switch_short = 1,
soft_switch_long = 2,
soft_switch_double = 3
};
// Transition(State* from, State* to, int event_id, CallbackFunction on_run = NULL, String name = "", GuardCondition guard = NULL);
Transition transitions[] = {
Transition(&s[0], &s[1], soft_switch_long, on_to_off, "Transition ON to OFF"),
Transition(&s[1], &s[0], soft_switch_long, off_to_on, "Transition OFF to ON"),
Transition(&s[0], &s[2], soft_switch_double, NULL, "Transition ON to COUNTING"),
Transition(&s[2], &s[3], soft_switch_long, counting_to_off, "Transition COUNTING to OFF")
};
int num_transitions = sizeof(transitions) / sizeof(Transition);
/////////////////////////////////////////////////////////////////
void button_handler_short(Button2 &btn) {
Serial.println("button_handler_short()");
fsm.trigger(soft_switch_short);
}
void button_handler_long(Button2 &btn) {
Serial.println("button_handler_long()");
fsm.trigger(soft_switch_long);
}
void button_handler_double(Button2 &btn) {
Serial.println("button_handler_double()");
fsm.trigger(soft_switch_double);
}
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(SERIAL_BAUD);
while (!Serial) {
delay(300);
}
Serial.println("SimpleFSM - Soft Power State Machine Test\n");
fsm.add(transitions, num_transitions);
fsm.setInitialState(&s[1]); // start off
pinMode(POWER_LED_PIN, OUTPUT);
pinMode(COUNTING_LED_PIN, OUTPUT);
btn.begin(BUTTON_PIN);
btn.setClickHandler(button_handler_short);
btn.setLongClickDetectedHandler(button_handler_long);
btn.setDoubleClickHandler(button_handler_double);
Serial.println(fsm.getDotDefinition());
}
/////////////////////////////////////////////////////////////////
void loop() {
fsm.run(100);
btn.loop();
}
/////////////////////////////////////////////////////////////////