Skip to content

Commit 9a033dc

Browse files
committed
PDM: fix example to use USB Audio/CDC
1 parent 9f1b153 commit 9a033dc

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed
Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
#include "PDM.h"
2+
#include "USBSerial.h"
3+
#include "USBAudio.h"
24

3-
//mbed::DigitalOut led((PinName)40);
5+
// After defining USE_USB_AUDIO the board will behave as an USB sound card
6+
// You will, however, lose the chance to retrigger the bootloader via CDC Serial.
7+
// To program a new sketch, double click the RESET button and wait for the serial port to be enumerated again.
8+
// If USE_USB_AUDIO is not defined the stream will be sent via CDC Serial
49

5-
// Temporary patch to get unbuffered writes
6-
mbed::UARTSerial serial(SERIAL1_TX, SERIAL1_RX, 1000000);
10+
#ifdef USE_USB_AUDIO
11+
USBAudio audio(true, 16000, 1, 16000, 1);
12+
#endif
713

8-
const int led = 40;
14+
const int led = 41;
915
int led_status = HIGH;
1016

11-
#if 0
12-
/*
13-
This snippet allows to redirect stdout/stderr on a Stream at your choice
14-
Attention: it must be in mbed namespace to override the weak core definition
15-
*/
16-
namespace mbed {
17-
FileHandle *mbed_override_console(int fd) {
18-
return &serial;
19-
}
20-
21-
FileHandle *mbed_target_override_console(int fd) {
22-
return &serial;
23-
}
24-
}
25-
#endif
26-
2717
uint8_t buffer[1024];
2818
volatile int idx = 0;
2919

@@ -43,7 +33,7 @@ void send(void* buf, size_t size) {
4333

4434
void setup() {
4535
// Start the PDM as MONO @ 16KHz : gain @20
46-
// At this frequency you have 15ms in the callcack to use the returned buffer
36+
// At this frequency you have 15ms in the callback to use the returned buffer
4737
PDM.begin(1, 16000, 20);
4838
// The IRQ can call a naked function or one with buffer and size
4939
PDM.onReceive(send);
@@ -54,7 +44,11 @@ void setup() {
5444

5545
void loop() {
5646
if (idx == 1) {
57-
serial.write(buffer, DEFAULT_PDM_BUFFER_SIZE);
47+
#ifdef USE_USB_AUDIO
48+
audio.write(buffer, DEFAULT_PDM_BUFFER_SIZE);
49+
#else
50+
SerialUSB.send(buffer, DEFAULT_PDM_BUFFER_SIZE);
51+
#endif
5852
idx = 0;
5953
}
6054
}

0 commit comments

Comments
 (0)