forked from tinkerspy/Automaton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomaton.h
More file actions
executable file
·155 lines (132 loc) · 3.98 KB
/
Automaton.h
File metadata and controls
executable file
·155 lines (132 loc) · 3.98 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
/*
Automaton.h - Library for creating and running Finite State Machines.
Created by TinkerSpy, July, 2015.
*/
#ifndef Automaton_h
#define Automaton_h
#include "Arduino.h"
#define ATM_MAX_STATES 32767
#if ATM_MAX_STATES < 128
typedef int8_t state_t;
#define read_state(addr) (state_t)pgm_read_byte_near(addr)
#else
typedef int16_t state_t;
#define read_state(addr) (state_t)pgm_read_word_near(addr)
#endif
typedef void (*swcb_num_t)( const char label[], int current, int next, int trigger, uint32_t runtime, uint32_t cycles );
typedef void (*swcb_sym_t)( const char label[], const char current[], const char next[], const char trigger[], uint32_t runtime, uint32_t cycles );
typedef uint16_t atm_msg_t;
const int8_t ATM_NO_OF_QUEUES = 5; // queues 0, 1, 2, 3, 4
const int8_t ATM_DEFAULT_PRIO = 1;
const state_t ATM_NOP = -1;
const state_t ATM_SLEEP = -2;
const state_t ATM_ON_SWITCH = -3;
const state_t ATM_ON_ENTER = 0;
const state_t ATM_ON_LOOP = 1;
const state_t ATM_ON_EXIT = 2;
const uint32_t ATM_TIMER_OFF = 0xffffffff; // This timer value never expires
const uint16_t ATM_COUNTER_OFF = 0xffff; // This counter value never expires
class Factory; // pre declare!
class Machine;
class atm_milli_timer {
public: uint32_t value;
};
class atm_micro_timer {
public: uint32_t value;
};
class atm_timer {
public:
uint32_t value;
Machine * pmachine;
void begin( Machine * machine, uint32_t v );
void set( uint32_t v );
virtual int expired( void ) = 0;
};
class atm_timer_millis: public atm_timer {
public:
int expired( void );
};
class atm_timer_micros: public atm_timer {
public:
int expired( void );
};
class atm_counter {
public: uint16_t value;
void set( uint16_t v );
uint8_t expired( void );
uint16_t decrement( void );
};
class atm_counter_auto {
public: uint16_t value;
void set( uint16_t v );
uint8_t expired( void );
uint16_t decrement( void );
};
class Machine
{
public:
virtual int event( int id ) = 0; // Pure virtual methods -> make this an abstract class
virtual void action( int id ) = 0;
Machine &state( state_t state);
state_t state( void );
Machine & toggle( state_t state1, state_t state2 );
Machine & begin( const state_t tbl[], int width );
Machine & msgQueue( atm_msg_t msg[], int width );
uint8_t asleep( void );
Machine & priority( int8_t priority );
int8_t priority( void );
uint32_t runtime_millis( void );
uint32_t runtime_micros( void );
uint8_t pinChange( uint8_t pin );
int msgRead( uint8_t id_msg );
int msgRead( uint8_t id_msg, int cnt );
int msgPeek( uint8_t id_msg );
int msgClear( uint8_t id_msg );
Machine & msgClear( void );
Machine & msgWrite( uint8_t id_msg );
Machine & msgWrite( uint8_t id_msg, int cnt );
Machine & msgMap( uint32_t map );
Machine & cycle( void );
Machine & onSwitch( swcb_num_t callback );
Machine & onSwitch( swcb_sym_t callback, const char sym_s[], const char sym_e[] );
Machine & label( const char label[] );
const char * map_symbol( int id, const char map[] );
public:
int8_t prio;
int8_t sleep;
const char * inst_label;
const char * class_label;
Machine * inventory_next;
Machine * priority_next;
Factory * factory;
uint32_t state_millis, state_micros;
protected:
state_t next;
state_t current = -1;
state_t trigger = -1;
uint32_t pinstate;
const state_t* state_table;
const char* sym_states;
const char* sym_events;
uint8_t state_width;
swcb_sym_t callback_sym;
swcb_num_t callback_num;
uint32_t cycles;
atm_msg_t * msg_table;
int msg_width;
};
class Factory
{
public:
Factory & add( Machine & machine );
Factory & cycle( void );
Machine * find( const char label[] );
protected:
int8_t recalibrate = 1;
Machine * inventory_root;
Machine * priority_root[ATM_NO_OF_QUEUES];
private:
void calibrate( void );
void run( int q );
};
#endif