-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuzzer.c
379 lines (321 loc) · 7.67 KB
/
buzzer.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <stdint.h>
#include <stdio.h>
#include <msp430.h>
#include "bithacks.h"
#include <legacymsp430.h>
#include "uart.h"
#include "oscillator.h"
#include "buzzer.h"
#include "target.h"
#include "vario.h"
#include "timer.h"
#include "charge_pump.h"
#include "button.h"
#include "sensor.h"
#include "filter.h"
static int CurrentLen = 0;
static int CurrentFreq = 0;
static int NextLen= 0;
static int NextFreq;
const char * stringLift = "SMB:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6";
const char * stringSink = "SW:d=4,o=3,b=110:4g,g,g,8d#,16p,16a#,g,8d#,16p,16a#,2g,d4,d4,d4,8d#4,16p,16a#,f#,8d#,16p,16a#,2g,g4,8g,16p,16g,g4,8f#4,16p,16f4,16e4,16d#4,e4,8g#,c#4,8c4,16p,16b,16a#,16a,a#,8d#,f#,8d#,16p,16a#,4g,8d#,16p,16a#,2g";
int notefreq[12] = {4186, 4434, 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902};
extern SensorValues sensor;
void CheckBuzzerStart(void)
{
VarioInitialiseFilter(&sensor);
}
void CheckBuzzer(bool mode)
{
int32_t Vz = 0;
SensorStartTemperatureSampling();
sensor.temperature = SensorCompensateTemperature(sensor.temperature_ut);
sensor.temperature_ut = SensorReadTemperatureWhenAvailable();
SensorStartPressureSampling();
sensor.pressure_ut = SensorReadPressureWhenAvailable();
sensor.pressure = SensorCompensatePressure(sensor.pressure_ut);
Vz = Filter(sensor.pressure);
if((Vz < (-10000))&&mode == false)
{
play(stringSink);
VarioInitialiseFilter(&sensor);
}
else if((Vz > 10000)&&mode == true)
{
play(stringLift);
VarioInitialiseFilter(&sensor);
}
}
int note2freq(int note)
{
return notefreq[note % 12] / (1 << (9 - (note / 12)));
}
void play( const char *song)
{
char *p = (char *)song;
int defdur = 4;
int defscale = 6;
int bpm = 63;
// Skip name
while (*p && *p != ':') p++;
if (!*p) return;
p++;
// Parse defaults
while (*p)
{
char param;
int value;
while (*p == ' ') p++;
if (!*p) return;
if (*p == ':') break;
param = *p++;
if (*p != '=') return;
p++;
value = 0;
while (*p >= '0' && *p <= '9') value = value * 10 + (*p++ - '0');
switch (param)
{
case 'd': defdur = 32 / value; break;
case 'o': defscale = value; break;
case 'b': bpm = value; break;
}
while (*p == ' ') p++;
if (*p == ',') p++;
}
p++;
while (*p)
{
int note = -1;
int scale = defscale;
int dur = defdur;
int ms;
int freq;
// Skip whitespace
while (*p == ' ') p++;
if (!*p) return;
// Parse duration
if (*p >= '0' && *p <= '9')
{
int value = 0;
while (*p >= '0' && *p <= '9') value = value * 10 + (*p++ - '0');
dur = 32 / value;
}
// Parse note
switch (*p)
{
case 0: return;
case 'C': case 'c': note = 0; break;
case 'D': case 'd': note = 2; break;
case 'E': case 'e': note = 4; break;
case 'F': case 'f': note = 5; break;
case 'G': case 'g': note = 7; break;
case 'A': case 'a': note = 9; break;
case 'H': case 'h': note = 11; break;
case 'B': case 'b': note = 11; break;
case 'P': case 'p': note = -1; break;
}
p++;
if (*p == '#')
{
note++;
p++;
}
if (*p == 'b')
{
note--;
p++;
}
// Parse special duration
if (*p == '.')
{
dur += dur / 2;
p++;
}
// Parse scale
if (*p >= '0' && *p <= '9') scale = (*p++ - '0');
// Parse special duration (again...)
if (*p == '.')
{
dur += dur / 2;
p++;
}
// Skip delimiter
while (*p == ' ') p++;
if (*p == ',') p++;
// Play note
ms = dur * 60000 / (bpm * 8);
if (note == -1)
freq = 0;
else
freq = note2freq((scale + 1) * 12 + note);
if (freq)
{
BuzzerSetFrequency(freq);
delay_ms(ms * 7 / 8);
BuzzerSetFrequency(0);
delay_ms(ms / 8);
}
else
delay_ms(ms);
}
BuzzerSetFrequency(0);
}
void BuzzerResetSound()
{
BuzzerStartSound();
BuzzerSetBlocking(0, 100);
BuzzerSetBlocking(600, 120);
BuzzerSetBlocking(0, 30);
BuzzerSetBlocking(800, 60);
}
void BuzzerDemoSoundTacTacOn()
{
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(20,200);
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(30,150);
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(40,100);
}
void BuzzerDemoSoundTacTacOff()
{
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(40,100);
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(30,150);
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(20,200);
}
void BuzzerDemoSound()
{
BuzzerSetFrequency(0);
BuzzerSetBlocking(0,200);
BuzzerSetBlocking(600,200);
BuzzerSetBlocking(0,75);
BuzzerSetBlocking(600,200);
BuzzerSetBlocking(0,75);
BuzzerSetBlocking(600,200);
BuzzerSetQueue(0,1000);
}
void BuzzerStartSound()
{
BuzzerSetFrequency(0);
BuzzerSetBlocking(200,150);
BuzzerSetBlocking(0,50);
BuzzerSetBlocking(400,120);
BuzzerSetBlocking(0,40);
BuzzerSetBlocking(600,100);
BuzzerSetBlocking(0,30);
BuzzerSetBlocking(800,60);
}
void BuzzerStopSound()
{
BuzzerSetFrequency(0);
BuzzerSetBlocking(600,150);
BuzzerSetBlocking(0,30);
BuzzerSetBlocking(400,170);
BuzzerSetBlocking(0,40);
BuzzerSetBlocking(200,190);
BuzzerSetBlocking(0,50);
BuzzerSetBlocking(150,200);
}
#define DEFAULT_VOLUME_MODE 2
static char BuzzerSoundMode = DEFAULT_VOLUME_MODE;
#define BUZZER_BUTTON_TIMEOUT_MS 4000
#define BUZZER_BUTTON_TIMEOUT BUZZER_BUTTON_TIMEOUT_MS/TASK_PERIOD_MS
static int BuzzerButtonTemp = 0;
void BuzzerSetVolume(char button)
{
if (button == BUTTON_SHORT)
{
if (BuzzerButtonTemp == 0)
{
BuzzerButtonTemp = BUZZER_BUTTON_TIMEOUT;
}
else if (BuzzerButtonTemp > 0)
{
BuzzerButtonTemp = BUZZER_BUTTON_TIMEOUT;
BuzzerSoundMode++;
}
}
if ( BuzzerButtonTemp > 0 )
BuzzerButtonTemp--;
if (button != BUTTON_SHORT)
return;
if (BuzzerSoundMode > 3)
BuzzerSoundMode = 1;
#ifdef DEBUG
char printf_buff[30];
char printf_len = 0;
printf_len += snprintf(printf_buff+printf_len, sizeof(printf_buff)-printf_len, "Volume mode %d\n\r", BuzzerSoundMode);
UartXmitString(printf_buff);
#endif //DEBUG
switch(BuzzerSoundMode)
{
case 1:
ChargePumpSetPower(POWER_VOLUME_LOW);
BuzzerDemoSound();
break;
case 2:
ChargePumpSetPower(POWER_VOLUME_MED);
BuzzerDemoSound();
break;
case 3:
ChargePumpSetPower(POWER_VOLUME_HIG);
BuzzerDemoSound();
break;
default:
BuzzerSoundMode = 1;
}
}
#define BUZZER_DEAD_TIME_CYCLE 2
void BuzzerSetFrequency(unsigned int freq)
{
if (freq)
{
int ccr0 = (CPU_FREQUENCY/16)/(freq);
int ccr0_2 = ccr0/2;
TimerTA1Set(ccr0, ccr0_2-BUZZER_DEAD_TIME_CYCLE, ccr0-ccr0_2+BUZZER_DEAD_TIME_CYCLE );
}
else
{
TimerTA1Stop();
}
}
void BuzzerTask()
{
if(CurrentLen>0)
CurrentLen-=TASK_PERIOD_MS;
else
{
if(NextLen)
{
if (CurrentFreq != NextFreq) //avoid constant tone glitch
BuzzerSetFrequency(NextFreq); //update only if requiered
CurrentLen = NextLen;
NextLen = 0;
}
else
{
BuzzerSetFrequency(0);
}
}
}
void BuzzerSetBlocking(unsigned int freq, unsigned int ms)
{
BuzzerSetFrequency(freq);
delay_ms(ms);
BuzzerSetFrequency(0);
}
void BuzzerSetNow(unsigned int freq, unsigned int ms)
{
if (CurrentFreq != freq) //avoid constant tone glitch
BuzzerSetFrequency(freq); //update only if requiered
CurrentLen = ms;
NextLen = 0;
}
void BuzzerSetQueue(unsigned int freq, unsigned int ms)
{
NextFreq = freq;
NextLen = ms;
}