Skip to content

Commit 1971e6a

Browse files
committed
make i2s speaker test interuptible
1 parent 90783fa commit 1971e6a

1 file changed

Lines changed: 92 additions & 63 deletions

File tree

src/Controllers/I2sController.cpp

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -148,87 +148,113 @@ Test Speaker
148148
*/
149149
void I2sController::handleTestSpeaker() {
150150
switchOutputInput(true);
151-
terminalView.println("I2S Speaker Test: Running full tests...\n");
151+
terminalView.println("I2S Speaker Test: Running ... Press [ENTER] to stop\n");
152+
153+
bool stop = false;
154+
155+
// Helper to wait with check for stop on enter
156+
auto waitOrStop = [&](uint32_t ms) {
157+
for (uint32_t i = 0; i < ms; ++i) {
158+
char ch = terminalInput.readChar();
159+
if (ch == '\n' || ch == '\r') {
160+
stop = true;
161+
return;
162+
}
163+
delay(1);
164+
}
165+
};
166+
167+
auto rate = state.getI2sSampleRate();
152168

153-
// Show pin config
154169
terminalView.println("Using pins:");
155170
terminalView.println(" BCLK : " + std::to_string(state.getI2sBclkPin()));
156171
terminalView.println(" LRCK : " + std::to_string(state.getI2sLrckPin()));
157172
terminalView.println(" DATA : " + std::to_string(state.getI2sDataPin()));
158173
terminalView.println("");
159174

160-
auto rate = state.getI2sSampleRate();
161-
162175
// Melody
163176
terminalView.println(" Melody...");
164-
const uint16_t melody[] = {262, 294, 330, 349, 392, 440, 494, 523}; // C major
177+
const uint16_t melody[] = {262, 294, 330, 349, 392, 440, 494, 523};
165178
for (uint16_t f : melody) {
179+
if (stop) break;
166180
i2sService.playTone(rate, f, 200);
167-
delay(50);
181+
waitOrStop(50);
168182
}
169-
delay(1000);
170183

171-
// Frequency Sweep
172-
terminalView.println(" Frequency Sweep...");
173-
for (uint16_t f = 100; f <= 3000; f += 300) {
174-
i2sService.playTone(rate, f, 100);
184+
if (!stop) {
185+
waitOrStop(1000);
175186
}
176-
delay(800);
177187

178-
// Low Freq
179-
terminalView.println(" Low Frequency...");
180-
for (uint16_t f : {50, 100, 150, 200, 250, 300, 350, 400, 450, 500}) {
181-
i2sService.playTone(rate, f, 400);
182-
delay(100);
188+
// Sweep
189+
if (!stop) {
190+
terminalView.println(" Frequency Sweep...");
191+
for (uint16_t f = 100; f <= 3000 && !stop; f += 300) {
192+
i2sService.playTone(rate, f, 100);
193+
}
194+
waitOrStop(800);
183195
}
184-
delay(800);
185196

186-
// High Freq
187-
terminalView.println(" High Frequency...");
188-
for (uint16_t f = 10000; f <= 16000; f += 1000) {
189-
i2sService.playTone(rate, f, 300);
190-
delay(100);
197+
// Low
198+
if (!stop) {
199+
terminalView.println(" Low Frequency...");
200+
for (uint16_t f : {50,100,150,200,250,300,350,400,450,500}) {
201+
if (stop) break;
202+
i2sService.playTone(rate, f, 400);
203+
waitOrStop(100);
204+
}
205+
waitOrStop(800);
191206
}
192-
delay(800);
193-
194-
// Beep Pattern
195-
terminalView.println(" Beep Pattern (short/long)...");
196-
i2sService.playTone(rate, 800, 100);
197-
delay(100);
198-
i2sService.playTone(rate, 800, 100);
199-
delay(100);
200-
i2sService.playTone(rate, 800, 100);
201-
delay(100);
202-
i2sService.playTone(rate, 800, 400);
203-
delay(100);
204-
i2sService.playTone(rate, 800, 400);
205-
delay(100);
206-
i2sService.playTone(rate, 800, 400);
207-
delay(800);
208-
209-
// Binary pattern (square wave)
210-
terminalView.println(" Binary tone pattern...");
211-
for (int i = 0; i < 15; ++i) {
212-
i2sService.playTone(rate, 1000, 50);
213-
delay(50);
207+
208+
// High
209+
if (!stop) {
210+
terminalView.println(" High Frequency...");
211+
for (uint16_t f = 10000; f <= 16000 && !stop; f += 1000) {
212+
i2sService.playTone(rate, f, 300);
213+
waitOrStop(100);
214+
}
215+
waitOrStop(800);
214216
}
215-
delay(800);
216217

217-
// Config for PCM playback
218-
i2sService.configureOutput(
219-
state.getI2sBclkPin(),
220-
state.getI2sLrckPin(),
221-
state.getI2sDataPin(),
222-
24000,
223-
16,
224-
state.getI2sPercentLevel()
225-
);
226-
227-
// PCM Playback test
228-
terminalView.println(" PCM playback...");
229-
i2sService.playPcm(PcmSoundtestComplete, sizeof(PcmSoundtestComplete));
230-
231-
// Restaure config
218+
// Beep
219+
if (!stop) {
220+
terminalView.println(" Beep Pattern...");
221+
for (int i = 0; i < 3 && !stop; ++i) {
222+
i2sService.playTone(rate, 800, 100);
223+
waitOrStop(100);
224+
}
225+
for (int i = 0; i < 3 && !stop; ++i) {
226+
i2sService.playTone(rate, 800, 400);
227+
waitOrStop(100);
228+
}
229+
waitOrStop(800);
230+
}
231+
232+
// Binary
233+
if (!stop) {
234+
terminalView.println(" Binary tone pattern...");
235+
for (int i = 0; i < 15 && !stop; ++i) {
236+
i2sService.playTone(rate, 1000, 50);
237+
waitOrStop(50);
238+
}
239+
waitOrStop(800);
240+
}
241+
242+
// PCM
243+
if (!stop) {
244+
i2sService.configureOutput(
245+
state.getI2sBclkPin(),
246+
state.getI2sLrckPin(),
247+
state.getI2sDataPin(),
248+
24000,
249+
16,
250+
state.getI2sPercentLevel()
251+
);
252+
253+
terminalView.println(" PCM playback...");
254+
i2sService.playPcm(PcmSoundtestComplete, sizeof(PcmSoundtestComplete));
255+
}
256+
257+
// Restore config
232258
i2sService.configureOutput(
233259
state.getI2sBclkPin(),
234260
state.getI2sLrckPin(),
@@ -237,8 +263,11 @@ void I2sController::handleTestSpeaker() {
237263
state.getI2sBitsPerSample(),
238264
state.getI2sPercentLevel()
239265
);
240-
241-
terminalView.println("\nI2S Speaker Test: Done.");
266+
267+
if (stop)
268+
terminalView.println("\nI2S Speaker Test: Stopped by user.\n");
269+
else
270+
terminalView.println("\nI2S Speaker Test: Done.");
242271
}
243272

244273
/*

0 commit comments

Comments
 (0)