-
Notifications
You must be signed in to change notification settings - Fork 2
/
beeper.cpp
74 lines (60 loc) · 2.08 KB
/
beeper.cpp
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
#include <sound/beeper.h>
#include <driver/i2s.h>
#include <algorithm>
#include <sound/waveout.h>
static char LOG_TAG[] = "BOOP";
Beeper::Beeper():
duration_samples{0},
samples {0},
channel_status { 0xFF },
active_channel { -1 } {
voice = new SquareGenerator();
}
Beeper::~Beeper() {
delete voice;
}
void Beeper::set_channel_state(beeper_channel_t ch, bool active) {
if(active) {
channel_status |= (1 << ch);
} else {
if(active_channel == ch) {
stop_tone(ch);
}
channel_status &= ~(1 << ch);
}
}
bool Beeper::is_channel_enabled(beeper_channel_t ch) {
return (channel_status & (1 << ch)) > 0;
}
size_t Beeper::fill_buffer(void* buffer, size_t length_) {
int want_samples = duration_samples == 0? 0: (duration_samples - samples);
size_t length = duration_samples == 0? length_ : std::min(length_, (size_t) (want_samples / 8));
size_t generated = voice->generate_samples(buffer, length, want_samples);
samples += generated*8;
if(duration_samples > 0 && duration_samples <= samples) {
voice->set_parameter(ToneGenerator::Parameter::PARAMETER_ACTIVE, false);
active_channel = -1;
}
return generated;
}
void Beeper::start_tone(beeper_channel_t ch, uint freq, uint16_t duty) {
if(active_channel > ch) return;
if(!is_channel_enabled(ch)) return;
if(active_channel < ch) active_channel = ch;
duration_samples = 0;
samples = 0;
voice->set_parameter(ToneGenerator::Parameter::PARAMETER_FREQUENCY, freq);
}
void Beeper::stop_tone(beeper_channel_t ch) {
if(active_channel != ch) return;
active_channel = -1;
voice->set_parameter(ToneGenerator::Parameter::PARAMETER_ACTIVE, false);
}
void Beeper::beep(beeper_channel_t ch, uint freq, uint len, uint16_t duty) {
if(active_channel > ch) return;
if(!is_channel_enabled(ch)) return;
if(active_channel < ch) active_channel = ch;
samples = 0;
duration_samples = len * (WaveOut::BAUD_RATE / 1000);
voice->set_parameter(ToneGenerator::Parameter::PARAMETER_FREQUENCY, freq);
}