-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarningTone.ino
282 lines (219 loc) · 9.2 KB
/
warningTone.ino
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
#include "warningTone.h"
// need to switch the playTonesStarted back to false at some point
WarningTone::WarningTone(bool useMaintaining):
useMaintaining(useMaintaining)
{
// Initialize output pin.
pinMode(tonePin, OUTPUT);
// Initialize variable that will hold warning tone parameters.
struct WarningTone::ToneParameters toneParameters[1];
}
void WarningTone::PlayWarningTone(WarningTone::ToneParameters new_toneParameters)
{
// Find a starting time for saying when multiple-beep warnings should stop and start.
this->toneParameters = new_toneParameters;
// If tones have never been called,
if (this->playTonesStarted == false) {
// Switch to playTonesStarted = true so you don't re-calculate tone start time or tone parameters again
this->playTonesStarted = true;
// Switch toneCase to first tone, start playing tones
this->toneCase = 1;
}
this->toneCurrentTime=millis();
switch (this->toneCase) {
case 1: {
// set "tone case" to run next tone
toneCase = 2;
// Get the start time
this->toneStartTime = millis();
// If frequncy isn't 0,
if (this->toneParameters.frequency1 != 0){
// Play tone 1
startTone(tonePin, this->toneParameters.frequency1, this->toneDuration);
}
// if frequency IS 0, turn off tone
else {
// Turn off tones
stopTone();
}
break;
}
case 2: {
// If the tone duration for the last tone is up,
if (this->toneCurrentTime > this->toneStartTime + this->toneDuration + this->toneBuffer){
// Switch logic to next tone so you never try to play tone 2 again
this->toneCase = 3;
// Use toneCurrentTime as new toneStartTime
this->toneStartTime = this->toneCurrentTime;
// If frequncy isn't 0,
if (this->toneParameters.frequency2 != 0){
// Play tone 2
startTone(tonePin, this->toneParameters.frequency2, this->toneDuration);
}
// if frequency IS 0, turn off tone
else {
// Turn off tones
stopTone();
}
}
break;
}
case 3: {
// If the tone duration for the last tone is up,
if (this->toneCurrentTime > this->toneStartTime + this->toneDuration + this->toneBuffer){
// Switch to the tone-off case so you don't try to play tone 3 again.
this->toneCase = 4;
// Use toneCurrentTime as new toneStartTime
this->toneStartTime = this->toneCurrentTime;
// If frequncy isn't 0,
if (this->toneParameters.frequency3 != 0){
// Play tone 3
startTone(tonePin, this->toneParameters.frequency3,this->toneDuration);
}
// if frequency IS 0, turn off tone
else {
// Turn off tones
stopTone();
}
}
break;
}
case 4: {
// If the tone duration for the last tone is up,
if (this->toneCurrentTime > this->toneStartTime + this->toneDuration + this->toneBuffer){
// Switch to an idle tone case so you don't try to play tones again.
this->toneCase = 0;
// Turn off tones
stopTone();
}
break;
}
}
}
// Write out code for calculating the tone parameters
struct WarningTone::ToneParameters WarningTone::CalculateToneParameters(int currentStage)
{
WarningTone::ToneParameters result;
String message;
// Declare probe subtypes. Give probe_subtype2 a default value of Blank
ProbeSubtype1 probe_subtype1 = ProbeSubtype1::Warning;
ProbeSubtype2 probe_subtype2 = ProbeSubtype2::Blank;
// If there are probe trials, and this is a probe trial,
if (useProbeTrials && stageParameters[currentStage].probe == Probe::NoWarning)
{
// The probe subtype2 should be blank.
probe_subtype2 = ProbeSubtype2::Blank;
// Call correct probes
Probe_Messages probe_messages = getProbeMessages(stageParameters[currentStage].probe, probe_subtype1, probe_subtype2);
// Report
Report(stageParameters[currentStage].speed, stageParameters[currentStage].accel, probe_messages.activity_tag, probe_messages.probe_string);
// Output the probe toneParameters
return probe_messages.toneParameters;
}
// If not that particular probe, calculate tone parameters.
else
{
// First check value of stageParameters[currentStage].speed_difference, then check if current or next stage speed is 0
// If stageParameters[currentStage].speed_difference is positve
if (stageParameters[currentStage].speed_difference > 0) {
// If current speed is 0, then give "starting" cue
if (stageParameters[currentStage].speed == 0) {
// Reassign activity reporting tag.
activityTag = 8;
// Report
message = "Warning cue: starting ";
probe_subtype2 = ProbeSubtype2::Starting;
// High pitch, long
result = {
.frequency1 = 10000,
.frequency2 = 10000,
.frequency3 = 10000
};
}
// If curernt speed is not 0, then give "accelerating" cue.
else {
//Announce warning label
// Reassign activity reporting tag.
activityTag = 10;
// Report
message = "Warning cue: accelerating ";
probe_subtype2 = ProbeSubtype2::Accelerating;
// low, blank, high (ascending pitches)
result = {
.frequency1 = 4000,
.frequency2 = 0,
.frequency3 = 10000
};
}
}
// Else if stageParameters[currentStage].speed_difference is negative
else if (stageParameters[currentStage].speed_difference < 0 ){
// If next speed is 0, then give "stopping" cue
if (stageParameters[currentStage + 1].speed == 0) {
// Reassign activity reporting tag.
activityTag = 9;
// Report
message = "Warning cue: stopping";
probe_subtype2 = ProbeSubtype2::Stopping;
// Low pitch, 1 long
result = {
.frequency1 = 4000,
.frequency2 = 4000,
.frequency3 = 4000
};
}
// If next speed is not 0, then give "decelerating" cue.
else {
// Reassign activity reporting tag.
activityTag = 11;
// Report
message = "Warning cue: decelerating ";
probe_subtype2 = ProbeSubtype2::Decelerating;
// high, blank, low (descending pitches)
result = {
.frequency1 = 10000,
.frequency2 = 0,
.frequency3 = 4000
};
}
}
// Else (if stageParameters[currentStage].speed_difference is 0)
else {
// Motor will continue at current speed
// Reassign activity reporting tag.
activityTag = 12;
if (useMaintaining){
// Report
message = "Warning cue: maintaining";
probe_subtype2 = ProbeSubtype2::Maintaining;
// Mid, blank, mid
result = {
.frequency1 = 7000,
.frequency2 = 0,
.frequency3 = 7000
};
}
else {
// Report
message = "Warning cue: maintaining, no tone given ";
probe_subtype2 = ProbeSubtype2::Maintaining;
result = {
.frequency1 = 0,
.frequency2 = 0,
.frequency3 = 0
};
}
}
// If this was the "no change" probe, overwrite the reporting messages
if (useProbeTrials && stageParameters[currentStage].probe == Probe::NoChange){
// Call correct probes
Probe_Messages probe_messages = getProbeMessages(stageParameters[currentStage].probe, probe_subtype1, probe_subtype2);
// Reassign
activityTag = probe_messages.activity_tag;
// Reassign
message = probe_messages.probe_string;
}
Report(stageParameters[currentStage].speed, stageParameters[currentStage].accel, activityTag, message);
return result;
}
}