/
Display.cpp
335 lines (283 loc) · 7.93 KB
/
Display.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "Display.h"
#include <Arduino.h>
#define row(t) (t * 2)
#define UNLIMITED 0
#define INDICATOR_TIME 25
#define TRACK_INDICATOR_TIME 300
#define MODE_FRAME_TIME 500
#define FLASH_TIME_ON 50
#define FLASH_TIME_OFF 100
#define INDICATOR_ROW 7
#define MODE_ROW 6
#define CLOCK_INDICATOR 0
#define RESET_INDICATOR 1
#define TRACK_ONE_INDICATOR 2
#define TRACK_TWO_INDICATOR 5
#define TRACK_THREE_INDICATOR 7
#define OFF_BEAT_INDICATOR 3
#define MATRIX_ROWS 8
#define MATRIX_COLUMNS 8
#define ALL_ON 255
#define LED_ON 1
#define ALL_OFF 0
#define LED_OFF 0
#define TRACKS 3
#define CURSORS TRACKS
Display::Display()
: frame{0, 0, 0, false},
clock{INDICATOR_ROW, CLOCK_INDICATOR, false, 0},
reset{INDICATOR_ROW, RESET_INDICATOR, false, 0},
tracks{{INDICATOR_ROW, TRACK_ONE_INDICATOR, false, 0},
{INDICATOR_ROW, TRACK_TWO_INDICATOR, false, 0},
{INDICATOR_ROW, TRACK_THREE_INDICATOR, false, 0},
{INDICATOR_ROW, OFF_BEAT_INDICATOR, false, 0}} {
}
void Display::initialise() {
matrix.initialise();
simley();
}
void Display::render() {
updateFlashState();
updateFrame();
updateCursors();
updateIndicators();
for (int row = 0; row < MATRIX_ROWS; ++ row) {
if (frame.active) matrix.setRow(row, getFrameRow(row));
else matrix.setRow(row, display[row].state ^ cursorMask[row].state);
}
}
int Display::getFrameRow(int row) {
int frameRow = (frame.image >> row * MATRIX_ROWS) & 0xFF;
if (frame.clocked && !clock.active) frameRow = frameRow & frame.image;
return frameRow;
}
void Display::updateFlashState() {
unsigned long now = millis();
if ((flashState && (now - cursorTime) > FLASH_TIME_ON) || (!flashState && (now - cursorTime) > FLASH_TIME_OFF)) {
flashState = !flashState;
cursorTime = now;
}
}
void Display::updateFrame() {
if (frame.active) {
if (frame.time != 0 && (millis() - frame.start > frame.time)) {
frame.active = false;
frame.start = 0;
}
}
}
void Display::updateCursors() {
clear(cursorMask);
for (int cursor = 0; cursor < CURSORS; ++cursor) updateCursorMask(cursor);
}
void Display::updateCursorMask(int cursor) {
if(cursors[cursor].active) {
bitWrite(cursorMask[cursors[cursor].row].state, cursors[cursor].position, flashState);
}
}
void Display::updateIndicators() {
updateIndicator(clock);
updateIndicator(reset);
for(int track = 0; track <= TRACKS; ++track) updateIndicator(tracks[track]);
updateTrackIndicator(activeTrack);
}
void Display::updateIndicator(Indicator& indicator) {
if (indicator.active) {
if (indicator.start == 0 || (millis() - indicator.start > INDICATOR_TIME)) {
indicator.active = false;
indicator.start = 0;
}
setLed(indicator.row, indicator.column, indicator.active);
}
}
void Display::updateTrackIndicator(TrackIndicator& indicator) {
if (indicator.active) {
int row = row(indicator.track);
setRows(row, ALL_OFF);
cursorMask[row].state = flashState ? ALL_ON : ALL_OFF;
cursorMask[row + 1].state = flashState ? ALL_ON : ALL_OFF;
if (indicator.start == 0 || (millis() - indicator.start > TRACK_INDICATOR_TIME)) {
indicator.active = false;
indicator.start = 0;
}
}
}
void Display::showCursor(int track, bool visible) {
cursors[track].active = visible;
}
void Display::drawPlayView(int track, int position, int pattern, bool cursor) {
showCursor(track, cursor);
setRows(row(track), pattern);
setTrackCursor(track, position);
// int state = 0;
// if(position < MATRIX_COLUMNS) {
// state = lowByte(pattern);
// position += MATRIX_COLUMNS;
// } else {
// state = highByte(pattern);
// }
// bitSet(state, position);
// setRows(row(track), state);
}
void Display::drawProgrammedView(int track, int pattern) {
timeout();
showCursor(track, true);
setRows(row(track), pattern);
}
void Display::drawEuclideanView(int track, int pattern) {
timeout();
showCursor(track, false);
setRows(row(track), pattern);
}
void Display::drawOffsetView(int track, int pattern) {
timeout();
showCursor(track, false);
setRows(row(track), pattern);
}
void Display::drawLengthView(int track, int start, int end, bool active) {
timeout();
setTrackCursor(track, active ? end : start);
showCursor(track, true);
int state = 0;
setRange(state, start, end, LED_ON);
setRows(row(track), state);
}
void Display::drawLengthView(int track, int length) {
timeout();
drawLengthView(track, 0, length, true);
}
void Display::drawShuffleView(int track, int shuffle) {
showFrame(&SHUFFLE[shuffle]);
}
void Display::drawPlayModeView(int track, PlayMode mode) {
showFrame(&PLAY_MODES[mode]);
}
void Display::drawOutModeView(int track, OutMode mode) {
showFrame(&OUT_MODES[mode]);
}
void Display::drawPatternTypeView(int track, PatternType mode) {
showFrame(&PATTERN_MODES[mode]);
}
void Display::drawDividerView(int track, int divider, DividerType type) {
switch(type) {
case Beat:
showFrame(&BEAT_DIVIDERS[divider]);
break;
case Triplet:
showFrame(&TRIPLET_DIVIDERS[divider]);
break;
}
}
void Display::drawDividerTypeView(int track, DividerType type) {
showFrame(&DIVIDER_TYPES[type]);
}
void Display::drawMutationView(int track, int mutation) {
showFrame(&MUTATION_CHANCE[mutation]);
}
void Display::drawMutationSeedView(int track, MutationSeed seed) {
showFrame(&MUTATION_SEEDS[seed]);
}
void Display::drawClockSpeed(bool state) {
if(state) showClockedFrame(&CLOCK_STATE[state]);
else showFrame(&CLOCK_STATE[state]);
}
void Display::drawClockWidth(int width) {
showFrame(&CLOCK_WIDTH[width]);
}
void Display::drawOffbeatOutput(bool offBeat) {
showFrame(&OFFBEAT_OUTPUT[offBeat]);
}
void Display::setRows(int row, int state) {
setRow(row, lowByte(state));
setRow(row + 1, highByte(state));
}
void Display::setLed(int row, int column, bool state) {
bitWrite(display[row].state, column, state);
}
void Display::setRow(int row, byte state) {
display[row].state = state;
}
void Display::clear() {
clear(display);
timeout();
}
void Display::timeout() {
frame.active = false;
}
void Display::clear(DisplayRow rows[]) {
for (int row = 0; row < MATRIX_ROWS; ++row) {
rows[row].state = ALL_OFF;
}
}
void Display::indicateClock() {
showIndicator(clock);
}
void Display::indicateReset() {
showIndicator(reset);
}
void Display::indicateTrack(int track) {
showIndicator(tracks[track]);
}
void Display::indicateActiveTrack(int track) {
activeTrack.track = track;
activeTrack.active = true;
activeTrack.start = millis();
}
void Display::showIndicator(Indicator& indicator) {
indicator.active = true;
indicator.start = millis();
}
void Display::showFrame(const uint64_t *image) {
showFrame(image, UNLIMITED, false);
}
void Display::showClockedFrame(const uint64_t *image) {
showFrame(image, UNLIMITED, true);
}
void Display::showTimedFrame(const uint64_t *image, unsigned long time) {
showFrame(image, time, false);
}
void Display::showFrame(const uint64_t *image, unsigned long time, bool clocked) {
memcpy_P(&frame.image, image, MATRIX_ROWS);
frame.time = time;
frame.start = millis();
frame.active = true;
frame.clocked = clocked;
}
void Display::indicateMode(int mode) {
byte state = 0;
bitSet(state, mode);
setRow(MODE_ROW, state);
showTimedFrame(&MODES[mode], MODE_FRAME_TIME);
}
void Display::setTrackCursor(int track, int position) {
int row = row(track);
if(position >= MATRIX_ROWS) {
++row;
position %= MATRIX_ROWS;
}
cursors[track].row = row;
cursors[track].position = position;
}
void Display::fill(int &value, int length, int bit) {
setRange(value, 0, length, bit);
}
void Display::setRange(int &value, int start, int end, int bit) {
for(int i = start; i <= end; ++i) {
bitWrite(value, i, bit);
}
}
void Display::simley() {
for (int repeat = 0; repeat < 3; ++ repeat) {
showFrame(&INVERSE_SMILE);
render();
delay(100);
showFrame(&SMILE);
render();
delay(100);
}
showFrame(&INVERSE_SMILE);
render();
delay(100);
clear();
render();
}