Skip to content

Commit 50a7586

Browse files
author
Damien Laidin
authored
Merge pull request #13 from chirp/feature/pr
Feature/pr
2 parents 8f043cb + a340de5 commit 50a7586

File tree

8 files changed

+151
-129
lines changed

8 files changed

+151
-129
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Recent changes to the [Chirp Arduino SDK](https://developers.chirp.io/docs).
44

55
## v3.3.0 (09/08/2019)
6+
67
- Added support for cortex-m4 (Nano 33 Sense)
78
- Added send-only support for cortex-m0plus (MKRZero, MKR Vidor 4000)
89
- Build v3.3.0-rc1

README.md

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
Chirp is a library enabling Arduino-based devices to send and receive data using sound. You'll need:
88

99
* A compatible Arduino board
10-
* A digital I2S MEMS microphone
10+
* A digital I2S MEMS microphone (if your board does not contain a microphone)
1111
* A digital I2S amplifier and compatible speaker
1212

13-
For sound input you will need a digital MEMS microphone such as the SPH0645 or ICS-43434. (Not necessary for the Nano 33 Sense as it comes with an on board microphone)
14-
For sound output it is recommended to use a digital I2S output such as the UDA1334A or MAX98357A connected to a compatible speaker.
13+
For receiving data, you will need a digital MEMS microphone. Some boards (for example, the Nano 33 Sense and Microsoft MXChip) already include a MEMS mic so you are good to go. For others, you will need an external mic such as the [SPH0645](https://www.adafruit.com/product/3421) or [ICS-43434](https://www.mouser.co.uk/ProductDetail/TDK-InvenSense/ICS-43434?qs=u4fy%2FsgLU9PAgmWRI7%252BqXA%3D%3D).
1514

16-
You can quickly test the sound input by playing random chirps from the [Developer Hub](https://developers.chirp.io).
17-
The easiest way to test the sound output would be to use Chirp on the [command line](https://developers.chirp.io/docs/tutorials/command-line) to receive data from the Arduino.
15+
For sending data, we recommend using a digital I2S audio output such as the [UDA1334A](https://www.adafruit.com/product/3678) or [MAX98357A](https://www.adafruit.com/product/3006), connected to a compatible speaker.
16+
17+
You can quickly test that your device is receiving chirps by playing some random test signals from the [Developer Hub](https://developers.chirp.io).
18+
19+
To test whether your device is sending chirps OK, we recommend setting up the [Python command-line tools](https://developers.chirp.io/docs/tutorials/command-line) to receive data from the Arduino.
1820

1921
## Supported hardware
2022

21-
Send and receive capabilities
23+
The following Arduino-compatible boards are able to both send and receive chirps:
2224

2325
* Arduino Nano 33 Sense
2426
* Microsoft MXChip
2527
* ESP32
2628

27-
Send only
29+
The following Arduino-compatible boards are only able to send chirps, as they are not able to do on-chip DSP:
2830

2931
* Arduino MKRZero
3032
* Arduino Vidor 4000
@@ -36,17 +38,21 @@ Send only
3638

3739
Chirp is written for the Arduino IDE versions 1.8.6 and above.
3840

39-
Install ChirpSDK as a library. For instructions, see
41+
Install ChirpSDK as a library using "Manage Libraries". For instructions, see
4042

4143
[http://arduino.cc/en/Guide/Libraries](http://arduino.cc/en/Guide/Libraries)
4244

43-
Once installed, you can access the example programs from the menu :
45+
Once installed, you can access the example programs from the menu:
4446

45-
```File > Examples > ChirpSDK > Example ```
47+
```
48+
File > Examples > ChirpSDK > Example
49+
```
4650

47-
and you can include the headers to use Chirp in your own code by adding :
51+
and you can include the headers to use Chirp in your own code by adding:
4852

49-
```#include "chirp_connect.h"```
53+
```
54+
#include "chirp_connect.h"
55+
```
5056

5157
## Usage
5258

examples/ESP32Receive/ESP32Receive.ino

+48-42
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 ------------------------------------------------------------
@@ -56,8 +58,7 @@ void setupAudioInput(int sample_rate);
5658

5759
// Function declarations -------------------------------------------------------
5860

59-
void
60-
setup()
61+
void setup()
6162
{
6263
pinMode(LED_PIN, OUTPUT);
6364
digitalWrite(LED_PIN, LOW);
@@ -68,35 +69,34 @@ setup()
6869
xTaskCreate(initTask, "initTask", 16384, NULL, 1, NULL);
6970
}
7071

71-
void
72-
loop()
72+
void loop()
7373
{
7474
esp_err_t audioError;
7575
chirp_connect_error_code_t chirpError;
7676

77-
if (startTasks) {
77+
if (startTasks)
78+
{
7879
xTaskCreate(processInputTask, "processInputTask", 16384, NULL, 5, NULL);
7980
startTasks = false;
8081
}
8182
}
8283

8384
// RTOS Tasks ------------------------------------------------------------------
8485

85-
void
86-
initTask(void *parameter)
86+
void initTask(void *parameter)
8787
{
8888
setupChirp();
8989

90-
uint32_t input_sample_rate = chirp_connect_set_input_sample_rate(chirp, SAMPLE_RATE);
90+
chirp_connect_error_code_t chirpError = chirp_connect_set_input_sample_rate(chirp, SAMPLE_RATE);
91+
chirpErrorHandler(chirpError);
9192
setupAudioInput(SAMPLE_RATE);
9293

9394
Serial.printf("Heap size: %u\n", ESP.getFreeHeap());
9495
startTasks = true;
9596
vTaskDelete(NULL);
9697
}
9798

98-
void
99-
processInputTask(void *parameter)
99+
void processInputTask(void *parameter)
100100
{
101101
esp_err_t audioError;
102102
chirp_connect_error_code_t chirpError;
@@ -105,11 +105,14 @@ processInputTask(void *parameter)
105105
float buffer[BUFFER_SIZE] = {0};
106106
int32_t ibuffer[BUFFER_SIZE] = {0};
107107

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

115118
chirpError = chirp_connect_process_input(chirp, buffer, bytesLength / 4);
@@ -121,39 +124,39 @@ processInputTask(void *parameter)
121124

122125
// Chirp -----------------------------------------------------------------------
123126

124-
void
125-
onStateChangedCallback(void *chirp, chirp_connect_state_t previous, chirp_connect_state_t current)
127+
void onStateChangedCallback(void *chirp, chirp_connect_state_t previous, chirp_connect_state_t current)
126128
{
127129
currentState = current;
128130
Serial.printf("State changed from %d to %d\n", previous, current);
129131
}
130132

131-
void
132-
onReceivingCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel)
133+
void onReceivingCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel)
133134
{
134135
Serial.println("Receiving data...");
135136
digitalWrite(LED_PIN, HIGH);
136137
}
137138

138-
void
139-
onReceivedCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel)
139+
void onReceivedCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel)
140140
{
141-
if (payload) {
141+
if (payload)
142+
{
142143
char *data = (char *)calloc(length + 1, sizeof(uint8_t));
143144
memcpy(data, payload, length * sizeof(uint8_t));
144145
Serial.print("Received data: ");
145146
Serial.println(data);
146147
free(data);
147-
} else {
148+
}
149+
else
150+
{
148151
Serial.println("Decode failed.");
149152
}
150153
}
151154

152-
void
153-
setupChirp()
155+
void setupChirp()
154156
{
155157
chirp = new_chirp_connect(CHIRP_APP_KEY, CHIRP_APP_SECRET);
156-
if (chirp == NULL) {
158+
if (chirp == NULL)
159+
{
157160
Serial.println("Chirp initialisation failed.");
158161
return;
159162
}
@@ -178,8 +181,7 @@ setupChirp()
178181
Serial.println("Chirp Connect initialised.");
179182
}
180183

181-
void
182-
chirpErrorHandler(chirp_connect_error_code_t code)
184+
void chirpErrorHandler(chirp_connect_error_code_t code)
183185
{
184186
if (code != CHIRP_CONNECT_OK)
185187
{
@@ -191,16 +193,16 @@ chirpErrorHandler(chirp_connect_error_code_t code)
191193

192194
// I2S Audio -------------------------------------------------------------------
193195

194-
void
195-
setupAudioInput(int sample_rate)
196+
void setupAudioInput(int sample_rate)
196197
{
197198
/*
198199
Set up I2S audio for SPH0645 microphone
199200
*/
200201
esp_err_t err;
201202
Serial.println("Initialising audio input driver..");
202203

203-
const i2s_config_t i2s_config = {
204+
const i2s_config_t i2s_config =
205+
{
204206
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
205207
.sample_rate = sample_rate,
206208
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
@@ -212,27 +214,31 @@ setupAudioInput(int sample_rate)
212214
.use_apll = true
213215
};
214216

215-
const i2s_pin_config_t pin_config = {
217+
const i2s_pin_config_t pin_config =
218+
{
216219
.bck_io_num = I2SI_BCK,
217220
.ws_io_num = I2SI_LRCL,
218221
.data_out_num = I2S_PIN_NO_CHANGE,
219222
.data_in_num = I2SI_DATA
220223
};
221224

222225
err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
223-
if (err != ESP_OK) {
226+
if (err != ESP_OK)
227+
{
224228
Serial.printf("Failed installing driver: %d\n", err);
225229
while (true);
226230
}
227231

228232
err = i2s_set_pin(I2S_NUM_0, &pin_config);
229-
if (err != ESP_OK) {
233+
if (err != ESP_OK)
234+
{
230235
Serial.printf("Failed setting pin: %d\n", err);
231236
while (true);
232237
}
233238

234239
err = i2s_set_sample_rates(I2S_NUM_0, sample_rate);
235-
if (err != ESP_OK) {
240+
if (err != ESP_OK)
241+
{
236242
Serial.printf("Failed to set sample rates: %d\n", err);
237243
while (true);
238244
}

0 commit comments

Comments
 (0)