Skip to content

Commit 9c08a7a

Browse files
committed
Example tidyups
1 parent 8688b41 commit 9c08a7a

File tree

6 files changed

+114
-75
lines changed

6 files changed

+114
-75
lines changed

examples/ESP32Receive/ESP32Receive.ino

+38-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**-----------------------------------------------------------------------------
22
3-
Example code to receive data using ESP32 and SPH0645 microphone
3+
Example code using the Chirp SDK to receive data using ESP32 and
4+
SPH0645 microphone
45
56
@file ESP32Receive.ino
67
@@ -27,19 +28,20 @@
2728
#define I2SI_BCK 14 // I2S BCLK on GPIO14
2829
#define I2SI_LRCL 15 // I2S SELECT on GPIO15
2930

30-
#define LED_PIN 2 // LED
31-
#define SWITCH_PIN 0 // Switch
31+
#define LED_PIN 2 // Pin number for on-board LED
32+
#define SWITCH_PIN 0 // Pin number for on-board switch
3233

33-
#define BUFFER_SIZE 512
34-
#define MIC_CALIBRATION 13125
35-
#define SAMPLE_RATE 16000
34+
#define BUFFER_SIZE 512 // Audio buffer size
35+
#define SAMPLE_RATE 16000 // Audio sample rate
3636

3737
/**
3838
Convert I2S input data.
3939
Data is 18 bit signed, MSBit first, two's complement.
40-
The calibration value is determined using the Serial
41-
Plotter to centre the audio about zero.
40+
The MIC_CALIBRATION value is determined using the Serial
41+
Plotter to centre the audio about zero. The below value
42+
should be correct for most ESP32 boards.
4243
*/
44+
#define MIC_CALIBRATION 13125
4345
#define CONVERT_INPUT(sample) (((int32_t)(sample) >> 14) + MIC_CALIBRATION)
4446

4547
// Global variables ------------------------------------------------------------
@@ -72,7 +74,8 @@ loop()
7274
esp_err_t audioError;
7375
chirp_connect_error_code_t chirpError;
7476

