-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUi.cpp
More file actions
416 lines (358 loc) · 14.6 KB
/
Ui.cpp
File metadata and controls
416 lines (358 loc) · 14.6 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "Ui.h"
#include "AudioEngine.h"
#include "NetAudio.h"
#include <M5Unified.h>
#include <math.h>
Ui UI;
// Layout (320 x 240) -------------------------------------------------------
static constexpr int HEADER_H = 34;
static constexpr int GRID_Y = HEADER_H + 2; // 36
static constexpr int GRID_H = 144; // -> 180
static constexpr int BAR_Y = GRID_Y + GRID_H; // 180
static constexpr int TILE_W = 158;
static constexpr int TILE_H = 70;
static constexpr int COLS = 2;
// Settings hit zone = whole top-right corner above the tiles (easy to tap).
static constexpr int SET_ZONE_X = 196;
// Per-tile MERGE button (top-right corner of each sound box).
static constexpr int MB_W = 30, MB_H = 22, MB_MARGIN = 3;
// Header widgets
static constexpr int GEAR_CX = 236;
static constexpr int GEAR_CY = 17;
static constexpr int GEAR_R = 13;
static constexpr int BATT_X = 288; // body left
static constexpr int BATT_Y = 10;
static constexpr int BATT_W = 24;
static constexpr int BATT_H = 15;
// Settings volume controls (kept above the transport button row)
static constexpr int VOL_BAR_X = 30, VOL_BAR_Y = 78, VOL_BAR_W = 260, VOL_BAR_H = 26;
static constexpr int VOL_MINUS_X = 30, VOL_BTN_Y = 116, VOL_BTN_W = 80, VOL_BTN_H = 52;
static constexpr int VOL_PLUS_X = 210;
// Bottom transport buttons (touchable, mirror the hardware A/B/C dots)
static constexpr int BTN_Y = 192, BTN_H = 44, BTN_W = 100;
static int btnX(int i) { return 4 + i * 106; } // 4, 110, 216
static int btnCx(int i) { return btnX(i) + BTN_W / 2; }
// Colours
static constexpr uint16_t C_BG = TFT_BLACK;
static constexpr uint16_t C_HEADER = 0x18C3; // dark blue-grey
static constexpr uint16_t C_TILE = 0x2104; // dark grey
static constexpr uint16_t C_SEL = 0x04A0; // green border
static constexpr uint16_t C_REC = 0xF800; // red
static constexpr uint16_t C_PLAY = 0x07E0; // green
static constexpr uint16_t C_MUTE = 0x7BEF; // grey text
static constexpr uint16_t C_EMPTY = 0x528A; // dim
static constexpr uint16_t C_YELLOW = 0xFFE0;
static void tileRect(uint8_t i, int& x, int& y) {
x = (i % COLS) * (TILE_W + 4) + 2;
y = GRID_Y + (i / COLS) * (TILE_H + 4);
}
static void mergeBtnRect(uint8_t i, int& mx, int& my) {
int x, y;
tileRect(i, x, y);
mx = x + TILE_W - MB_W - MB_MARGIN;
my = y + MB_MARGIN;
}
static bool inRect(int px, int py, int x, int y, int w, int h) {
return px >= x && px < x + w && py >= y && py < y + h;
}
void Ui::begin() {
M5.Display.setRotation(1);
M5.Display.fillScreen(C_BG);
M5.Touch.setHoldThresh(600);
_dirty = true;
}
// ---------------------------------------------------------------------------
// Header: title, gear button, battery gauge
// ---------------------------------------------------------------------------
void Ui::drawGear(int cx, int cy, uint16_t col) {
// 8 teeth around a ring with a hole in the middle.
for (int a = 0; a < 8; ++a) {
float r = a * (float)M_PI / 4.0f;
int tx = cx + (int)roundf(cosf(r) * (GEAR_R));
int ty = cy + (int)roundf(sinf(r) * (GEAR_R));
M5.Display.fillCircle(tx, ty, 2, col);
}
M5.Display.fillCircle(cx, cy, GEAR_R - 2, col);
M5.Display.fillCircle(cx, cy, 3, C_HEADER); // hole
}
void Ui::drawBattery() {
// Clear the gauge area (header background).
M5.Display.fillRect(255, 0, 320 - 255, HEADER_H, C_HEADER);
int level = M5.Power.getBatteryLevel();
if (level < 0) level = 0;
if (level > 100) level = 100;
const bool charging = (M5.Power.isCharging() == 1);
uint16_t col = (level > 50) ? C_PLAY : (level > 20 ? C_YELLOW : C_REC);
if (charging) col = C_PLAY;
// Body + terminal nub.
M5.Display.drawRect(BATT_X, BATT_Y, BATT_W, BATT_H, TFT_WHITE);
M5.Display.fillRect(BATT_X + BATT_W, BATT_Y + 4, 2, BATT_H - 8, TFT_WHITE);
int innerW = (BATT_W - 4) * level / 100;
M5.Display.fillRect(BATT_X + 2, BATT_Y + 2, BATT_W - 4, BATT_H - 4, C_HEADER);
M5.Display.fillRect(BATT_X + 2, BATT_Y + 2, innerW, BATT_H - 4, col);
// Percentage to the left of the icon.
M5.Display.setTextSize(1);
M5.Display.setTextColor(TFT_WHITE, C_HEADER);
M5.Display.setTextDatum(textdatum_t::middle_right);
M5.Display.drawString(charging ? (String(level) + "%+") : (String(level) + "%"),
BATT_X - 4, GEAR_CY);
M5.Display.setTextDatum(textdatum_t::top_left);
}
void Ui::drawHeader() {
M5.Display.fillRect(0, 0, 320, HEADER_H, C_HEADER);
M5.Display.setTextDatum(textdatum_t::middle_left);
M5.Display.setTextColor(TFT_WHITE, C_HEADER);
M5.Display.setTextSize(2);
M5.Display.drawString(_screen == SCREEN_SETTINGS ? "Settings" : "MiniLoopDeck",
6, HEADER_H / 2);
M5.Display.setTextDatum(textdatum_t::top_left);
drawRf();
drawGear(GEAR_CX, GEAR_CY, _screen == SCREEN_SETTINGS ? C_PLAY : TFT_WHITE);
drawBattery();
}
void Ui::drawRf() {
// Compact link dot between the MERGE button and the gear (visual only).
const int x = 208, cy = GEAR_CY;
const bool linked = Net.connected();
M5.Display.fillRect(x - 6, 0, 16, HEADER_H, C_HEADER);
M5.Display.fillCircle(x, cy, 4, linked ? C_PLAY : C_EMPTY);
}
void Ui::refreshBattery() { drawBattery(); }
void Ui::refreshLink() { drawRf(); }
// ---------------------------------------------------------------------------
// Deck screen
// ---------------------------------------------------------------------------
void Ui::drawTile(uint8_t i) {
int x, y;
tileRect(i, x, y);
const Track& tr = Engine.track(i);
const bool sel = (i == _selected);
const bool recHere =
(Engine.state() == STATE_RECORDING && Engine.recordingTrk() == i);
M5.Display.fillRoundRect(x, y, TILE_W, TILE_H, 6, C_TILE);
if (sel) {
// Merge target = amber, normal selection = green.
uint16_t bc = _mergeArmed ? C_YELLOW : C_SEL;
M5.Display.drawRoundRect(x, y, TILE_W, TILE_H, 6, bc);
M5.Display.drawRoundRect(x + 1, y + 1, TILE_W - 2, TILE_H - 2, 6, bc);
} else if (_mergeArmed && tr.hasContent()) {
// Candidate to merge into the selected track.
M5.Display.drawRoundRect(x, y, TILE_W, TILE_H, 6, TFT_CYAN);
}
// Track number stays small.
M5.Display.setTextDatum(textdatum_t::top_left);
M5.Display.setTextColor(TFT_WHITE, C_TILE);
M5.Display.setTextSize(1);
M5.Display.setCursor(x + 8, y + 6);
M5.Display.printf("TRK %d", i + 1);
// Status line - bigger now.
M5.Display.setTextSize(2);
M5.Display.setCursor(x + 8, y + 20);
if (recHere) {
const bool remote = Engine.micSource() == MIC_REMOTE;
M5.Display.setTextColor(remote ? TFT_CYAN : C_REC, C_TILE);
M5.Display.print(remote ? "OVERDUB" : "REC");
} else if (!tr.hasContent()) {
M5.Display.setTextColor(C_EMPTY, C_TILE);
M5.Display.print("empty");
} else {
M5.Display.setTextColor(tr.muted ? C_MUTE : C_PLAY, C_TILE);
float secs = (float)tr.length / SAMPLE_RATE;
M5.Display.printf("%s %.1fs", tr.muted ? "MUTE" : "LOOP", secs);
}
// Effect badge - bigger now, bottom-right.
M5.Display.setTextSize(2);
M5.Display.setTextDatum(textdatum_t::bottom_right);
M5.Display.setTextColor(tr.fx == FX_NONE ? C_EMPTY : C_YELLOW, C_TILE);
M5.Display.drawString(FX_NAMES[tr.fx], x + TILE_W - 8, y + TILE_H - 6);
M5.Display.setTextDatum(textdatum_t::top_left);
// Merge button (top-right corner): arm this tile as the merge target.
int mx, my;
mergeBtnRect(i, mx, my);
const bool isTarget = _mergeArmed && sel;
const uint16_t mfill = isTarget ? C_PLAY : C_HEADER;
M5.Display.fillRoundRect(mx, my, MB_W, MB_H, 4, mfill);
M5.Display.setTextSize(1);
M5.Display.setTextColor(TFT_WHITE, mfill);
M5.Display.setTextDatum(textdatum_t::middle_center);
M5.Display.drawString("MIX", mx + MB_W / 2, my + MB_H / 2);
M5.Display.setTextDatum(textdatum_t::top_left);
}
void Ui::drawButtonBar() {
const DeckState st = Engine.state();
const char* lbl[3];
uint16_t fill[3];
if (_screen == SCREEN_SETTINGS) {
lbl[0] = "VOL-"; lbl[1] = "BACK"; lbl[2] = "VOL+";
fill[0] = fill[1] = fill[2] = C_TILE;
} else {
lbl[0] = (st == STATE_RECORDING) ? "STOP" : "REC";
lbl[1] = (st == STATE_PLAYING) ? "STOP" : "PLAY";
lbl[2] = "FX>";
fill[0] = (st == STATE_RECORDING) ? C_REC : C_TILE;
fill[1] = (st == STATE_PLAYING) ? C_PLAY : C_TILE;
fill[2] = C_TILE;
}
M5.Display.fillRect(0, BTN_Y - 2, 320, 240 - (BTN_Y - 2), C_BG);
M5.Display.setTextSize(2);
M5.Display.setTextDatum(textdatum_t::middle_center);
for (int i = 0; i < 3; ++i) {
M5.Display.fillRoundRect(btnX(i), BTN_Y, BTN_W, BTN_H, 8, fill[i]);
M5.Display.setTextColor(TFT_WHITE, fill[i]);
M5.Display.drawString(lbl[i], btnCx(i), BTN_Y + BTN_H / 2);
}
M5.Display.setTextDatum(textdatum_t::top_left);
}
void Ui::drawProgress() {
if (_screen != SCREEN_DECK) return;
const int y = BAR_Y + 4;
const int w = (int)(320 * Engine.progress());
uint16_t col = (Engine.state() == STATE_RECORDING) ? C_REC : C_PLAY;
M5.Display.fillRect(0, y, 320, 6, C_TILE);
if (Engine.state() != STATE_IDLE) {
M5.Display.fillRect(0, y, w, 6, col);
}
}
void Ui::drawDeck() {
M5.Display.fillRect(0, GRID_Y, 320, 240 - GRID_Y, C_BG);
for (uint8_t i = 0; i < NUM_TRACKS; ++i) drawTile(i);
drawProgress();
drawButtonBar();
}
// ---------------------------------------------------------------------------
// Settings screen
// ---------------------------------------------------------------------------
void Ui::drawVolume() {
const uint8_t v = Engine.volume();
const int pct = (v * 100) / 255;
// Label
M5.Display.setTextDatum(textdatum_t::top_left);
M5.Display.setTextSize(2);
M5.Display.setTextColor(TFT_WHITE, C_BG);
M5.Display.setCursor(VOL_BAR_X, VOL_BAR_Y - 26);
M5.Display.print("Volume");
// Value, right aligned
M5.Display.setTextDatum(textdatum_t::top_right);
M5.Display.setTextColor(C_PLAY, C_BG);
M5.Display.drawString(String(pct) + "%", VOL_BAR_X + VOL_BAR_W, VOL_BAR_Y - 26);
M5.Display.setTextDatum(textdatum_t::top_left);
// Bar
int fill = (VOL_BAR_W - 4) * v / 255;
M5.Display.drawRoundRect(VOL_BAR_X, VOL_BAR_Y, VOL_BAR_W, VOL_BAR_H, 4, TFT_WHITE);
M5.Display.fillRect(VOL_BAR_X + 2, VOL_BAR_Y + 2,
VOL_BAR_W - 4, VOL_BAR_H - 4, C_TILE);
M5.Display.fillRect(VOL_BAR_X + 2, VOL_BAR_Y + 2, fill, VOL_BAR_H - 4, C_PLAY);
}
void Ui::drawSettings() {
M5.Display.fillRect(0, GRID_Y, 320, 240 - GRID_Y, C_BG);
drawVolume();
// Wireless mic (StickS3) status line.
const bool linked = Net.connected();
M5.Display.setTextSize(2);
M5.Display.setTextDatum(textdatum_t::top_left);
M5.Display.setTextColor(TFT_WHITE, C_BG);
M5.Display.setCursor(VOL_MINUS_X, 174);
M5.Display.print("Stick mic: ");
M5.Display.setTextColor(linked ? C_PLAY : C_EMPTY, C_BG);
M5.Display.print(linked ? "linked" : "----");
// Minus / plus touch buttons
M5.Display.fillRoundRect(VOL_MINUS_X, VOL_BTN_Y, VOL_BTN_W, VOL_BTN_H, 8, C_TILE);
M5.Display.fillRoundRect(VOL_PLUS_X, VOL_BTN_Y, VOL_BTN_W, VOL_BTN_H, 8, C_TILE);
M5.Display.setTextSize(4);
M5.Display.setTextColor(TFT_WHITE, C_TILE);
M5.Display.setTextDatum(textdatum_t::middle_center);
M5.Display.drawString("-", VOL_MINUS_X + VOL_BTN_W / 2, VOL_BTN_Y + VOL_BTN_H / 2);
M5.Display.drawString("+", VOL_PLUS_X + VOL_BTN_W / 2, VOL_BTN_Y + VOL_BTN_H / 2);
M5.Display.setTextDatum(textdatum_t::top_left);
drawButtonBar();
}
// ---------------------------------------------------------------------------
void Ui::draw() {
if (!_dirty) return;
_dirty = false;
drawHeader();
if (_screen == SCREEN_DECK) drawDeck();
else drawSettings();
}
// ---------------------------------------------------------------------------
// Input
// ---------------------------------------------------------------------------
int Ui::tileAt(int x, int y) const {
if (y < GRID_Y || y >= BAR_Y) return -1;
int col = x / (TILE_W + 4);
int row = (y - GRID_Y) / (TILE_H + 4);
if (col >= COLS) col = COLS - 1;
int idx = row * COLS + col;
return (idx < NUM_TRACKS) ? idx : -1;
}
int Ui::pollTap() {
if (_screen != SCREEN_DECK || M5.Touch.getCount() == 0) return -1;
auto t = M5.Touch.getDetail(0);
if (!t.wasClicked()) return -1;
return tileAt(t.x, t.y);
}
int Ui::pollHold() {
if (_screen != SCREEN_DECK || M5.Touch.getCount() == 0) return -1;
auto t = M5.Touch.getDetail(0);
if (!t.wasHold()) return -1;
return tileAt(t.x, t.y);
}
bool Ui::gearTapped() {
if (M5.Touch.getCount() == 0) return false;
auto t = M5.Touch.getDetail(0);
if (!t.wasClicked()) return false;
// Whole top-right corner above the tiles, so a slightly-low tap still hits.
return inRect(t.x, t.y, SET_ZONE_X, 0, 320 - SET_ZONE_X, GRID_Y);
}
int Ui::pollMergeButton() {
if (_screen != SCREEN_DECK || M5.Touch.getCount() == 0) return -1;
auto t = M5.Touch.getDetail(0);
if (!t.wasClicked()) return -1;
for (uint8_t i = 0; i < NUM_TRACKS; ++i) {
int mx, my;
mergeBtnRect(i, mx, my);
if (inRect(t.x, t.y, mx, my, MB_W, MB_H)) return i;
}
return -1;
}
SettingsAction Ui::pollSettings() {
if (_screen != SCREEN_SETTINGS || M5.Touch.getCount() == 0) return SA_NONE;
auto t = M5.Touch.getDetail(0);
if (!t.wasClicked()) return SA_NONE;
if (inRect(t.x, t.y, VOL_MINUS_X, VOL_BTN_Y, VOL_BTN_W, VOL_BTN_H)) return SA_VOL_DOWN;
if (inRect(t.x, t.y, VOL_PLUS_X, VOL_BTN_Y, VOL_BTN_W, VOL_BTN_H)) return SA_VOL_UP;
return SA_NONE;
}
int Ui::pollBottom() {
if (M5.Touch.getCount() == 0) return -1;
auto t = M5.Touch.getDetail(0);
if (!t.wasClicked() || t.y < BTN_Y) return -1;
for (int i = 0; i < 3; ++i) {
if (inRect(t.x, t.y, btnX(i), BTN_Y, BTN_W, BTN_H)) return i;
}
return -1;
}
// ---------------------------------------------------------------------------
// Visual metronome: while recording a follower (speaker is muted, so the
// performer can't hear the loop), flash the recording tile border on the beat
// grid derived from the master loop length. Downbeat = cyan, off-beats = amber.
void Ui::drawMetronome() {
if (Engine.state() != STATE_RECORDING || !Engine.hasMaster()) return;
uint32_t beatSamples = Engine.masterLoopSamples() / BEATS_PER_LOOP;
if (beatSamples == 0) return;
uint32_t beatMs = 1000u * beatSamples / SAMPLE_RATE;
if (beatMs == 0) return;
uint32_t elapsed = Engine.recElapsedMs();
uint32_t beatIndex = elapsed / beatMs;
uint32_t phaseMs = elapsed - beatIndex * beatMs;
bool flash = phaseMs < BEAT_FLASH_MS;
bool downbeat = (beatIndex % BEATS_PER_LOOP) == 0;
int x, y;
tileRect(Engine.recordingTrk(), x, y);
uint16_t col;
if (flash) col = downbeat ? TFT_CYAN : C_YELLOW;
else col = (Engine.recordingTrk() == _selected) ? C_SEL : C_TILE;
for (int r = 0; r < 3; ++r) {
M5.Display.drawRoundRect(x + r, y + r, TILE_W - 2 * r, TILE_H - 2 * r, 6, col);
}
}