-
Notifications
You must be signed in to change notification settings - Fork 117
/
Audio_Alarm_RAK18003.ino
182 lines (164 loc) · 5.62 KB
/
Audio_Alarm_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
@file Audio_Alarm_RAK18003.ino
@author rakwireless.com
@brief The microphone detects the noise threshold and when the ambient noise is greater than the set threshold,
a warning will be generated. And the LED of WisBase will lights.
@note This example need use the RAK18003 module.
@version 0.1
@date 2022-06-10
@copyright Copyright (c) 2022
*/
#include "audio.h" // Click here to install the library: http://librarymanager/All#RAKwireless-Audio
#include <Arduino.h>
TPT29555 Expander1(0x23);
TPT29555 Expander2(0x25);
Channel_Mode channels = left; // stereo or left or right
int frequency = 16000;
// buffer to read samples into, each sample is 16-bits
short sampleBuffer[BUFFER_SIZE];
volatile uint8_t read_flag = 0;
//Alarm threshold
int audio_threshold = 1000; //The threshold value of the noise trigger can be modified according to the environmental conditions
int g_alarm = 0;
int abs_int(short data);
void onPDMdata();
void RAK18003Init(void);
void setup()
{
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
delay(500);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 3000)
{
delay(100);
}
else
{
break;
}
}
RAK18003Init();
// configure the data receive callback
PDM.onReceive(onPDMdata);
// initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1) yield();
}
delay(500);
Serial.println("=====================================");
}
void loop()
{
// wait for samples to be read
if (read_flag == 1) {
read_flag = 0;
uint32_t sum = 0;
// print samples to the serial monitor or plotter
for (int i = 0; i < BUFFER_SIZE; i++) {
sum = sum + abs(sampleBuffer[i]);
}
int aver = sum / BUFFER_SIZE;
if (aver > audio_threshold)
{
/*You can add your alarm processing tasks here*/
g_alarm++;
Serial.printf("Alarm %d\r\n", g_alarm);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_GREEN, HIGH);
delay(2000);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
}
}
}
int abs_int(short data)
{
if (data > 0) return data;
else return (0 - data);
}
void onPDMdata() {
// query the number of bytes available
// read into the sample buffer
PDM.read((uint8_t *)sampleBuffer, BUFFER_SIZE * 2);
read_flag = 1;
}
void RAK18003Init(void)
{
while (!Expander1.begin())
{
Serial.println("Did not find RAK18003 IO Expander Chip1,please check !");
delay(500);
}
while (!Expander2.begin())
{
Serial.println("Did not find RAK18003 IO Expander Chip2,please check !");
delay(500);
}
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 !");
// }
}