-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathPDMSerialPlotter_RAK18003.ino
165 lines (142 loc) · 5.23 KB
/
PDMSerialPlotter_RAK18003.ino
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
/**
@file PDMSerialPlotter_RAK18003.ino
@author rakwireless.com
@brief This example reads audio data from the PDM microphones, and prints out the samples to the Serial console.
The Serial Plotter built into the Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
@note This example need use the RAK18003 module.If using stereo, you need to use RAK18033 module and two PDM microphones.
@version 0.1
@date 2022-06-6
@copyright Copyright (c) 2022
*/
#include "audio.h" // Click here to install the library: http://librarymanager/All#RAKwireless-Audio
TPT29555 Expander1(0x23);
TPT29555 Expander2(0x25);
int channels = 2; //1 mono 2 stereo
// default PCM output frequency
static const int frequency = 16000;
// buffer to read samples into, each sample is 16-bits
short sampleBuffer[BUFFER_SIZE];
void RAK18003Init(void);
void setup() {
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
delay(500);
pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_GREEN, HIGH);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 3000)
{
delay(100);
}
else
{
break;
}
}
RAK18003Init();
// initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate
// default PCM output frequency
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1) yield();
}
Serial.println("=====================================");
Serial.println("PDM test");
Serial.println("=====================================");
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, HIGH);
}
void loop() {
// Read data from microphone
int sampleRead = PDM.read(sampleBuffer, sizeof(sampleBuffer));
sampleRead = sampleRead / 2; //Each short data contains two Bytes
// wait for samples to be read
if (sampleRead > 0) {
// print samples to the serial monitor or plotter
for (int i = 0; i < sampleRead; i++) {
if (channels == 2)
{
Serial.print("L:");
Serial.print(sampleBuffer[i]);
Serial.print(" R:");
i++;
}
Serial.println(sampleBuffer[i]);
}
sampleRead = 0;
}
}
void RAK18003Init(void)
{
while (!Expander1.begin())
{
Serial.println("Did not find RAK18003 IO Expander Chip1, please check !");
delay(200);
}
while (!Expander2.begin())
{
Serial.println("Did not find RAK18003 IO Expander Chip2, please check !");
delay(200);
}
Expander1.pinMode(0, INPUT); //SD check
Expander1.pinMode(1, INPUT); //MIC check
Expander1.pinMode(2, INPUT); //MIC CTR1
Expander1.pinMode(3, INPUT); //MIC CTR2
Expander1.pinMode(4, INPUT); //AMP check
Expander1.pinMode(5, INPUT); //AMP CTR1
Expander1.pinMode(6, INPUT); //AMP CTR2
Expander1.pinMode(7, INPUT); //AMP CTR3
Expander1.pinMode(8, INPUT); //DSP check
Expander1.pinMode(9, INPUT); //DSP CTR1 DSP int
Expander1.pinMode(10, INPUT); //DSP CTR2 DSP ready
Expander1.pinMode(11, OUTPUT); //DSP CTR3 DSP reset
Expander1.pinMode(12, INPUT); //DSP CTR4 not use
Expander1.pinMode(13, INPUT); //DSP CTR5 not use
Expander1.pinMode(14, INPUT); //NOT USE
Expander1.pinMode(15, INPUT); //NOT USE
// Expander1.digitalWrite(14, 0); //set chip 1 not use pin output low
// Expander1.digitalWrite(15, 0); //set chip 1 not use pin output low
Expander2.pinMode(0, OUTPUT); //CORE SPI CS1 for DSPG CS
Expander2.pinMode(1, OUTPUT); //CORE SPI CS2
Expander2.pinMode(2, OUTPUT); //CORE SPI CS3
Expander2.pinMode(3, OUTPUT); //PDM switch CTR 1 to dsp 0 to core
Expander2.pinMode(4, INPUT); //not use
Expander2.pinMode(5, INPUT); //not use
Expander2.pinMode(6, INPUT); //not use
Expander2.pinMode(7, INPUT); //not use
Expander2.pinMode(8, INPUT); //not use
Expander2.pinMode(9, INPUT); //not use
Expander2.pinMode(10, INPUT); //not use
Expander2.pinMode(11, INPUT); //not use
Expander2.pinMode(12, INPUT); //not use
Expander2.pinMode(13, INPUT); //not use
Expander2.pinMode(14, INPUT); //not use
Expander2.pinMode(15, INPUT); //not use
Expander2.digitalWrite(0, 1); //set SPI CS1 High
Expander2.digitalWrite(1, 1); //set SPI CS2 High
Expander2.digitalWrite(2, 1); //set SPI CS3 High
Expander2.digitalWrite(3, 0); //set the PDM data direction from MIC to WisCore
// if(Expander1.digitalRead(0) == 1) //Check SD card
// {
// Serial.println("There is no SD card on the RAK18003 board, please check !");
// }
while (Expander1.digitalRead(1) == 0) //Check if the microphone board is connected on the RAK18003
{
Serial.println("There is no microphone board, please check !");
delay(500);
}
// if(Expander1.digitalRead(4) == 0) //Check if the RAK18060 AMP board is connected on the RAK18003
// {
// Serial.println("There is no RAK18060 AMP board, please check !");
// }
// if(Expander1.digitalRead(8) == 0) //Check if the RAK18080 DSPG board is connected on the RAK18003
// {
// Serial.println("There is no RAK18080 DSPG board, please check !");
// }
}