|
| 1 | +/**----------------------------------------------------------------------------- |
| 2 | +
|
| 3 | + Example code using ESP32 with SPH0645 microphone and UDA1334 audio output |
| 4 | +
|
| 5 | + @file esp32.ino |
| 6 | +
|
| 7 | + @brief Create a developer account at https://developers.chirp.io, |
| 8 | + and copy and paste your key, secret and config string for the "arduino" |
| 9 | + protocol into the credentials.h file. |
| 10 | +
|
| 11 | + This example will start listening for chirps and print to the terminal |
| 12 | + when anything is received. |
| 13 | +
|
| 14 | + If the EN switch is pressed on the board, a random chirp will be sent to |
| 15 | + the audio output. |
| 16 | +
|
| 17 | +
|
| 18 | + Copyright © 2011-2019, Asio Ltd. |
| 19 | + All rights reserved. |
| 20 | +
|
| 21 | + ----------------------------------------------------------------------------*/ |
| 22 | +#include <driver/i2s.h> |
| 23 | + |
| 24 | +#include "chirp_connect.h" |
| 25 | +#include "credentials.h" |
| 26 | + |
| 27 | +#define I2SI_DATA 12 // I2S DATA IN on GPIO32 |
| 28 | +#define I2SI_BCK 14 // I2S BCLK on GPIO14 |
| 29 | +#define I2SI_LRCL 15 // I2S SELECT on GPIO15 |
| 30 | + |
| 31 | +#define I2SO_DATA 23 // I2S DATA OUT on GPIO23 |
| 32 | +#define I2SO_BCK 18 // I2S BCLK on GPIO18 |
| 33 | +#define I2SO_WSEL 5 // I2S SELECT on GPIO5 |
| 34 | + |
| 35 | +#define LED_PIN 2 // LED |
| 36 | +#define SWITCH_PIN 0 // Switch |
| 37 | + |
| 38 | +#define BUFFER_SIZE 512 |
| 39 | +#define MIC_CALIBRATION 13125 |
| 40 | +#define SAMPLE_RATE 16000 |
| 41 | + |
| 42 | +/** |
| 43 | + Convert I2S input data. |
| 44 | + Data is 18 bit signed, MSBit first, two's complement. |
| 45 | + The calibration value is determined using the Serial |
| 46 | + Plotter to centre the audio about zero. |
| 47 | +*/ |
| 48 | +#define CONVERT_INPUT(sample) (((int32_t)(sample) >> 14) + MIC_CALIBRATION) |
| 49 | + |
| 50 | +// Global variables ------------------------------------------- |
| 51 | +static chirp_connect_t *connect = NULL; |
| 52 | +static chirp_connect_state_t currentState = CHIRP_CONNECT_STATE_NOT_CREATED; |
| 53 | +static volatile bool buttonPressed = false; |
| 54 | +static bool startTasks = false; |
| 55 | + |
| 56 | +// Function declarations -------------------------------------- |
| 57 | +void IRAM_ATTR handleInterrupt(); |
| 58 | +void setupChirp(); |
| 59 | +void chirpErrorHandler(chirp_connect_error_code_t code); |
| 60 | +void setupAudioInput(int sample_rate); |
| 61 | +void setupAudioOutput(int sample_rate); |
| 62 | + |
| 63 | +// Function definitions --------------------------------------- |
| 64 | +void |
| 65 | +onStateChangedCallback(void *connect, chirp_connect_state_t previous, chirp_connect_state_t current) |
| 66 | +{ |
| 67 | + currentState = current; |
| 68 | + Serial.printf("State changed from %d to %d\n", previous, current); |
| 69 | +} |
| 70 | + |
| 71 | +void |
| 72 | +onReceivingCallback(void *connect, uint8_t *payload, size_t length, uint8_t channel) |
| 73 | +{ |
| 74 | + Serial.println("Receiving data..."); |
| 75 | + digitalWrite(LED_PIN, HIGH); |
| 76 | +} |
| 77 | + |
| 78 | +void |
| 79 | +onReceivedCallback(void *connect, uint8_t *payload, size_t length, uint8_t channel) |
| 80 | +{ |
| 81 | + if (payload) { |
| 82 | + char *data = chirp_connect_as_string((chirp_connect_t *)connect, payload, length); |
| 83 | + Serial.printf("data = %s\n", data); |
| 84 | + digitalWrite(LED_PIN, LOW); |
| 85 | + chirp_connect_free(data); |
| 86 | + } else { |
| 87 | + Serial.println("Decode failed."); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Tasks ------------------------------------------------------- |
| 92 | + |
| 93 | +void |
| 94 | +initTask(void *parameter) |
| 95 | +{ |
| 96 | + setupChirp(); |
| 97 | + |
| 98 | + uint32_t output_sample_rate = chirp_connect_set_output_sample_rate(connect, SAMPLE_RATE); |
| 99 | + setupAudioOutput(SAMPLE_RATE); |
| 100 | + |
| 101 | + uint32_t input_sample_rate = chirp_connect_set_input_sample_rate(connect, SAMPLE_RATE); |
| 102 | + setupAudioInput(SAMPLE_RATE); |
| 103 | + |
| 104 | + Serial.printf("Heap size: %u\n", ESP.getFreeHeap()); |
| 105 | + startTasks = true; |
| 106 | + vTaskDelete(NULL); |
| 107 | +} |
| 108 | + |
| 109 | +void |
| 110 | +processInputTask(void *parameter) |
| 111 | +{ |
| 112 | + esp_err_t audioError; |
| 113 | + chirp_connect_error_code_t chirpError; |
| 114 | + |
| 115 | + size_t bytesLength = 0; |
| 116 | + float buffer[BUFFER_SIZE] = {0}; |
| 117 | + int32_t ibuffer[BUFFER_SIZE] = {0}; |
| 118 | + |
| 119 | + while (currentState >= CHIRP_CONNECT_STATE_RUNNING) { |
| 120 | + audioError = i2s_read(I2S_NUM_0, ibuffer, BUFFER_SIZE * 4, &bytesLength, portMAX_DELAY); |
| 121 | + if (bytesLength) { |
| 122 | + for (int i = 0; i < bytesLength / 4; i++) { |
| 123 | + buffer[i] = (float)CONVERT_INPUT(ibuffer[i]); |
| 124 | + } |
| 125 | + |
| 126 | + uint32_t t1 = millis(); |
| 127 | + chirpError = chirp_connect_process_input(connect, buffer, bytesLength / 4); |
| 128 | + if (chirpError != CHIRP_CONNECT_OK) { |
| 129 | + chirpErrorHandler(chirpError); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + vTaskDelete(NULL); |
| 134 | +} |
| 135 | + |
| 136 | +void |
| 137 | +processOutputTask(void *parameter) |
| 138 | +{ |
| 139 | + esp_err_t audioError; |
| 140 | + chirp_connect_error_code_t chirpError; |
| 141 | + |
| 142 | + size_t bytesLength = 0; |
| 143 | + short buffer[BUFFER_SIZE] = {0}; |
| 144 | + int32_t ibuffer[BUFFER_SIZE] = {0}; |
| 145 | + |
| 146 | + while (currentState >= CHIRP_CONNECT_STATE_RUNNING) { |
| 147 | + chirpError = chirp_connect_process_shorts_output(connect, buffer, BUFFER_SIZE); |
| 148 | + if (chirpError != CHIRP_CONNECT_OK) { |
| 149 | + chirpErrorHandler(chirpError); |
| 150 | + } |
| 151 | + for (int i = 0; i < BUFFER_SIZE; i++) { |
| 152 | + ibuffer[i] = (int32_t)buffer[i]; |
| 153 | + } |
| 154 | + audioError = i2s_write(I2S_NUM_1, ibuffer, BUFFER_SIZE * 4, &bytesLength, portMAX_DELAY); |
| 155 | + } |
| 156 | + vTaskDelete(NULL); |
| 157 | +} |
| 158 | + |
| 159 | +// Main ------------------------------------------------------- |
| 160 | + |
| 161 | +void |
| 162 | +setup() |
| 163 | +{ |
| 164 | + pinMode(LED_PIN, OUTPUT); |
| 165 | + digitalWrite(LED_PIN, LOW); |
| 166 | + pinMode(SWITCH_PIN, INPUT_PULLUP); |
| 167 | + attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), handleInterrupt, FALLING); |
| 168 | + |
| 169 | + Serial.begin(115200); |
| 170 | + Serial.printf("Heap size: %u\n", ESP.getFreeHeap()); |
| 171 | + |
| 172 | + xTaskCreate(initTask, "initTask", 16384, NULL, 1, NULL); |
| 173 | +} |
| 174 | + |
| 175 | +void |
| 176 | +loop() |
| 177 | +{ |
| 178 | + esp_err_t audioError; |
| 179 | + chirp_connect_error_code_t chirpError; |
| 180 | + |
| 181 | + if (startTasks) { |
| 182 | + xTaskCreate(processInputTask, "processInputTask", 16384, NULL, 5, NULL); |
| 183 | + xTaskCreate(processOutputTask, "processOutputTask", 16384, NULL, 3, NULL); |
| 184 | + startTasks = false; |
| 185 | + } |
| 186 | + |
| 187 | + if (buttonPressed) { |
| 188 | + size_t payloadLength = 0; |
| 189 | + uint8_t *payload = chirp_connect_random_payload(connect, &payloadLength); |
| 190 | + chirp_connect_send(connect, payload, payloadLength); |
| 191 | + Serial.println("Sending data..."); |
| 192 | + buttonPressed = false; |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +void |
| 197 | +IRAM_ATTR handleInterrupt() |
| 198 | +{ |
| 199 | + buttonPressed = true; |
| 200 | +} |
| 201 | + |
| 202 | +// Chirp ------------------------------------------------------- |
| 203 | + |
| 204 | +void |
| 205 | +setupChirp() |
| 206 | +{ |
| 207 | + connect = new_chirp_connect(APP_KEY, APP_SECRET); |
| 208 | + if (connect == NULL) { |
| 209 | + Serial.println("Chirp initialisation failed."); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + chirp_connect_error_code_t err = chirp_connect_set_config(connect, APP_CONFIG); |
| 214 | + if (err != CHIRP_CONNECT_OK) |
| 215 | + chirpErrorHandler(err); |
| 216 | + |
| 217 | + chirp_connect_callback_set_t callbacks = {0}; |
| 218 | + callbacks.on_sending = NULL; |
| 219 | + callbacks.on_sent = NULL; |
| 220 | + callbacks.on_state_changed = onStateChangedCallback; |
| 221 | + callbacks.on_receiving = onReceivingCallback; |
| 222 | + callbacks.on_received = onReceivedCallback; |
| 223 | + |
| 224 | + err = chirp_connect_set_callbacks(connect, callbacks); |
| 225 | + if (err != CHIRP_CONNECT_OK) |
| 226 | + chirpErrorHandler(err); |
| 227 | + |
| 228 | + err = chirp_connect_set_callback_ptr(connect, connect); |
| 229 | + if (err != CHIRP_CONNECT_OK) |
| 230 | + chirpErrorHandler(err); |
| 231 | + |
| 232 | + err = chirp_connect_start(connect); |
| 233 | + if (err != CHIRP_CONNECT_OK) |
| 234 | + chirpErrorHandler(err); |
| 235 | + |
| 236 | + // Set volume to 0.5 to not distort output |
| 237 | + chirp_connect_set_volume(connect, 0.5); |
| 238 | + |
| 239 | + Serial.println("Chirp Connect initialised."); |
| 240 | +} |
| 241 | + |
| 242 | +void |
| 243 | +chirpErrorHandler(chirp_connect_error_code_t code) |
| 244 | +{ |
| 245 | + if (code != CHIRP_CONNECT_OK) { |
| 246 | + const char *error_string = chirp_connect_error_code_to_string(code); |
| 247 | + Serial.printf("Chirp error handler : %s\n", error_string); |
| 248 | + chirp_connect_free((void *) error_string); |
| 249 | + while (true) { |
| 250 | + delay(1000); |
| 251 | + Serial.print('.'); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +// Audio ------------------------------------------------------- |
| 257 | + |
| 258 | +void |
| 259 | +setupAudioInput(int sample_rate) |
| 260 | +{ |
| 261 | + /* |
| 262 | + Set up I2S audio for SPH0645 microphone |
| 263 | + */ |
| 264 | + esp_err_t err; |
| 265 | + Serial.println("Initialising audio input driver.."); |
| 266 | + |
| 267 | + const i2s_config_t i2s_config = { |
| 268 | + .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), |
| 269 | + .sample_rate = sample_rate, |
| 270 | + .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, |
| 271 | + .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, |
| 272 | + .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB), |
| 273 | + .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, |
| 274 | + .dma_buf_count = 8, |
| 275 | + .dma_buf_len = 64, |
| 276 | + .use_apll = true |
| 277 | + }; |
| 278 | + |
| 279 | + const i2s_pin_config_t pin_config = { |
| 280 | + .bck_io_num = I2SI_BCK, |
| 281 | + .ws_io_num = I2SI_LRCL, |
| 282 | + .data_out_num = I2S_PIN_NO_CHANGE, |
| 283 | + .data_in_num = I2SI_DATA |
| 284 | + }; |
| 285 | + |
| 286 | + err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); |
| 287 | + if (err != ESP_OK) { |
| 288 | + Serial.printf("Failed installing driver: %d\n", err); |
| 289 | + while (true); |
| 290 | + } |
| 291 | + |
| 292 | + err = i2s_set_pin(I2S_NUM_0, &pin_config); |
| 293 | + if (err != ESP_OK) { |
| 294 | + Serial.printf("Failed setting pin: %d\n", err); |
| 295 | + while (true); |
| 296 | + } |
| 297 | + |
| 298 | + err = i2s_set_sample_rates(I2S_NUM_0, sample_rate); |
| 299 | + if (err != ESP_OK) { |
| 300 | + Serial.printf("Failed to set sample rates: %d\n", err); |
| 301 | + while (true); |
| 302 | + } |
| 303 | + |
| 304 | + Serial.println("Audio input driver initalised."); |
| 305 | +} |
| 306 | + |
| 307 | + |
| 308 | +void |
| 309 | +setupAudioOutput(int sample_rate) |
| 310 | +{ |
| 311 | + /* |
| 312 | + Set up I2S audio for UDA1334 DAC output |
| 313 | + */ |
| 314 | + esp_err_t err; |
| 315 | + Serial.println("Initialising audio output driver.."); |
| 316 | + |
| 317 | + const i2s_config_t i2s_config = { |
| 318 | + .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_TX), |
| 319 | + .sample_rate = sample_rate, |
| 320 | + .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, |
| 321 | + .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, |
| 322 | + .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB), |
| 323 | + .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, |
| 324 | + .dma_buf_count = 8, |
| 325 | + .dma_buf_len = 64, |
| 326 | + .use_apll = true |
| 327 | + }; |
| 328 | + |
| 329 | + const i2s_pin_config_t pin_config = { |
| 330 | + .bck_io_num = I2SO_BCK, |
| 331 | + .ws_io_num = I2SO_WSEL, |
| 332 | + .data_out_num = I2SO_DATA, |
| 333 | + .data_in_num = I2S_PIN_NO_CHANGE |
| 334 | + }; |
| 335 | + |
| 336 | + err = i2s_driver_install(I2S_NUM_1, &i2s_config, 0, NULL); |
| 337 | + if (err != ESP_OK) { |
| 338 | + Serial.printf("Failed installing driver: %d\n", err); |
| 339 | + while (true); |
| 340 | + } |
| 341 | + |
| 342 | + err = i2s_set_pin(I2S_NUM_1, &pin_config); |
| 343 | + if (err != ESP_OK) { |
| 344 | + Serial.printf("Failed setting pin: %d\n", err); |
| 345 | + while (true); |
| 346 | + } |
| 347 | + |
| 348 | + err = i2s_set_sample_rates(I2S_NUM_1, sample_rate); |
| 349 | + if (err != ESP_OK) { |
| 350 | + Serial.printf("Failed to set sample rates: %d\n", err); |
| 351 | + while (true); |
| 352 | + } |
| 353 | + |
| 354 | + Serial.println("Audio output driver initalised."); |
| 355 | +} |
0 commit comments