-
Notifications
You must be signed in to change notification settings - Fork 6
/
buzzer.h
100 lines (79 loc) · 2.28 KB
/
buzzer.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
91
92
93
94
95
96
97
98
99
100
#ifndef _BUZZER_H
#define _BUZZER_H
#define BUZZER_TIME_TOLERANCE 3
#define BUZZ_Prompt 0
#define BUZZ_TempReached 1
#define BUZZ_TimeOut 2
#define BUZZ_AddHop 3
#define BUZZ_End 4
#define BUZZ_Warn 5
const byte _sound_Prompt[] PROGMEM = { 2, 2, 5 };
const byte _sound_TempReached[] PROGMEM = { 5, 10, 3, 10, 3, 10};
const byte _sound_TimeOut[] PROGMEM = {10, 1, 39, 1, 39, 1, 39, 1, 39, 20, 20 }; // 5 seconds
const byte _sound_AddHop[] PROGMEM = {18, 2, 2, 2, 2, 2, 9, 6, 2, 6, 2, 6, 9, 2, 2 , 2 , 2 , 2 , 2};
const byte _sound_End[] PROGMEM = { 2, 40, 5 };
const byte _sound_Warn[] PROGMEM = { 5, 4, 3, 4, 3, 4 };
const byte * const _sounds [] PROGMEM = {
_sound_Prompt,
_sound_TempReached,
_sound_TimeOut,
_sound_AddHop,
_sound_End,
_sound_Warn
};
byte _numberofNtesToPlay;
boolean _playing = false;
boolean _buzzing;
unsigned long _buzzingTime;
word _currentPeriod;
byte* _ptrCurrentNote;
byte* _currentSound;
void BuzzerMute(void);
void BuzzerInit() {
pinMode (BuzzControlPin, OUTPUT);
digitalWrite (BuzzControlPin, LOW);
}
void BuzzerThread() {
if (! _playing)
return;
if ((gCurrentTimeInMS - _buzzingTime) >= (_currentPeriod + BUZZER_TIME_TOLERANCE)) {
_numberofNtesToPlay --;
if (_numberofNtesToPlay == 0) {
// finished, stop
digitalWrite (BuzzControlPin, LOW);
_playing = false;
return;
}
_buzzingTime = millis();
_ptrCurrentNote ++;
_currentPeriod = (word)pgm_read_byte(_ptrCurrentNote) * 25;
#if Silent != true
if (_buzzing) {
digitalWrite(BuzzControlPin, LOW);
} else {
digitalWrite(BuzzControlPin, HIGH);
}
#endif
_buzzing = ! _buzzing;
}
}
void BuzzerPlay(byte id) {
_currentSound = (byte *)pgm_read_word(&(_sounds[id]));
_numberofNtesToPlay = pgm_read_byte(_currentSound);
_ptrCurrentNote = _currentSound;
_ptrCurrentNote ++;
_currentPeriod = (word)pgm_read_byte(_ptrCurrentNote) * 25;
_playing = true;
_buzzingTime = millis();
#if Silent != true
digitalWrite (BuzzControlPin, HIGH);
#endif
_buzzing = true;
#if DebugBuzzer == true
Serial.print(F("BufferPlay("));
Serial.print(id);
Serial.print(F(") notes: "));
Serial.println(_numberofNtesToPlay);
#endif
}
#endif