-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.h
90 lines (75 loc) · 2.05 KB
/
states.h
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
#ifndef STATE_H_
#define STATE_H_
#define NUM_STATE_THREADS 8
#define THREAD_SIZE 100
#define THREAD_0 1 << 0
#define THREAD_1 1 << 1
#define THREAD_2 1 << 2
#define THREAD_3 1 << 3
#define THREAD_4 1 << 4
#define THREAD_5 1 << 5
#define THREAD_6 1 << 6
#define THREAD_7 1 << 7
#define THREAD_8 1 << 8
#define THREAD_9 1 << 9
#define THREAD_10 1 << 10
#define THREAD_11 1 << 11
#define THREAD_12 1 << 12
#define THREAD_13 1 << 13
#define THREAD_14 1 << 14
#define THREAD_15 1 << 15
int line_tracking (void *n)
{
(void) n;
return 0;
}
int default_state (bool b, void *v)
{
(void) v, b;
long long timer = millis();
//Serial.println("RUNTIME LOGIC ERROR - IN STATE THAT DOES NOT EXIST");
while (100 > millis() - timer) {}
exit(0);
return 0;
}
// defines the structure to hold a single state
struct state
{
// ways to call
state () : action(NULL), data(NULL) {};
state (int (*f)(bool,void*)) : action(f), data(NULL) {};
state (int (*f)(bool,void*), void*v) : action(f), data(v) {};
bool operator == (const state &s) const
{return data == s.data && action == s.action;}
// stored data
int (*action)(bool,void*);
void *data;
};
struct state_initializer {
int threads;
int id;
state s;
};
// takes in the state initializer array and
// places the relavent states into the thread;
template <int N>
void initialize_thread
(state_initializer (&si)[N], int routine,
state thread[THREAD_SIZE])
{
int mask = 1 << routine;
// setup state thread to be default
for (int i = 0; i < THREAD_SIZE; ++i) thread[i] = {default_state};
for (int i = 0; i < N; ++i) {
if (!(si[i].threads & mask)) {
// leave if this thread not apply
} else if (si[i].id > THREAD_SIZE) {
Serial.println("FATAL INIT ERROR - STATE ID OUT OF BOUNDS");
} else if (si[i].id <= 0) {
Serial.println("FATAL INIT ERROR - STATE ID 0 OR LOWER IS INVALID");
} else {
thread[si[i].id] = si[i].s;
}
}
}
#endif