-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuzzer.cpp
More file actions
120 lines (104 loc) · 2.99 KB
/
buzzer.cpp
File metadata and controls
120 lines (104 loc) · 2.99 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
/*
* This file is part of LynxGradProject.
*
* LynxGradProject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LynxGradProject is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LynxGradProject. If not, see <http://www.gnu.org/licenses/>.
*/
#include "buzzer.h"
#include <ChibiOS_AVR.h>
bool Buzzer::s_Initialized = false;
Buzzer::BuzzerTone Buzzer::s_BuzzerTone = Buzzer::ToneConstant;
unsigned long Buzzer::s_ToneTime[4] = {0, 0, 0, 0};
byte Buzzer::s_ToneStage = 0;
bool Buzzer::s_State = false;
Buzzer::Buzzer()
{
if (s_Initialized == false) {
s_Initialized = true;
pinMode(c_nBuzzerPin, OUTPUT);
setTone(ToneOff);
}
}
Buzzer::~Buzzer()
{
}
void Buzzer::setTone(Buzzer::BuzzerTone tone)
{
if (tone == ToneOff) {
s_State = false;
} else if (tone == ToneConstant) {
s_State = true;
} else if (tone == Tone2Short) {
s_ToneTime[0] = c_Tone2ShortOnTime1;
s_ToneTime[1] = c_Tone2ShortOffTime1;
s_ToneTime[2] = c_Tone2ShortOnTime2;
s_ToneTime[3] = c_Tone2ShortOffTime2;
s_State = true;
} else if (tone == Tone50DC) {
s_ToneTime[0] = c_Tone50DCOnTime1;
s_ToneTime[1] = c_Tone50DCOffTime1;
s_ToneTime[2] = c_Tone50DCOnTime1;
s_ToneTime[3] = c_Tone50DCOffTime1;
s_State = true;
} else if (tone == ToneSalute) {
s_ToneTime[0] = c_ToneSaluteOnTime1;
s_ToneTime[1] = c_ToneSaluteOffTime1;
s_ToneTime[2] = c_ToneSaluteOnTime2;
s_ToneTime[3] = c_ToneSaluteOffTime2;
s_State = true;
} else {
return;
}
s_BuzzerTone = tone;
s_ToneStage = 0;
digitalWrite(c_nBuzzerPin, !s_State);
}
void Buzzer::salute()
{
setTone(ToneSalute);
}
void Buzzer::turnOn()
{
setTone(ToneConstant);
}
void Buzzer::turnOff()
{
setTone(ToneOff);
}
void Buzzer::run()
{
while (1) {
if (s_BuzzerTone == ToneConstant) {
s_State = true;
digitalWrite(c_nBuzzerPin, !s_State);
chThdSleepMilliseconds(50);
continue;
}
if (s_BuzzerTone == ToneOff) {
s_State = false;
digitalWrite(c_nBuzzerPin, !s_State);
chThdSleepMilliseconds(50);
continue;
}
chThdSleepMilliseconds(s_ToneTime[s_ToneStage]);
s_State = !s_State;
s_ToneStage++;
if (s_ToneStage > 3) {
if (s_BuzzerTone == ToneSalute) {
setTone(ToneOff);
}
s_ToneStage = 0;
}
digitalWrite(c_nBuzzerPin, !s_State);
}
}