-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
SimpleTransitionWithButton.ino
104 lines (75 loc) · 2.27 KB
/
SimpleTransitionWithButton.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/////////////////////////////////////////////////////////////////
/*
This emulates a light switch.
The button click will turn on/off the light.
While the light is "on", a "." will be shown.
*/
/////////////////////////////////////////////////////////////////
#include "SimpleFSM.h"
#include "Button2.h" // https://github.com/LennartHennigs/Button2
/////////////////////////////////////////////////////////////////
#define BUTTON_PIN 39
/////////////////////////////////////////////////////////////////
SimpleFSM fsm;
Button2 btn;
/////////////////////////////////////////////////////////////////
void light_on() {
Serial.println("Entering State: ON");
}
void light_off() {
Serial.println("Entering State: OFF");
}
void exit_light_on() {
Serial.println("Leaving State: ON ");
}
void exit_light_off() {
Serial.println("Leaving State: OFF");
}
void on_to_off() {
Serial.println("ON -> OFF");
}
void off_to_on() {
Serial.println("OFF -> ON");
}
void ongoing() {
Serial.print(".");
}
/////////////////////////////////////////////////////////////////
State s[] = {
State("on", light_on, ongoing, exit_light_on),
State("off", light_off, NULL, exit_light_off)
};
enum triggers {
light_switch_flipped = 1
};
Transition transitions[] = {
Transition(&s[0], &s[1], light_switch_flipped, on_to_off),
Transition(&s[1], &s[0], light_switch_flipped, off_to_on)
};
int num_transitions = sizeof(transitions) / sizeof(Transition);
/////////////////////////////////////////////////////////////////
void button_handler(Button2 &btn) {
if (fsm.getState() == &s[0]) Serial.println();
Serial.println("BUTTON: I was flipped");
fsm.trigger(light_switch_flipped);
}
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial) {
delay(300);
}
Serial.println();
Serial.println();
Serial.println("SimpleFSM - Simple Transitions (Light Switch w/ Button)\n");
fsm.add(transitions, num_transitions);
fsm.setInitialState(&s[1]);
btn.begin(BUTTON_PIN);
btn.setTapHandler(button_handler);
}
/////////////////////////////////////////////////////////////////
void loop() {
fsm.run(1000);
btn.loop();
}
/////////////////////////////////////////////////////////////////