forked from tinkerspy/Automaton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomaton.cpp
More file actions
executable file
·330 lines (289 loc) · 8.2 KB
/
Automaton.cpp
File metadata and controls
executable file
·330 lines (289 loc) · 8.2 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Automaton.cpp - Library for creating Finite State Machines.
Created by TinkerSpy, july, 2015.
*/
#include "Automaton.h"
void atm_timer::set( uint32_t v ) {
value = v;
}
void atm_timer::begin( Machine * machine, uint32_t v ) {
pmachine = machine;
value = v;
}
int atm_timer_millis::expired( void ) {
return value == ATM_TIMER_OFF ? 0 : millis() - pmachine->state_millis >= value;
}
int atm_timer_micros::expired( void ) {
return value == ATM_TIMER_OFF ? 0 : micros() - pmachine->state_micros >= value;
}
void atm_counter::set( uint16_t v ) {
value = v;
}
uint16_t atm_counter::decrement( void )
{
return value > 0 && value != ATM_COUNTER_OFF ? --value : 0;
}
uint8_t atm_counter::expired()
{
return value == ATM_COUNTER_OFF ? 0 : ( value > 0 ? 0 : 1 );
}
Machine & Machine::state(state_t state)
{
next = state;
trigger = -1;
sleep = 0;
return *this;
}
state_t Machine::state()
{
return current;
}
Machine & Machine::toggle( state_t state1, state_t state2 )
{
state( current == state1 ? state2 : state1 );
return *this;
}
Machine & Machine::onSwitch( swcb_num_t callback )
{
callback_num = callback;
return *this;
}
Machine & Machine::onSwitch( swcb_sym_t callback, const char sym_s[], const char sym_e[] )
{
callback_sym = callback;
sym_states = sym_s;
sym_events = sym_e;
return *this;
}
Machine & Machine::label( const char label[] )
{
inst_label = label;
return *this;
}
int8_t Machine::priority()
{
return prio;
}
Machine & Machine::priority( int8_t priority )
{
prio = priority;
return *this;
}
// .asleep() Returns true if the machine is in a sleeping state
uint8_t Machine::asleep()
{
return sleep;
}
// .runtime_millis() Returns the number of millis since the machine entered the current state
uint32_t Machine::runtime_millis()
{
return millis() - state_millis;
}
// .runtime_millis() Returns the number of micros since the machine entered the current state
uint32_t Machine::runtime_micros()
{
return micros() - state_micros;
}
Machine & Machine::begin( const state_t* tbl, int width )
{
state_table = tbl;
state_width = ATM_ON_EXIT + width + 2;
prio = ATM_DEFAULT_PRIO;
if ( !inst_label ) inst_label = class_label;
return *this;
}
Machine & Machine::msgQueue( atm_msg_t msg[], int width )
{
msg_table = msg;
msg_width = width;
return *this;
}
unsigned char Machine::pinChange( uint8_t pin ) {
unsigned char v = digitalRead( pin ) ? 1 : 0;
if ( (( pinstate >> pin ) & 1 ) != ( v == 1 ) ) {
pinstate ^= ( (uint32_t)1 << pin );
return 1;
}
return 0;
}
int Machine::msgRead( uint8_t id_msg ) // Checks msg queue and removes 1 msg
{
if ( msg_table[id_msg] > 0 ) {
msg_table[id_msg]--;
return 1;
}
return 0;
}
int Machine::msgRead( uint8_t id_msg, int cnt )
{
if ( msg_table[id_msg] > 0 ) {
if ( cnt >= msg_table[id_msg] ) {
msg_table[id_msg] = 0;
} else {
msg_table[id_msg] -= cnt;
}
return 1;
}
return 0;
}
int Machine::msgPeek( uint8_t id_msg )
{
if ( msg_table[id_msg] > 0 ) {
return 1;
}
return 0;
}
int Machine::msgClear( uint8_t id_msg ) // Destructive read (clears queue for this type)
{
sleep = 0;
if ( msg_table[id_msg] > 0 ) {
msg_table[id_msg] = 0;
return 1;
}
return 0;
}
Machine & Machine::msgClear()
{
sleep = 0;
for ( int i = 0; i < msg_width; i++ ) {
msg_table[i] = 0;
}
return *this;
}
Machine & Machine::msgWrite( uint8_t id_msg )
{
sleep = 0;
msg_table[id_msg]++;
return *this;
}
Machine & Machine::msgWrite( uint8_t id_msg, int cnt )
{
sleep = 0;
msg_table[id_msg] += cnt;
return *this;
}
Machine & Machine::msgMap( uint32_t map ) // TESTME!
{
sleep = 0;
for ( int i = 0; i < msg_width; i++ ) {
if ( map & ( 1 << i ) ) {
msg_table[i]++;
}
}
return *this;
}
const char * Machine::map_symbol( int id, const char map[] )
{
int cnt = 0;
int i = 0;
if ( id == -1 ) return "*NONE*";
if ( id == 0 ) return map;
while ( 1 ) {
if ( map[i] == '\0' && ++cnt == id ) {
i++;
break;
}
i++;
}
return &map[i];
}
// .cycle() Executes one cycle of a state machine
Machine & Machine::cycle()
{
if ( !sleep ) {
cycles++;
if ( next != -1 ) {
action( ATM_ON_SWITCH );
if ( callback_sym || callback_num ) {
if ( callback_sym ) {
callback_sym( inst_label,
map_symbol( current, sym_states ),
map_symbol( next, sym_states ),
map_symbol( trigger, sym_events ), this->runtime_millis(), cycles );
} else {
callback_num( inst_label, current, next, trigger, this->runtime_millis(), cycles );
}
}
current = next;
next = -1;
state_millis = millis();
state_micros = micros();
action( read_state( state_table + ( current * state_width ) + ATM_ON_ENTER ) );
sleep = read_state( state_table + ( current * state_width ) + ATM_ON_LOOP ) == ATM_SLEEP;
cycles = 0;
}
state_t i = read_state( state_table + ( current * state_width ) + ATM_ON_LOOP );
if ( i != -1 ) { action( i ); }
for ( i = ATM_ON_EXIT + 1; i < state_width; i++ ) {
if ( ( read_state( state_table + ( current * state_width ) + i ) != -1 ) && ( i == state_width - 1 || event( i - ATM_ON_EXIT - 1 ) ) ) {
action( read_state( state_table + ( current * state_width ) + ATM_ON_EXIT ) );
state( read_state( state_table + ( current * state_width ) + i ) );
trigger = i - ATM_ON_EXIT - 1;
return *this;
}
}
}
return *this;
}
// FACTORY
// .calibrate() Distributes the machines in the inventory to the appropriate priority queues
void Factory::calibrate( void )
{
// Reset all priority queues to empty lists
for ( int8_t i = 0; i < ATM_NO_OF_QUEUES; i++ )
priority_root[i] = 0;
// Walk the inventory list that contains all state machines in this factory
Machine * m = inventory_root;
while ( m ) {
// Prepend every machine to the appropriate priority queue
if ( m->prio < ATM_NO_OF_QUEUES ) {
m->priority_next = priority_root[m->prio];
priority_root[m->prio] = m;
}
m = m->inventory_next;
}
recalibrate = 0;
}
// .run( q ) Traverses an individual priority queue and cycles the machines in it once (except for queue 0)
void Factory::run( int q )
{
Machine * m = priority_root[ q ];
while ( m ) {
if ( q > 0 && !m->sleep ) m->cycle();
// Request a recalibrate if the prio field doesn't match the current queue
if ( m->prio != q ) recalibrate = 1;
// Move to the next machine
m = m->priority_next;
}
}
// .add( machine ) Adds a state machine to the factory by prepending it to the inventory list
Factory & Factory::add( Machine & machine )
{
machine.inventory_next = inventory_root;
inventory_root = &machine;
machine.factory = this;
recalibrate = 1;
return *this;
}
// .find() Search the factory inventory for a machine by instance label
Machine * Factory::find( const char label[] )
{
Machine * m = inventory_root;
while ( m ) {
if ( *label == *m->inst_label ) {
return m;
}
m = m->inventory_next;
}
return 0;
}
// .cycle() executes one factory cycle (runs all priority queues a certain number of times)
Factory & Factory::cycle( void )
{
if ( recalibrate ) calibrate();
run( 1 ); run( 2 ); run( 1 ); run( 2 );
run( 1 ); run( 3 ); run( 1 ); run( 4 );
run( 1 ); run( 2 ); run( 1 ); run( 3 );
run( 1 ); run( 2 ); run( 1 ); run( 0 );
return *this;
}