-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_stick.cpp
More file actions
206 lines (171 loc) · 6.16 KB
/
main_stick.cpp
File metadata and controls
206 lines (171 loc) · 6.16 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
// ===========================================================================
// MiniLoopDeck - wireless microphone firmware for the M5Stack StickS3
// ===========================================================================
//
// Hold KEY1 (BtnA) to record: the ES8311 mic is captured in 20 ms frames,
// IMA-ADPCM encoded and broadcast to the Core2 over ESP-NOW (channel 1).
// The Core2 plays its loops the whole time -> true overdub.
//
// Display shows mic level, REC state, link status and battery.
// ===========================================================================
#include <M5Unified.h>
#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include "AudioLink.h"
static const uint8_t BCAST[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
static int16_t frame[FRAME_SAMPLES];
static uint16_t g_seq = 0;
static int16_t g_pred = 0; // running ADPCM state across frames
static int8_t g_index = 0;
static uint8_t g_warmup = 0; // frames to skip after KEY1 press
// Mirrored deck status (from Core2 StatusPacket) for the display.
static volatile uint32_t g_lastStatusMs = 0;
static volatile uint8_t g_deckState = 0;
static volatile uint8_t g_selTrack = 0;
static int16_t g_level = 0; // last frame peak, for the meter
// Off-screen canvas -> draw once, push once. Kills the flicker that direct
// fillScreen-per-frame drawing caused.
static M5Canvas g_canvas(&M5.Display);
// ---------------------------------------------------------------------------
static void onRecv(const uint8_t* mac, const uint8_t* data, int len) {
(void)mac;
if (len < (int)sizeof(StatusPacket)) return;
const StatusPacket* p = (const StatusPacket*)data;
if (p->magic != LINK_MAGIC || p->type != LT_STATUS) return;
g_deckState = p->deckState;
g_selTrack = p->selTrack;
g_lastStatusMs = millis();
}
static void linkBegin() {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
esp_wifi_set_channel(LINK_CHANNEL, WIFI_SECOND_CHAN_NONE);
if (esp_now_init() != ESP_OK) {
M5.Display.println("ESP-NOW init failed");
return;
}
esp_now_register_recv_cb(onRecv);
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, BCAST, 6);
peer.channel = LINK_CHANNEL;
peer.encrypt = false;
esp_now_add_peer(&peer);
}
// ---------------------------------------------------------------------------
static void captureAndSend() {
// Blocking ~20 ms capture of one frame.
if (!M5.Mic.record(frame, FRAME_SAMPLES, SAMPLE_RATE_HZ, false)) return;
while (M5.Mic.isRecording()) { M5.delay(1); }
// Peak for the level meter.
int16_t peak = 0;
for (uint16_t i = 0; i < FRAME_SAMPLES; ++i) {
int16_t a = frame[i] < 0 ? -frame[i] : frame[i];
if (a > peak) peak = a;
}
g_level = peak;
// Skip the mic settling frames right after KEY1 is pressed.
if (g_warmup) { --g_warmup; return; }
AudioPacket pkt;
pkt.magic = LINK_MAGIC;
pkt.ver = LINK_VER;
pkt.type = LT_AUDIO;
pkt.flags = LF_RECORDING;
pkt.seq = g_seq++;
pkt.predictor = g_pred; // entry state, so the frame self-decodes
pkt.stepIndex = (uint8_t)g_index;
pkt.pad = 0;
adpcm_encodeFrame(frame, pkt.data, g_pred, g_index);
esp_now_send(BCAST, (const uint8_t*)&pkt, sizeof(pkt));
}
// Idle heartbeat so the Core2 shows "linked" even when not recording.
static void sendHeartbeat() {
AudioPacket pkt;
memset(&pkt, 0, sizeof(pkt));
pkt.magic = LINK_MAGIC;
pkt.ver = LINK_VER;
pkt.type = LT_AUDIO;
pkt.flags = 0; // not recording
pkt.seq = g_seq++;
esp_now_send(BCAST, (const uint8_t*)&pkt, sizeof(pkt));
}
// ---------------------------------------------------------------------------
static void drawUi(bool recording) {
static uint32_t last = 0;
if (millis() - last < 50) return;
last = millis();
auto& g = g_canvas;
const int W = g.width();
const int H = g.height();
g.fillScreen(TFT_BLACK);
// Title + REC dot
g.setTextDatum(textdatum_t::top_left);
g.setTextColor(TFT_WHITE, TFT_BLACK);
g.setTextSize(2);
g.setCursor(6, 4);
g.print("StickMic");
if (recording) g.fillCircle(W - 16, 14, 8, TFT_RED);
// Level meter
int lvl = (int)((int32_t)g_level * (W - 12) / 32767);
g.drawRect(6, 36, W - 12, 18, TFT_DARKGREY);
g.fillRect(8, 38, lvl > 0 ? lvl : 0, 14, recording ? TFT_GREEN : TFT_DARKGREEN);
// Link status
bool linked = (millis() - g_lastStatusMs) < 1500;
g.setTextSize(1);
g.setCursor(6, 62);
g.setTextColor(linked ? TFT_GREEN : TFT_DARKGREY, TFT_BLACK);
g.printf("Core2: %s", linked ? "linked" : "----");
if (linked) {
const char* st = g_deckState == 2 ? "PLAY" : g_deckState == 1 ? "REC" : "idle";
g.setTextColor(TFT_WHITE, TFT_BLACK);
g.setCursor(6, 76);
g.printf("deck:%s trk:%d", st, g_selTrack + 1);
}
// Battery
int batt = M5.Power.getBatteryLevel();
g.setTextColor(TFT_WHITE, TFT_BLACK);
g.setCursor(6, H - 14);
g.printf("bat %d%%", batt < 0 ? 0 : batt);
// Hint
g.setTextDatum(textdatum_t::bottom_right);
g.setTextColor(TFT_YELLOW, TFT_BLACK);
g.drawString("hold KEY1", W - 6, H - 4);
g.setTextDatum(textdatum_t::top_left);
g.pushSprite(0, 0);
}
// ---------------------------------------------------------------------------
void setup() {
auto cfg = M5.config();
cfg.internal_mic = true;
cfg.internal_spk = false; // we don't play on the stick
M5.begin(cfg);
M5.Display.setRotation(1);
M5.Display.setBrightness(110);
M5.Display.fillScreen(TFT_BLACK);
g_canvas.setColorDepth(16);
g_canvas.createSprite(M5.Display.width(), M5.Display.height());
linkBegin();
auto mcfg = M5.Mic.config();
mcfg.sample_rate = SAMPLE_RATE_HZ;
mcfg.noise_filter_level = 8;
M5.Mic.config(mcfg);
M5.Mic.begin();
}
void loop() {
M5.update();
const bool recording = M5.BtnA.isPressed(); // KEY1 push-to-talk
if (M5.BtnA.wasPressed()) { // new take -> reset ADPCM state + warm-up
g_pred = 0;
g_index = 0;
g_warmup = 6; // ~120 ms mic settling
}
if (recording && M5.Mic.isEnabled()) {
captureAndSend();
} else {
g_level = 0;
static uint32_t lastHb = 0;
if (millis() - lastHb > 500) { sendHeartbeat(); lastHb = millis(); }
M5.delay(2);
}
drawUi(recording);
}