forked from digint/tinyfsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_switch.cpp
85 lines (67 loc) · 2.14 KB
/
simple_switch.cpp
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
#include <tinyfsm.hpp>
#include <iostream>
struct Off; // forward declaration
// ----------------------------------------------------------------------------
// 1. Event Declarations
//
struct Toggle : tinyfsm::Event { };
// ----------------------------------------------------------------------------
// 2. State Machine Base Class Declaration
//
struct Switch : tinyfsm::Fsm<Switch>
{
virtual void react(Toggle const &) { };
// alternative: enforce handling of Toggle in all states (pure virtual)
//virtual void react(Toggle const &) = 0;
virtual void entry(void) { }; /* entry actions in some states */
void exit(void) { }; /* no exit actions */
// alternative: enforce entry actions in all states (pure virtual)
//virtual void entry(void) = 0;
};
// ----------------------------------------------------------------------------
// 3. State Declarations
//
struct On : Switch
{
void entry() override { std::cout << "* Switch is ON" << std::endl; };
void react(Toggle const &) override { transit<Off>(); };
};
struct Off : Switch
{
void entry() override { std::cout << "* Switch is OFF" << std::endl; };
void react(Toggle const &) override { transit<On>(); };
};
FSM_INITIAL_STATE(Switch, Off)
// ----------------------------------------------------------------------------
// 4. State Machine List Declaration (dispatches events to multiple FSM's)
//
// In this example, we only have a single state machine, no need to use FsmList<>:
//using fsm_handle = tinyfsm::FsmList< Switch >;
using fsm_handle = Switch;
// ----------------------------------------------------------------------------
// Main
//
int main()
{
// instantiate events
Toggle toggle;
fsm_handle::start();
while(1)
{
char c;
std::cout << std::endl << "t=Toggle, q=Quit ? ";
std::cin >> c;
switch(c) {
case 't':
std::cout << "> Toggling switch..." << std::endl;
fsm_handle::dispatch(toggle);
// alternative: instantiating causes no overhead (empty declaration)
//fsm_handle::dispatch(Toggle());
break;
case 'q':
return 0;
default:
std::cout << "> Invalid input" << std::endl;
};
}
}