-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorder.hpp
60 lines (53 loc) · 1.6 KB
/
recorder.hpp
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
#pragma once
#include "logger.hpp"
#include "button.hpp"
#include "melody-storage.hpp"
#include "keyboard.hpp"
#include "buzzer.hpp"
class Recorder {
public:
Recorder(uint8_t pin,
MelodyStorage& melodyStorage)
: m_ControlButton{ pin, INPUT_PULLUP },
m_MelodyStorage{ melodyStorage } {
log("Recorder created");
}
// Handle recording state and control melody writing process
void OnUpdate(const Keyboard& keyboard, const Buzzer& buzzer) {
const auto frequency = Buzzer::MatchNoteWithFrequency(keyboard.GetNote(),
keyboard.GetOctave());
switch (const auto event = m_ControlButton.OnUpdate()) {
case Button::Event::PRESS:
if (m_MelodyStorage.IsBufferFull()) {
log("Recorder: melody storage is full");
return;
}
m_IsRecording = !m_IsRecording;
log("Recorder: %s", (m_IsRecording ? "on" : "off"));
if (m_IsRecording) {
if (!m_MelodyStorage.StartRecording()) {
reactToStorageOverflow(buzzer);
}
} else {
m_MelodyStorage.StopRecording();
}
break;
case Button::Event::NONE:
if (m_IsRecording) {
if (!m_MelodyStorage.UpdateMelody(frequency)) {
reactToStorageOverflow(buzzer);
}
}
break;
}
}
private:
void reactToStorageOverflow(const Buzzer& buzzer) {
log("Recorder: overflow attempt detected");
buzzer.PlayAlarm();
m_IsRecording = false;
}
Button m_ControlButton;
bool m_IsRecording{};
MelodyStorage& m_MelodyStorage;
};