-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.cpp
More file actions
279 lines (236 loc) · 5.47 KB
/
Gui.cpp
File metadata and controls
279 lines (236 loc) · 5.47 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "Gui.h"
#include <arduino.h>
#define CURSOR_BLINK_RATE 200
#define REPAINT_RATE 10
Gui::Gui(State &state) : _state(state), _screen(Screen()), _splash(Splash(_screen))
{
redraw(true);
_splash.playPicto(_mode, *this);
}
void Gui::loop()
{
unsigned long now = millis();
if ((now - _lastBlink) >= CURSOR_BLINK_RATE)
{
_lastBlink = now;
_cursorBlinkState = !_cursorBlinkState;
}
if ((now - _lastRepaint) >= REPAINT_RATE)
{
if (_splash.isPlaying())
{
_splash.loop();
}
else
{
_screen.setPixel(_cursorX, _cursorY, _cursorBlinkState && _cursorVisible);
}
_lastRepaint = now;
_screen.repaint();
}
}
void Gui::renderStep(uint8_t step)
{
// chord cursor
_screen.setPixel((step / 4) % 32, 4, true);
_screen.setPixel(((step / 4) + 31) % 32, 4, false);
// notes cursor
_screen.setPixel(step % 32, 15, true);
_screen.setPixel((step + 31) % 32, 15, false);
}
void Gui::redrawChords()
{
// iterate over 8 bars (chords sequence loop)
for (uint8_t step = 0; step < 128; step += 4)
{
// draw chord selections (only one channel for now)
for (uint8_t selectionId = 0; selectionId < 4; selectionId++)
{
_screen.setPixel(step / 4, (3 - selectionId), _state.isChordSelected(step, 0, selectionId));
}
}
}
void Gui::redrawChannel(uint8_t channel)
{
uint8_t vShift = channel == 0 ? 5 : 10;
// iterate over 2 bars (notes sequence loop)
for (uint8_t step = 0; step < 32; step++)
{
// draw note selections
for (uint8_t selectionId = 0; selectionId < 4; selectionId++)
{
_screen.setPixel(step, (3 - selectionId) + vShift, _state.isNoteSelected(step, channel, selectionId));
}
// draw note trigs
_screen.setPixel(step, vShift + 4, (_state.hasTrigOn(step, channel, true) != 0));
}
}
void Gui::redrawAtCursorY()
{
switch (getStateDestination())
{
// chords
case 0:
redrawChords();
break;
// channel 1 note selections / trigs
case 1:
case 2:
redrawChannel(0);
break;
// channel 2 note selections / trigs
case 3:
case 4:
redrawChannel(1);
break;
}
}
void Gui::redraw(bool resetScreen)
{
if(resetScreen)
{
_screen.clear();
}
redrawChords();
redrawChannel(0);
redrawChannel(1);
}
void Gui::showCursor(bool state)
{
_cursorVisible = state;
}
void Gui::moveCursorX(uint8_t n)
{
redrawAtCursorY();
_cursorX = n;
_state.setStepEditAtStep(_cursorX);
}
void Gui::moveCursorY(uint8_t n)
{
_cursorY = n;
redrawAtCursorY();
}
void Gui::clickCursor()
{
uint8_t selectionId;
bool current;
switch (getStateDestination())
{
// chords (assuming cursorY is 0-3)
case 0:
_state.setChordSelected(_cursorX * 4, (3 - _cursorY));
break;
//channel 1 notes (assuming cursorY is 5-8)
case 1:
selectionId = 3 - (_cursorY - 5);
current = _state.isNoteSelected(_cursorX, 0, selectionId);
_state.setNoteSelected(_cursorX, 0, selectionId, !current);
break;
// channel 1 trigs
case 2:
current = _state.hasTrigOn(_cursorX, 0, true);
_state.setTrig(_cursorX, 0, !current);
break;
// channel 2 notes (assuming cursorY is 10-13)
case 3:
selectionId = 3 - (_cursorY - 10);
current = _state.isNoteSelected(_cursorX, 1, selectionId);
_state.setNoteSelected(_cursorX, 1, selectionId, !current);
break;
// channel 2 trigs
case 4:
current = _state.hasTrigOn(_cursorX, 1, true);
_state.setTrig(_cursorX, 1, !current);
break;
}
}
void Gui::switchMode(uint8_t mode)
{
_mode = mode;
_splash.playPicto(mode, *this);
}
void Gui::showNumber(uint8_t number)
{
_splash.showNumber(number, *this);
}
// callback invoked at end of splash screen animation
void Gui::onSplashEnd()
{
redraw(false);
}
void Gui::nudge(uint8_t amount, bool horizontal)
{
switch (getStateDestination())
{
// chords
case 0:
_state.setChordNudge(amount, horizontal);
break;
// channel 1 notes
case 1:
_state.setNotesNudge(0, amount, horizontal);
break;
// channel 1 trigs
case 2:
if (horizontal)
{
_state.setTrigNudge(0, amount);
}
break;
// channel 2 notes
case 3:
_state.setNotesNudge(1, amount, horizontal);
break;
// channel 2 trigs
case 4:
if (horizontal)
{
_state.setTrigNudge(1, amount);
}
break;
}
redrawAtCursorY();
}
void Gui::handleChordIn(uint8_t *chord, uint8_t nbNotes)
{
_state.handleChord(chord, nbNotes, getStateDestination());
redrawAtCursorY();
}
void Gui::loopSteps()
{
_state.loopSteps(getStateDestination());
redrawAtCursorY();
}
uint8_t Gui::getStateDestination()
{
switch (_cursorY)
{
// 0-3 -> chords
case 0:
case 1:
case 2:
case 3:
return 0;
// 5-8 -> channel 1 note selections
case 5:
case 6:
case 7:
case 8:
return 1;
// 9 -> channel 1 trigs
case 9:
return 2;
// 10-13 -> channel 2 note selections
case 10:
case 11:
case 12:
case 13:
return 3;
// 14 -> channel 2 trigs
case 14:
return 4;
// unmapped
default:
return 42;
}
}