75-
if (startTasks) {
77+
if (startTasks)
78+
{
7679
xTaskCreate(processInputTask, "processInputTask", 16384, NULL, 5, NULL);
7780
startTasks = false;
7881
}
@@ -85,7 +88,8 @@ initTask(void *parameter)
8588
{
8689
setupChirp();
8790

88-
uint32_t input_sample_rate = chirp_connect_set_input_sample_rate(chirp, SAMPLE_RATE);
91+
chirp_connect_error_code_t chirpError = chirp_connect_set_input_sample_rate(chirp, SAMPLE_RATE);
92+
chirpErrorHandler(chirpError);
8993
setupAudioInput(SAMPLE_RATE);
9094

9195
Serial.printf("Heap size: %u\n", ESP.getFreeHeap());
@@ -103,11 +107,14 @@ processInputTask(void *parameter)
103107
float buffer[BUFFER_SIZE] = {0};
104108
int32_t ibuffer[BUFFER_SIZE] = {0};
105109

106-
while (currentState >= CHIRP_CONNECT_STATE_RUNNING) {
110+
while (currentState >= CHIRP_CONNECT_STATE_RUNNING)
111+
{
107112
audioError = i2s_read(I2S_NUM_0, ibuffer, BUFFER_SIZE * 4, &bytesLength, portMAX_DELAY);
108-
if (bytesLength) {
109-
for (int i = 0; i < bytesLength / 4; i++) {
110-
buffer[i] = (float)CONVERT_INPUT(ibuffer[i]);
113+
if (bytesLength)
114+
{
115+
for (int i = 0; i < bytesLength / 4; i++)
116+
{
117+
buffer[i] = (float) CONVERT_INPUT(ibuffer[i]);
111118
}
112119

113120
chirpError = chirp_connect_process_input(chirp, buffer, bytesLength / 4);
@@ -136,12 +143,15 @@ onReceivingCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channe
136143
void
137144
onReceivedCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel)
138145
{
139-
if (payload) {
146+
if (payload)
147+
{
140148
char *data = chirp_connect_as_string((chirp_connect_t *)chirp, payload, length);
141149
Serial.printf("data = %s\n", data);
142150
digitalWrite(LED_PIN, LOW);
143151
chirp_connect_free(data);
144-
} else {
152+
}
153+
else
154+
{
145155
Serial.println("Decode failed.");
146156
}
147157
}
@@ -150,7 +160,8 @@ void
150160
setupChirp()
151161
{
152162
chirp = new_chirp_connect(CHIRP_APP_KEY, CHIRP_APP_SECRET);
153-
if (chirp == NULL) {
163+
if (chirp == NULL)
164+
{
154165
Serial.println("Chirp initialisation failed.");
155166
return;
156167
}
@@ -197,7 +208,8 @@ setupAudioInput(int sample_rate)
197208
esp_err_t err;
198209
Serial.println("Initialising audio input driver..");
199210

200-
const i2s_config_t i2s_config = {
211+
const i2s_config_t i2s_config =
212+
{
201213
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
202214
.sample_rate = sample_rate,
203215
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
@@ -209,27 +221,31 @@ setupAudioInput(int sample_rate)
209221
.use_apll = true
210222
};
211223

212-
const i2s_pin_config_t pin_config = {
224+
const i2s_pin_config_t pin_config =
225+
{
213226
.bck_io_num = I2SI_BCK,
214227
.ws_io_num = I2SI_LRCL,
215228
.data_out_num = I2S_PIN_NO_CHANGE,
216229
.data_in_num = I2SI_DATA
217230
};
218231

219232
err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
220-
if (err != ESP_OK) {
233+
if (err != ESP_OK)
234+
{
221235
Serial.printf("Failed installing driver: %d\n", err);
222236
while (true);
223237
}
224238

225239
err = i2s_set_pin(I2S_NUM_0, &pin_config);
226-
if (err != ESP_OK) {
240+
if (err != ESP_OK)
241+
{
227242
Serial.printf("Failed setting pin: %d\n", err);
228243
while (true);
229244
}
230245

231246
err = i2s_set_sample_rates(I2S_NUM_0, sample_rate);
232-
if (err != ESP_OK) {
247+
if (err != ESP_OK)
248+
{
233249
Serial.printf("Failed to set sample rates: %d\n", err);
234250
while (true);
235251
}

examples/ESP32Send/ESP32Send.ino

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**-----------------------------------------------------------------------------
22
3-
Example code to send data using ESP32 and UDA1334 audio output
3+
Example code using the Chirpd SDK to send data using ESP32 and UDA1334
4+
audio output
45
56
@file ESP32Send.ino
67
@@ -27,11 +28,11 @@
2728
#define I2SO_BCK 18 // I2S BCLK on GPIO18
2829
#define I2SO_WSEL 5 // I2S SELECT on GPIO5
2930

30-
#define LED_PIN 2 // LED
31-
#define SWITCH_PIN 0 // Switch
31+
#define LED_PIN 2 // Pin number for on-board LED
32+
#define SWITCH_PIN 0 // Pin number for on-board switch
3233

33-
#define BUFFER_SIZE 512
34-
#define SAMPLE_RATE 16000
34+
#define BUFFER_SIZE 512 // Audio buffer size
35+
#define SAMPLE_RATE 16000 // Audio sample rate
3536

3637
// Global variables ------------------------------------------------------------
3738
static chirp_connect_t *chirp = NULL;
@@ -67,12 +68,14 @@ loop()
6768
esp_err_t audioError;
6869
chirp_connect_error_code_t chirpError;
6970

70-
if (startTasks) {
71+
if (startTasks)
72+
{
7173
xTaskCreate(processOutputTask, "processOutputTask", 16384, NULL, 3, NULL);
7274
startTasks = false;
7375
}
7476

75-
if (buttonPressed) {
77+
if (buttonPressed)
78+
{
7679
size_t payloadLength = 0;
7780
uint8_t *payload = chirp_connect_random_payload(chirp, &payloadLength);
7881
chirp_connect_send(chirp, payload, payloadLength);
@@ -113,12 +116,14 @@ processOutputTask(void *parameter)
113116
short buffer[BUFFER_SIZE] = {0};
114117
int32_t ibuffer[BUFFER_SIZE] = {0};
115118

116-
while (currentState >= CHIRP_CONNECT_STATE_RUNNING) {
119+
while (currentState >= CHIRP_CONNECT_STATE_RUNNING)
120+
{
117121
chirpError = chirp_connect_process_shorts_output(chirp, buffer, BUFFER_SIZE);
118122
chirpErrorHandler(chirpError);
119123

120-
for (int i = 0; i < BUFFER_SIZE; i++) {
121-
ibuffer[i] = (int32_t)buffer[i];
124+
for (int i = 0; i < BUFFER_SIZE; i++)
125+
{
126+
ibuffer[i] = (int32_t) buffer[i];
122127
}
123128
audioError = i2s_write(I2S_NUM_1, ibuffer, BUFFER_SIZE * 4, &bytesLength, portMAX_DELAY);
124129
}
@@ -154,7 +159,8 @@ void
154159
setupChirp()
155160
{
156161
chirp = new_chirp_connect(CHIRP_APP_KEY, CHIRP_APP_SECRET);
157-
if (chirp == NULL) {
162+
if (chirp == NULL)
163+
{
158164
Serial.println("Chirp initialisation failed.");
159165
return;
160166
}
@@ -204,7 +210,8 @@ setupAudioOutput(int sample_rate)
204210
esp_err_t err;
205211
Serial.println("Initialising audio output driver..");
206212

207-
const i2s_config_t i2s_config = {
213+
const i2s_config_t i2s_config =
214+
{
208215
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_TX),
209216
.sample_rate = sample_rate,
210217
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
@@ -216,27 +223,31 @@ setupAudioOutput(int sample_rate)
216223
.use_apll = true
217224
};
218225

219-
const i2s_pin_config_t pin_config = {
226+
const i2s_pin_config_t pin_config =
227+
{
220228
.bck_io_num = I2SO_BCK,
221229
.ws_io_num = I2SO_WSEL,
222230
.data_out_num = I2SO_DATA,
223231
.data_in_num = I2S_PIN_NO_CHANGE
224232
};
225233

226234
err = i2s_driver_install(I2S_NUM_1, &i2s_config, 0, NULL);
227-
if (err != ESP_OK) {
235+
if (err != ESP_OK)
236+
{
228237
Serial.printf("Failed installing driver: %d\n", err);
229238
while (true);
230239
}
231240

232241
err = i2s_set_pin(I2S_NUM_1, &pin_config);
233-
if (err != ESP_OK) {
242+
if (err != ESP_OK)
243+
{
234244
Serial.printf("Failed setting pin: %d\n", err);
235245
while (true);
236246
}
237247

238248
err = i2s_set_sample_rates(I2S_NUM_1, sample_rate);
239-
if (err != ESP_OK) {
249+
if (err != ESP_OK)
250+
{
240251
Serial.printf("Failed to set sample rates: %d\n", err);
241252
while (true);
242253
}

examples/MKRZeroSend/MKRZeroSend.ino

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**-----------------------------------------------------------------------------
22
3-
Example code using MKRZero and UDA1334 audio output
3+
Example code using the Chirp SDK to transmit data over sound, using the
4+
MKRZero and UDA1334 audio output.
45
56
@file MKRZeroSend.ino
67
@@ -15,8 +16,8 @@
1516
These buffers are read directly by the I2S peripheral to output
1617
audio data.
1718
18-
*Note*: This board is send-only, so cannot be configured to receive
19-
data due to the minimal memory available on the board
19+
*Note*: This board is send-only as it does not have a floating-point unit
20+
which is required to receive data.
2021
2122
Circuit:
2223
- Arduino MKRZero
@@ -38,7 +39,7 @@
3839
#include "chirp_connect.h"
3940
#include "credentials.h"
4041

41-
#define VOLUME 12000
42+
#define VOLUME 0.1
4243

4344
#define NUM_BUFFERS 2
4445
#define BUFFER_SIZE 512
@@ -48,7 +49,8 @@
4849

4950
int buffer[NUM_BUFFERS][BUFFER_SIZE];
5051
short tmpBuffer[BUFFER_SIZE / 2];
51-
uint8_t next, current;
52+
uint8_t nextBufferIndex, currentBufferIndex;
53+
uint16_t volumeInt;
5254

5355
Adafruit_ZeroI2S i2s;
5456
Adafruit_ZeroDMA dma;
@@ -68,7 +70,8 @@ void setupChirp(void);
6870

6971
void setup()
7072
{
71-
current = 0;
73+
currentBufferIndex = 0;
74+
volumeInt = VOLUME * INT16_MAX;
7275

7376
Serial.begin(115200);
7477
while(!Serial); // Wait for Serial monitor before continuing
@@ -101,18 +104,19 @@ void setup()
101104

102105
void loop()
103106
{
104-
if (dma_complete) {
105-
next = (current + 1) % NUM_BUFFERS;
107+
if (dma_complete)
108+
{
109+
nextBufferIndex = (currentBufferIndex + 1) % NUM_BUFFERS;
106110

107111
// Process data in to the next mono buffer
108112
chirp_connect_error_code_t err = chirp_connect_process_shorts_output(chirp, tmpBuffer, BUFFER_SIZE / 2);
109113
chirpErrorHandler(err);
110114

111115
// Copy the data into a stereo buffer for audio output
112116
for (int i = 0; i < BUFFER_SIZE / 2; i++) {
113-
int value = tmpBuffer[i] * VOLUME;
114-
buffer[next][i * 2] = value;
115-
buffer[next][i * 2 + 1] = value;
117+
int value = tmpBuffer[i] * volumeInt;
118+
buffer[nextBufferIndex][i * 2] = value;
119+
buffer[nextBufferIndex][i * 2 + 1] = value;
116120
}
117121

118122
dma_complete = false;
@@ -178,15 +182,19 @@ void setupChirp(void)
178182

179183
void dmaCallback(Adafruit_ZeroDMA *dma)
180184
{
181-
current++;
182-
if (current >= NUM_BUFFERS) {
183-
current -= NUM_BUFFERS;
185+
currentBufferIndex++;
186+
if (currentBufferIndex >= NUM_BUFFERS)
187+
{
188+
currentBufferIndex -= NUM_BUFFERS;
184189
}
185-
dma->changeDescriptor(desc, buffer[current]);
190+
dma->changeDescriptor(desc, buffer[currentBufferIndex]);
186191
ZeroDMAstatus stat = dma->startJob();
187-
if (stat == DMA_STATUS_OK) {
192+
if (stat == DMA_STATUS_OK)
193+
{
188194
dma_complete = true;
189-
} else {
195+
}
196+
else
197+
{
190198
Serial.println("ERROR");
191199
}
192200
}
@@ -208,7 +216,7 @@ void setupDMA(void)
208216

209217
Serial.println("Setting up transfer");
210218
desc = dma.addDescriptor(
211-
buffer[current],
219+
buffer[currentBufferIndex],
212220
(void *)(&I2S->DATA[0].reg),
213221
BUFFER_SIZE,
214222
DMA_BEAT_SIZE_WORD,

examples/MXChipSendReceive/MXChipSendReceive.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**------------------------------------------------------------------------------
22
*
3-
* Simple example of the Chirp C SDK on the Microsoft MXChip IoT DevKit.
3+
* Simple example using the Chirp SDK on the Microsoft MXChip IoT DevKit.
44
*
55
* @file MXChipSendReceive.ino
66
*

0 commit comments

Comments
 (0)