-
Notifications
You must be signed in to change notification settings - Fork 176
/
multiple_switch.cpp
145 lines (123 loc) · 3.62 KB
/
multiple_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// In this example, we want to use the DefectiveSwitch FSM multiple
// times. As TinyFSM is all about templates, we need to declare it as
// a template class.
//
// This is a bit cumbersome, as the C++ syntax is really ugly when it
// comes to derived template classes.
//
#include <tinyfsm.hpp>
#include <iostream>
#include <stdlib.h> /* rand */
template<int inum>
class Off; // forward declaration
static void DumpState(int inum, const char * state, int on_counter, int defect_level) {
std::cout << "* Switch[" << inum << "] is " << state << " (on_counter=" << on_counter << ", defect_level=" << defect_level << ")" << std::endl;
}
// ----------------------------------------------------------------------------
// 1. Event Declarations
//
struct Toggle : tinyfsm::Event { };
// ----------------------------------------------------------------------------
// 2. State Machine Base Class Declaration
//
template<int inum>
class DefectiveSwitch
: public tinyfsm::Fsm< DefectiveSwitch<inum> >
{
public:
static constexpr unsigned int defect_level = (inum * 2);
static void reset(void) {
on_counter = 0;
}
/* default reaction for unhandled events */
void react(tinyfsm::Event const &) { };
virtual void react(Toggle const &) { };
virtual void entry(void) { }; /* entry actions in some states */
void exit(void) { }; /* no exit actions */
protected:
static unsigned int on_counter;
};
// state variable definitions
template<int inum>
unsigned int DefectiveSwitch<inum>::on_counter{0};
// ----------------------------------------------------------------------------
// 3. State Declarations
//
template<int inum>
class On
: public DefectiveSwitch<inum>
{
// note: base class is not known in dependend template
using base = DefectiveSwitch<inum>;
void entry() override {
base::on_counter++;
DumpState(inum, "ON ", base::on_counter, base::defect_level);
};
void react(Toggle const &) override {
base::template transit< Off<inum> >();
};
};
template<int inum>
class Off
: public DefectiveSwitch<inum>
{
using base = DefectiveSwitch<inum>;
void entry() override {
DumpState(inum, "OFF", base::on_counter, base::defect_level);
};
void react(Toggle const &) override {
if((rand() % (base::defect_level + 1)) == 0)
base::template transit< On<inum> >();
else {
std::cout << "* Kzzz kzzzzzz" << std::endl;
base::template transit< Off<inum> >();
}
};
};
FSM_INITIAL_STATE(DefectiveSwitch<0>, Off<0> )
FSM_INITIAL_STATE(DefectiveSwitch<1>, Off<1> )
FSM_INITIAL_STATE(DefectiveSwitch<2>, Off<2> )
// ----------------------------------------------------------------------------
// 4. State Machine List Declaration
//
using fsm_handle = tinyfsm::FsmList<
DefectiveSwitch<0>,
DefectiveSwitch<1>,
DefectiveSwitch<2>
>;
template<int inum>
void ToggleSingle() {
std::cout << "> Toggling switch " << inum << "..." << std::endl;
DefectiveSwitch<inum>::dispatch(Toggle());
}
// ----------------------------------------------------------------------------
// Main
//
int main()
{
fsm_handle::start();
while(1)
{
char c;
std::cout << std::endl << "0,1,2=Toggle single, a=Toggle all, r=Restart, q=Quit ? ";
std::cin >> c;
switch(c) {
case '0': ToggleSingle<0>(); break;
case '1': ToggleSingle<1>(); break;
case '2': ToggleSingle<2>(); break;
case 'a':
std::cout << "> Toggling all switches..." << std::endl;
fsm_handle::dispatch(Toggle());
break;
case 'r':
fsm_handle::reset();
fsm_handle::start();
break;
case 'q':
return 0;
default:
std::cout << "> Invalid input" << std::endl;
};
}
}