-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
210 lines (180 loc) · 6.29 KB
/
main.cpp
File metadata and controls
210 lines (180 loc) · 6.29 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
// ===========================================================================
// MiniLoopDeck - Lo-Fi sample looper for the M5Stack Core2
// ===========================================================================
//
// Controls
// --------
// BtnA (left dot) : REC / STOP - record into the selected track
// BtnB (mid dot) : PLAY / STOP - loop all tracks mixed together
// BtnC (right dot) : FX> - cycle the effect of the selected track
//
// Tap a track tile : select it (tap again to MUTE / un-mute)
// Hold a track tile : clear that track
//
// Effects (per track): DRY, REVERSE, CRUSH, LOFI, DRIVE, ECHO
//
// Remember: mic and speaker share one I2S bus, so recording and playback
// never happen at the same time (no live overdub).
// ===========================================================================
#include <M5Unified.h>
#include "Config.h"
#include "AudioEngine.h"
#include "NetAudio.h"
#include "Ui.h"
static constexpr int VOL_STEP = 15;
static void changeVolume(int delta) {
int v = (int)Engine.volume() + delta;
if (v < 0) v = 0;
if (v > 255) v = 255;
Engine.setVolume((uint8_t)v);
UI.markDirty();
}
// The three transport actions, shared by the hardware dots and the on-screen
// touch buttons. They are context-sensitive (deck vs. settings screen).
static void actionA() { // REC / STOP | VOL-
if (UI.screen() == SCREEN_SETTINGS) { changeVolume(-VOL_STEP); return; }
if (Engine.state() == STATE_RECORDING) {
// A remote (StickS3) take is controlled by KEY1, not this button.
if (Engine.micSource() == MIC_REMOTE) return;
Engine.stopRecording();
} else {
Engine.startRecording(UI.selected());
}
UI.markDirty();
}
static void actionB() { // PLAY / STOP | BACK
if (UI.screen() == SCREEN_SETTINGS) { UI.closeSettings(); return; }
if (Engine.state() == STATE_PLAYING) Engine.stopPlayback();
else if (Engine.state() == STATE_IDLE) Engine.startPlayback();
UI.markDirty();
}
static void actionC() { // FX> | VOL+
if (UI.screen() == SCREEN_SETTINGS) { changeVolume(+VOL_STEP); return; }
Engine.cycleFx(UI.selected());
UI.markDirty();
}
static void handleButtons() {
if (M5.BtnA.wasClicked()) actionA();
if (M5.BtnB.wasClicked()) actionB();
if (M5.BtnC.wasClicked()) actionC();
}
static void handleTouch() {
// Gear button toggles the settings screen from anywhere.
if (UI.gearTapped()) {
if (UI.screen() == SCREEN_DECK) UI.openSettings();
else UI.closeSettings();
return;
}
// In-tile MERGE button: arm that track as the merge target (or cancel).
int mt = UI.pollMergeButton();
if (mt >= 0) {
if (UI.mergeArmed() && mt == UI.selected()) UI.setMergeArmed(false);
else { UI.select((uint8_t)mt); UI.setMergeArmed(true); }
return;
}
// On-screen transport buttons mirror the hardware dots.
int btn = UI.pollBottom();
if (btn == 0) { actionA(); return; }
if (btn == 1) { actionB(); return; }
if (btn == 2) { actionC(); return; }
if (UI.screen() == SCREEN_SETTINGS) {
switch (UI.pollSettings()) {
case SA_VOL_DOWN: changeVolume(-VOL_STEP); break;
case SA_VOL_UP: changeVolume(+VOL_STEP); break;
case SA_BACK: UI.closeSettings(); break;
default: break;
}
return;
}
int hold = UI.pollHold();
if (hold >= 0) {
Engine.clearTrack((uint8_t)hold);
UI.markDirty();
return;
}
int tap = UI.pollTap();
if (tap >= 0) {
if (UI.mergeArmed()) {
// Tapping another track merges it into the selected one; tapping the
// selected track (or an empty one) just cancels merge mode.
if (tap != UI.selected()) Engine.mergeTracks(UI.selected(), (uint8_t)tap);
UI.setMergeArmed(false);
} else if (tap == UI.selected()) {
Engine.toggleMute((uint8_t)tap); // 2nd tap = mute
} else {
UI.select((uint8_t)tap);
}
UI.markDirty();
}
}
void setup() {
auto cfg = M5.config();
cfg.internal_mic = true;
cfg.internal_spk = true;
M5.begin(cfg);
M5.Display.setBrightness(120);
if (!Engine.begin()) {
M5.Display.fillScreen(TFT_RED);
M5.Display.setTextColor(TFT_WHITE);
M5.Display.setTextSize(2);
M5.Display.setCursor(10, 100);
M5.Display.print("PSRAM alloc failed");
while (true) { M5.delay(1000); }
}
Net.begin(); // ESP-NOW receiver for the StickS3 wireless mic (best effort)
UI.begin();
UI.draw();
}
// Drive the remote (StickS3) overdub lifecycle from the network state.
static void handleRemote() {
const bool rec = Net.remoteRecActive();
const DeckState st = Engine.state();
if (rec) {
if (st != STATE_RECORDING) { // begin overdub from idle or playback
Engine.startRemoteRecording(UI.selected());
UI.markDirty();
}
if (Engine.micSource() == MIC_REMOTE) Engine.serviceRemoteRecording(Net);
} else if (st == STATE_RECORDING && Engine.micSource() == MIC_REMOTE) {
Engine.stopRemoteRecording(); // KEY1 released / stream timed out
UI.markDirty();
}
}
void loop() {
M5.update();
handleButtons();
handleTouch();
handleRemote();
if (Engine.state() == STATE_RECORDING && Engine.micSource() == MIC_INTERNAL) {
Engine.serviceRecording();
}
static uint32_t lastStatus = 0;
if (millis() - lastStatus > 300) {
Net.sendStatus((uint8_t)Engine.state(), UI.selected(),
Engine.micSource() == MIC_REMOTE &&
Engine.state() == STATE_RECORDING);
lastStatus = millis();
}
UI.draw(); // only redraws when dirty
static uint32_t lastBar = 0;
if (Engine.state() != STATE_IDLE && millis() - lastBar > 60) {
UI.drawProgress(); // animate the progress bar
lastBar = millis();
}
static uint32_t lastBeat = 0;
if (Engine.state() == STATE_RECORDING && millis() - lastBeat > 25) {
UI.drawMetronome(); // flash the beat grid while recording
lastBeat = millis();
}
static uint32_t lastBatt = 0;
if (millis() - lastBatt > 10000) { // refresh battery gauge every 10 s
UI.refreshBattery();
lastBatt = millis();
}
static uint32_t lastLink = 0;
if (millis() - lastLink > 1000) { // refresh RF link indicator every 1 s
UI.refreshLink();
lastLink = millis();
}
M5.delay(2);
}