-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathGuards.ino
84 lines (61 loc) · 1.76 KB
/
Guards.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
/////////////////////////////////////////////////////////////////
/*
This example showcases how guards work.
It emulates a time bomb count down.
As long as the countdown is not zero the FSM stays in the counting state.
When it reaches zero it goes to the exploding state.
*/
/////////////////////////////////////////////////////////////////
#include "SimpleFSM.h"
/////////////////////////////////////////////////////////////////
int countdown = 6;
SimpleFSM fsm;
/////////////////////////////////////////////////////////////////
void counting() {
Serial.println(countdown--);
}
void boom() {
Serial.println("BOOM");
}
bool not_zero_yet() {
return !zero_yet();
}
bool zero_yet() {
return countdown == 0;
}
void finished() {
Serial.println("THE END!");
}
void tick() {
Serial.println("TICK");
}
/////////////////////////////////////////////////////////////////
State s[] = {
State("counting", counting),
State("exploding", boom)
};
TimedTransition timedTransitions[] = {
TimedTransition(&s[0], &s[0], 1000, NULL, "", not_zero_yet)
TimedTransition(&s[0], &s[1], 1000, NULL, "", zero_yet),
};
int num_timed = sizeof(timedTransitions) / sizeof(TimedTransition);
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial) {
delay(300);
}
Serial.println();
Serial.println("SimpleFSM - Using Guard Condition (Time Bomb)\n");
fsm.add(timedTransitions, num_timed);
// initial state
fsm.setInitialState(&s[0]);
// final state
s[1].setAsFinal(true);
fsm.setFinishedHandler(finished);
}
/////////////////////////////////////////////////////////////////
void loop() {
fsm.run(1000, tick);
}
/////////////////////////////////////////////////////////////////