-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmelody-selector.hpp
61 lines (54 loc) · 1.75 KB
/
melody-selector.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
61
#pragma once
#include "button.hpp"
#include "melody-storage.hpp"
class MelodySelector {
public:
MelodySelector(uint8_t previousMelodyButtonPin,
uint8_t nextMelodyButtonPin,
const MelodyStorage& melodyStorage)
: m_PreviousMelodyButton{ previousMelodyButtonPin, INPUT_PULLUP },
m_NextMelodyButton{ nextMelodyButtonPin, INPUT_PULLUP },
m_MelodyStorage{ melodyStorage } {}
// Moving between recorded melodies in melody storage
void OnUpdate() {
if (const auto event = m_PreviousMelodyButton.OnUpdate();
event == Button::Event::PRESS) {
MoveToPreviousMelody();
log("MelodySelector: previous melody, current index=%hu", m_CurrentMelodyIndex);
}
if (const auto event = m_NextMelodyButton.OnUpdate();
event == Button::Event::PRESS) {
MoveToNextMelody();
log("MelodySelector: next melody, current index=%hu", m_CurrentMelodyIndex);
}
}
[[nodiscard]] size_t GetCurrentMelodyIndex() const {
return m_CurrentMelodyIndex;
}
private:
void MoveToPreviousMelody() {
if (m_CurrentMelodyIndex == 0) {
return;
}
--m_CurrentMelodyIndex;
while (m_CurrentMelodyIndex != 0
&& !m_MelodyStorage.Read(m_CurrentMelodyIndex - 1).IsSeparator) {
--m_CurrentMelodyIndex;
}
}
void MoveToNextMelody() {
size_t i = m_CurrentMelodyIndex + 1;
while (i != m_MelodyStorage.GetUsedLength()
&& !m_MelodyStorage.Read(i - 1).IsSeparator) {
++i;
}
if (i != m_MelodyStorage.GetUsedLength()
&& !m_MelodyStorage.Read(i).IsSeparator) {
m_CurrentMelodyIndex = i;
}
}
Button m_PreviousMelodyButton;
Button m_NextMelodyButton;
size_t m_CurrentMelodyIndex{};
const MelodyStorage& m_MelodyStorage;
};