forked from Rhoban/Metabot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuzzer.cpp
More file actions
229 lines (199 loc) · 4.36 KB
/
buzzer.cpp
File metadata and controls
229 lines (199 loc) · 4.36 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
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
#include "buzzer.h"
#include "voltage.h"
#ifdef HAS_TERMINAL
#include <terminal.h>
#endif
// Config
#ifndef __EMSCRIPTEN__
HardwareTimer timer(2);
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/bind.h>
unsigned int millis()
{
return EM_ASM_INT({
return (new Date()).getTime()%0xffffffff;
}, 0);
}
#endif
// Partitions
struct buzzer_note {
unsigned int freq;
unsigned int duration;
};
static struct buzzer_note melody_boot[] = {
{523, 200/2},
{659, 350/2},
{523, 200/2},
{698, 300/2},
{659, 160/2},
{0, 0}
};
static struct buzzer_note melody_alert[] = {
{2000, 200},
{200, 200},
{2000, 200},
{200, 200},
{0, 0}
};
static struct buzzer_note melody_alert_fast[] = {
{2000, 100},
{200, 100},
{2000, 100},
{200, 100},
{0, 0}
};
static struct buzzer_note melody_warning[] = {
{800, 200},
{400, 200},
{200, 0},
{200, 400},
{0, 0}
};
static struct buzzer_note melody_begin[] = {
{800, 200},
{0, 200},
{800, 200},
{0, 0},
};
static struct buzzer_note melody_custom[] = {
{0, 0},
{0, 0}
};
// Status
static struct buzzer_note *melody;
static struct buzzer_note *melody_repeat;
static int melody_st;
#ifdef __EMSCRIPTEN__
static int playing_note = 0;
int buzzer_get_frequency()
{
return playing_note;
}
EMSCRIPTEN_BINDINGS(buzzer) {
emscripten::function("buzzer_get_frequency", &buzzer_get_frequency);
}
#endif
void buzzer_init()
{
#ifndef __EMSCRIPTEN__
melody = NULL;
pinMode(BUZZER_PIN, PWM);
pwmWrite(BUZZER_PIN, 0);
#endif
}
void buzzer_play_note(int note)
{
#ifdef __EMSCRIPTEN__
playing_note = note;
#else
timer.pause();
timer.setPrescaleFactor(72000000 / (note * 100));
timer.setOverflow(100);
if (note == 0) {
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
} else {
timer.refresh();
timer.resume();
pinMode(BUZZER_PIN, PWM);
pwmWrite(BUZZER_PIN, 50);
}
#endif
}
static void buzzer_enter(struct buzzer_note *note)
{
buzzer_play_note(note->freq);
melody = note;
melody_st = millis();
if (note->freq == 0 && note->duration == 0) {
if (melody_repeat != NULL) {
buzzer_enter(melody_repeat);
} else {
melody = NULL;
}
}
}
void buzzer_play(unsigned int melody_num, bool repeat)
{
// Avoid playing another melody when there is a battery alert
#ifndef __EMSCRIPTEN__
if (voltage_error() && melody_num != MELODY_ALERT && melody_num != MELODY_ALERT_FAST) {
return;
}
#endif
struct buzzer_note *to_play = NULL;
if (melody_num == MELODY_BOOT) {
to_play = &melody_boot[0];
} else if (melody_num == MELODY_ALERT) {
to_play = &melody_alert[0];
} else if (melody_num == MELODY_ALERT_FAST) {
to_play = &melody_alert_fast[0];
} else if (melody_num == MELODY_WARNING) {
to_play = &melody_warning[0];
} else if (melody_num == MELODY_BEGIN) {
to_play = &melody_begin[0];
} else if (melody_num == MELODY_CUSTOM) {
to_play = &melody_custom[0];
} else {
melody = NULL;
}
if (to_play) {
melody_repeat = repeat ? to_play : NULL;
buzzer_enter(to_play);
}
}
void buzzer_tick()
{
if (melody != NULL) {
if (millis()-melody_st > melody->duration) {
buzzer_enter(melody+1);
}
}
}
void buzzer_stop()
{
if (voltage_error()) {
return;
}
buzzer_play_note(0);
melody = NULL;
melody_repeat = NULL;
}
bool buzzer_is_playing()
{
return melody != NULL;
}
void buzzer_wait_play()
{
while (buzzer_is_playing()) {
buzzer_tick();
}
}
void buzzer_beep(unsigned int freq, unsigned int duration)
{
melody_custom[0].freq = freq;
melody_custom[0].duration = duration;
buzzer_play(MELODY_CUSTOM);
}
#ifdef HAS_TERMINAL
TERMINAL_COMMAND(play, "Play a melody")
{
int melnum = atoi(argv[0]);
terminal_io()->print("Playing melody ");
terminal_io()->print(melnum);
terminal_io()->println();
buzzer_play(melnum);
}
TERMINAL_COMMAND(beep, "Plays a beep")
{
if (argc == 2) {
buzzer_beep(atoi(argv[0]), atoi(argv[1]));
} else if (argc == 1) {
buzzer_beep(atoi(argv[0]), 1000);
} else {
terminal_io()->println("Usage: beep freq [duration]");
}
}
#endif