-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.cpp
More file actions
138 lines (116 loc) · 3.1 KB
/
Storage.cpp
File metadata and controls
138 lines (116 loc) · 3.1 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
#include "Storage.h"
#include <arduino.h>
#include <EEPROM.h>
#include "MidiIo.h"
#define STORAGE_SETTINGS_SPACE_SHIFT 5
#define STORAGE_SETTINGS_VERSION 1
Storage::Storage(State &state) : _state(state)
{
restoreState();
restoreSettings();
}
void Storage::writeState()
{
int idx = STORAGE_SETTINGS_SPACE_SHIFT;
EEPROM.update(idx++, STORAGE_STATE_VERSION);
// chords
for (uint8_t i = 0; i < NB_CHORDS; i++)
{
for (uint8_t j = 0; j < NB_NOTES_PER_CHORD; j++)
{
EEPROM.update(idx++, _state._chords[i][j]);
}
}
// chord selections
for (uint8_t i = 0; i < NB_CHORD_BARS; i++)
{
for (uint8_t j = 0; j < NB_CHORDS_PER_BAR; j++)
{
EEPROM.update(idx++, _state._chordSel[i][j]);
}
}
// note selections & trigs
for (uint8_t c = 0; c < NB_CHANNELS; c++)
{
for (uint8_t i = 0; i < NB_NOTES_BARS; i++)
{
for (uint8_t j = 0; j < NB_NOTES_PER_BAR; j++)
{
EEPROM.update(idx++, _state._notesSel[c][i][j]);
EEPROM.update(idx++, _state._trigs[c][i][j]);
}
}
}
// transpositions
for (uint8_t c = 0; c < NB_CHANNELS; c++)
{
EEPROM.update(idx++, _state._transpose[c]);
}
}
bool Storage::restoreState()
{
int idx = STORAGE_SETTINGS_SPACE_SHIFT;
uint8_t version = EEPROM.read(idx++);
if (version != STORAGE_STATE_VERSION)
{
_state.reset(false);
return false;
}
_state.reset(true);
// chords
for (uint8_t i = 0; i < NB_CHORDS; i++)
{
for (uint8_t j = 0; j < NB_NOTES_PER_CHORD; j++)
{
_state._chords[i][j] = EEPROM.read(idx++);
}
}
// chord selections
for (uint8_t i = 0; i < NB_CHORD_BARS; i++)
{
for (uint8_t j = 0; j < NB_CHORDS_PER_BAR; j++)
{
_state._chordSel[i][j] = EEPROM.read(idx++);
}
}
// note selections & trigs
for (uint8_t c = 0; c < NB_CHANNELS; c++)
{
for (uint8_t i = 0; i < NB_NOTES_BARS; i++)
{
for (uint8_t j = 0; j < NB_NOTES_PER_BAR; j++)
{
_state._notesSel[c][i][j] = EEPROM.read(idx++);
_state._trigs[c][i][j] = EEPROM.read(idx++);
}
}
}
// transpositions
for (uint8_t c = 0; c < NB_CHANNELS; c++)
{
_state._transpose[c] = EEPROM.read(idx++);
}
return true;
}
void Storage::writeSettings()
{
int idx = 0;
EEPROM.update(idx++, STORAGE_SETTINGS_VERSION);
EEPROM.update(idx++, midiIo::getMidiChannelIn());
EEPROM.update(idx++, midiIo::getMidiChannelOut(0));
EEPROM.update(idx++, midiIo::getMidiChannelOut(1));
EEPROM.update(idx++, 0); // reserved
}
bool Storage::restoreSettings()
{
int idx = 0;
uint8_t version = EEPROM.read(idx++);
if (version != STORAGE_SETTINGS_VERSION)
{
return false;
}
midiIo::setMidiChannelIn(EEPROM.read(idx++));
midiIo::setMidiChannelOut(0, EEPROM.read(idx++));
midiIo::setMidiChannelOut(1, EEPROM.read(idx++));
return true;
}