-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuzzerManager.cpp
More file actions
65 lines (56 loc) · 1.45 KB
/
Copy pathBuzzerManager.cpp
File metadata and controls
65 lines (56 loc) · 1.45 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
#include "BuzzerManager.h"
#include "Config.h"
// Ascending, pleasant major arpeggio
const BuzzerManager::Note BuzzerManager::kGrantedSeq_[] = {
{523, 375}, // C5
{659, 375}, // E5
{784, 375}, // G5
{1047, 375}, // C6
};
const uint8_t BuzzerManager::kGrantedLen_ = sizeof(kGrantedSeq_) / sizeof(kGrantedSeq_[0]);
// Descending, clearly different "error" tone
const BuzzerManager::Note BuzzerManager::kDeniedSeq_[] = {
{440, 375}, // A4
{349, 375}, // F4
{294, 375}, // D4
{220, 375}, // A3
};
const uint8_t BuzzerManager::kDeniedLen_ = sizeof(kDeniedSeq_) / sizeof(kDeniedSeq_[0]);
void BuzzerManager::begin() {
pinMode(BUZZER_PIN, OUTPUT);
noTone(BUZZER_PIN);
}
void BuzzerManager::startNote_(uint8_t idx) {
index_ = idx;
noteStartMs_ = millis();
const Note& n = seq_[idx];
if (n.freqHz > 0) {
tone(BUZZER_PIN, n.freqHz);
} else {
noTone(BUZZER_PIN);
}
}
void BuzzerManager::startSequence_(const Note* seq, uint8_t len) {
seq_ = seq;
seqLen_ = len;
playing_ = true;
startNote_(0);
}
void BuzzerManager::playGranted() {
startSequence_(kGrantedSeq_, kGrantedLen_);
}
void BuzzerManager::playDenied() {
startSequence_(kDeniedSeq_, kDeniedLen_);
}
void BuzzerManager::update() {
if (!playing_) return;
if (millis() - noteStartMs_ >= seq_[index_].durationMs) {
uint8_t next = index_ + 1;
if (next >= seqLen_) {
noTone(BUZZER_PIN);
playing_ = false;
return;
}
startNote_(next);
}
}