-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSequence.cpp
More file actions
109 lines (95 loc) · 2.68 KB
/
Copy pathSequence.cpp
File metadata and controls
109 lines (95 loc) · 2.68 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
/*****************************************
* RemoteArDuino (RAD) Gate
* (c) Danny Frencham 2017
*****************************************/
#include "constants.h"
#include "utility.h"
#include "Sequence.h"
Sequence::Sequence(Gate* gateOb, AudioFX* audioOb, LightTree* lighttreeOb)
{
gate = gateOb;
audio = audioOb;
lighttree = lighttreeOb;
}
void Sequence::begin_sequence() {
// gate timings
// Tone Play Time, wait length, light tree step
gate_step gate_steps[] = {
{ DELAY_DROP_TONE_MS, 0, 1 },
{ 0, 60, 0 },
{ DELAY_DROP_TONE_MS, 0, 2 },
{ 0, 60, 0 },
{ DELAY_DROP_TONE_MS, 0, 3 },
{ 0, 60, 0 },
{ DELAY_DROP_TONE_FINAL_MS, 0, 4 },
{ 0, 0, 0 }
};
serial_print("Sequence begin");
gate->set_sequence_running(true);
int step = 0;
int playing_tone = 0;
int waiting = 0;
unsigned long timer_start = 0;
int gate_step_count = 7; // can't programatically count dynamic array
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
digitalWrite(PIN_LED_ACTIVE, HIGH); // turn on LED
lighttree->led_reset();
audio->play_sound_samples();
gate->random_wait();
serial_print("No longer abortable");
gate->set_abortable(false); // once the sequence starts, do not allow abort
while((step < gate_step_count) && (!gate->is_aborted())) {
unsigned long now = millis();
unsigned long timer = now - timer_start;
if (playing_tone && (timer >= gate_steps[step].tone_length)) {
// reset timer
playing_tone = 0;
serial_print_val("Stop tone",step);
serial_print_val("Tone length",gate_steps[step].tone_length);
audio->stop_tone();
step++;
}
if (!playing_tone && (gate_steps[step].tone_length > 0)) {
timer_start = now;
audio->start_tone(TONE_DROP_HZ);
serial_print_val("LED step",step);
lighttree->light_set(gate_steps[step].light_num, gate);
playing_tone = 1;
}
if (waiting && (timer >= gate_steps[step].wait_length)) {
waiting = 0;
step++;
}
if (!waiting && (gate_steps[step].wait_length > 0)) {
timer_start = now;
waiting = 1;
}
gate->set_sequence_running(false);
}
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, HIGH); // turn on magnet
serial_print("Sequence complete");
}
void Sequence::abort_seq() {
serial_print("Sequence abort");
lighttree->abort();
audio->play_abort();
lighttree->led_reset();
delay(3000);
set_ready();
}
void Sequence::set_ready() {
lighttree->ready();
gate->ready();
}