forked from dfrencham/rad-gate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
218 lines (181 loc) · 5.01 KB
/
Copy pathSequence.cpp
File metadata and controls
218 lines (181 loc) · 5.01 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
/*****************************************
* RemoteArDuino (RAD) Gate
* (c) Danny Frencham 2017
*****************************************/
#include "constants.h"
#include "utility.h"
#include "Sequence.h"
int T0 = 0;
int T1 = T0 + DELAY_DROP_TONE_MS;
int T2 = T1 + DELAY_INTERVAL_MS;
int T3 = T2 + DELAY_DROP_TONE_MS;
int T4 = T3 + DELAY_INTERVAL_MS;
int T5 = T4 + DELAY_DROP_TONE_MS;
int T6 = T5 + DELAY_INTERVAL_MS;
int T7 = T6 + GATE_ACTIVE_MS;
int T8 = T6 + DELAY_DROP_TONE_FINAL_MS;
#if SHORT_PULSE_GATE
int target[] = {T0, T1, T2, T3, T4, T5, T6, T7, T8};
uint8_t count = 9;
// column numbers 0=lighttree, 1=audiotone, 2=gatedrop
// column types int, bool, bool
// if -1 ignore
int seq[][3] = {
{ 1, 1,-1},
{-1, 0,-1},
{ 2, 1,-1},
{-1, 0,-1},
{ 3 ,1,-1},
{-1, 0,-1},
{ 4, 1, 1},
{-1,-1, 0},
{-1, 0,-1}
};
#else
int target[] = {T0, T1, T2, T3, T4, T5, T6, T8};
uint8_t count = 8;
int seq[][3] = {
{ 1, 1,-1},
{-1, 0,-1},
{ 2, 1,-1},
{-1, 0,-1},
{ 3 ,1,-1},
{-1, 0,-1},
{ 4, 1, 1},
{-1, 0,-1}
};
}
#endif
// for reaction time voice announce
// map individual digits (SFX_CHAR), to voice file numbers (SFX_MESG)
// SFX_CHAR, SFX_MESG
sfx_map sfx_maps[] = {
{'0', SFX_ZERO},
{'1', SFX_ONE},
{'2', SFX_TWO},
{'3', SFX_THREE},
{'4', SFX_FOUR},
{'5', SFX_FIVE},
{'6', SFX_SIX},
{'7', SFX_SEVEN},
{'8', SFX_EIGHT},
{'9', SFX_NINE},
{'.', SFX_POINT},
};
Sequence::Sequence(Gate* gateOb, AudioFX* audioOb, LightTree* lighttreeOb, Display* displayOb) {
gate = gateOb;
audio = audioOb;
lighttree = lighttreeOb;
display = displayOb;
}
void Sequence::begin_sequence() {
uint8_t step = 0;
uint32_t offset;
uint32_t now = 0;
int8_t val;
volatile uint32_t react_time;
serial_print("Sequence begin");
display->allOff();
gate->set_sequence_running(true);
// digitalWrite(LED_BUILTIN, HIGH); // turn on builtin LED. Note: also turns on Neo Pixel LED 0 green!
// lighttree->set_status(0x000000); // If using builtin LED can turn lightstrip led 0 off
#if LIGHT_TREE_RELAY_ENABLE
lighttree->led_reset();
#endif
#if LIGHT_TREE_STRIP
lighttree->set_status(0xFFFF00); // or other color eg Yellow
#endif
digitalWrite(PIN_LED_ACTIVE, HIGH); // turn on "Active" LED
audio->play_sound_sample(SFX_PREP, 3300); // 1500 for sample + 1800 delay
audio->play_sound_sample(SFX_WATCH_GATE, 2000); // 2000 for sample and no delay
gate->random_wait();
// once the sequence starts, do not allow abort
serial_print("No longer abortable");
gate->set_abortable(false);
offset = millis();
while((step < count) && (!gate->is_aborted())) {
// a tight loop that waits the target amount of time
while (target[step] > now) {
now = millis() - offset;
}
for (int i=0; i<3; i++){
val = seq[step][i];
// -1 means ignore
if (val >= 0){
// 0 = lightree
if (i == 0){
lighttree->light_set(val);
}
// 1 = audio
if (i == 1){
if (val == 0) {
audio->stop_tone();
}
else if (val == 1) {
audio->start_tone(TONE_DROP_HZ);
}
}
// 2 = gate drop
if (i == 2){
if (val == 1) {
GATE_DROP_START = millis();
gate->drop(); // turn on solenoid
serial_print("Waiting for reaction time...");
}
else if (val == 0) {
gate->arm(); // turn off solenoid
}
}
}
}
step++;
}
serial_print("Setting sequence to STOP");
gate->set_sequence_running(false);
if (!gate->is_aborted()) {
audio->stop_tone();
}
else {
abort_seq();
gate->set_abortable(true);
}
set_ready();
digitalWrite(PIN_LED_ACTIVE, LOW);
// digitalWrite(LED_BUILTIN, LOW);
digitalWrite(PIN_RELAY, !GATE_ACTIVE_LEVEL); // turn on magnet
serial_print("Sequence complete");
}
void Sequence::abort_seq() {
serial_print("Sequence abort");
lighttree->abort();
audio->play_abort();
//lighttree->led_reset(); // hold off reseting LEDs. Reset in set_ready()
delay(3000);
set_ready();
}
void Sequence::set_ready() {
lighttree->ready();
gate->ready();
}
void Sequence::end_seq(volatile uint32_t react_time) {
// convert reaction time from milliseconds to array of chars
// eg 1234 to '1' '.' '2' '3' '4' then play 'matching' files 'SFX_ONE' etc
serial_print_val("Reaction Time =", react_time);
double b = react_time/1000.0;
String s = String(b, 3);
int n = s.length();
// Write react time to seven segment display
display-> displayNumber(react_time, 1, n-4); // n-4 gives which digit dp is attached to
char char_array[n+1];
strcpy(char_array, s.c_str());
for (int i=0; i<n+1; i++) {
for (int j=0; j<11; j++) {
if (sfx_maps[j].SFX_CHAR == char_array[i]) {
audio->play_sound_sample(sfx_maps[j].SFX_MESG);
break;
}
}
}
audio->play_sound_sample(SFX_SECONDS);
serial_print("Press GO to restart...");
}