|
1 | | -# chirp-arduino |
| 1 | +# Chirp for Arduino |
| 2 | + |
| 3 | +*Version 3.2.5, January 2019* |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Chirp is a library enabling Arduino-based devices to send and receive data using sound. You'll need: |
| 8 | + |
| 9 | + * An ESP32 development board |
| 10 | + * A digital I2S MEMS microphone |
| 11 | + * A digital I2S amplifier and compatible speaker |
| 12 | + |
| 13 | +You'll need an ESP32 (or a board with an equivalent processor). For sound input you will need a digital MEMS microphone such as the SPH0645 or ICS-43434. For sound output it is recommended to use a digital I2S output such as the UDA1334A or MAX98357A connected to a compatible speaker, however analogue output is also possible with this board. |
| 14 | +You can quickly test the sound input by playing random chirps from the [Developer Hub](https://developers.chirp.io). The quickest way to test the sound output would be to use Chirp on the [command line](https://developers.chirp.io/docs/tutorials/command-line). |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Chirp is written for the Arduino IDE versions 1.8.6 and above. |
| 19 | + |
| 20 | +Install ChirpSDK as a library. For instructions, see |
| 21 | + |
| 22 | +[http://arduino.cc/en/Guide/Libraries](http://arduino.cc/en/Guide/Libraries) |
| 23 | + |
| 24 | +Once installed, you can access the example programs from the menu : |
| 25 | + |
| 26 | +```File > Examples > ChirpSDK > example ``` |
| 27 | + |
| 28 | +and you can include the headers to use Chirp in your own code by using : |
| 29 | + |
| 30 | +```Sketch > Import Library > ChirpSDK``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +To set up the Chirp SDK, initialise and configure with your app key, secret and `arduino` config from the [Developer Hub](https://developers.chirp.io). Then set any required callbacks and start the SDK running. |
| 35 | + |
| 36 | + connect = new_chirp_connect(APP_KEY, APP_SECRET); |
| 37 | + if (connect == NULL) { |
| 38 | + Serial.println("Chirp initialisation failed."); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + chirp_connect_error_code_t err = chirp_connect_set_config(connect, APP_CONFIG); |
| 43 | + if (err != CHIRP_CONNECT_OK) |
| 44 | + return; |
| 45 | + |
| 46 | + chirp_connect_callback_set_t callbacks = {0}; |
| 47 | + callbacks.on_received = onReceivedCallback; |
| 48 | + err = chirp_connect_set_callbacks(connect, callbacks); |
| 49 | + if (err != CHIRP_CONNECT_OK) |
| 50 | + return; |
| 51 | + |
| 52 | + err = chirp_connect_set_callback_ptr(connect, connect); |
| 53 | + if (err != CHIRP_CONNECT_OK) |
| 54 | + return; |
| 55 | + |
| 56 | + // Set input/output sample rates if not 44.1kHz |
| 57 | + err = chirp_connect_set_input_sample_rate(connect, input_sample_rate); |
| 58 | + if (err != CHIRP_CONNECT_OK) |
| 59 | + return; |
| 60 | + err = chirp_connect_set_output_sample_rate(connect, output_sample_rate); |
| 61 | + if (err != CHIRP_CONNECT_OK) |
| 62 | + return; |
| 63 | + |
| 64 | + err = chirp_connect_start(connect); |
| 65 | + if (err != CHIRP_CONNECT_OK) |
| 66 | + return; |
| 67 | + |
| 68 | +## Callbacks |
| 69 | + |
| 70 | +The received data is passed back to the `onReceivedCallback` function. If the payload pointer is null then there has been an error decoding the data. |
| 71 | + |
| 72 | + void |
| 73 | + onReceivedCallback(void *connect, uint8_t *payload, size_t length, uint8_t channel) { |
| 74 | + if (payload) { |
| 75 | + char *hexString = chirp_connect_as_string(connect, payload, length); |
| 76 | + Serial.printf("Received data = %s\n", hexString); |
| 77 | + chirp_connect_free(hexString); |
| 78 | + } else { |
| 79 | + Serial.println("Decode failed."); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +A complete list of callbacks is shown below. |
| 84 | + |
| 85 | + void onStateChangedCallback(void *ptr, chirp_connect_state_t old_state, chirp_connect_state_t new_state) { |
| 86 | + // Put here what you want to do when the SDK's state is changing. |
| 87 | + } |
| 88 | + |
| 89 | + void onSendingCallback(void *ptr, uint8_t *bytes, size_t length, uint8_t channel) { |
| 90 | + // Put here what you want to do when the SDK starts to send some data. |
| 91 | + } |
| 92 | + |
| 93 | + void onSentCallback(void *ptr, uint8_t *bytes, size_t length, uint8_t channel) { |
| 94 | + // Put here what you want to do when the SDK has sent some data. |
| 95 | + } |
| 96 | + |
| 97 | + void onReceivingCallback(void *ptr, uint8_t *bytes, size_t length, uint8_t channel) { |
| 98 | + // Put here what you want to do when the SDK starts receiving some data. |
| 99 | + } |
| 100 | + |
| 101 | + void onReceivedCallback(void *ptr, uint8_t *bytes, size_t length, uint8_t channel) { |
| 102 | + // Put here what you want to do when the SDK has received some data. |
| 103 | + } |
| 104 | + |
| 105 | + // If you don't set all the callbacks, make sure the unused callbacks are set to NULL. |
| 106 | + chirp_connect_callback_set_t callbacks_set = { |
| 107 | + .on_state_changed = on_state_changed_callback, |
| 108 | + .on_sending = on_sending_callback, |
| 109 | + .on_sent = on_sent_callback, |
| 110 | + .on_receiving = on_receiving_callback, |
| 111 | + .on_received = on_received_callback |
| 112 | + }; |
| 113 | + err = chirp_connect_set_callbacks(connect, callbacks_set); |
| 114 | + if (err != CHIRP_CONNECT_OK) |
| 115 | + { |
| 116 | + const char *error_string = chirp_connect_error_code_to_string(err); |
| 117 | + printf("%s\n", error_string); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | +## Payloads |
| 122 | + |
| 123 | +A Chirp payload is simply an array of bytes. You can send a random data payload to the speakers like so. |
| 124 | + |
| 125 | + size_t payload_length = chirp_connect_get_max_payload_length(connect); |
| 126 | + uint8_t *payload = chirp_connect_random_payload(connect, &payload_length); |
| 127 | + |
| 128 | + err = chirp_connect_send(chirp_connect, payload, payload_length); |
| 129 | + if (err != CHIRP_CONNECT_OK) { |
| 130 | + const char *error_string = chirp_connect_error_code_to_string(error_code); |
| 131 | + printf("%s\n", error_string); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | +## Processing |
| 136 | + |
| 137 | +To process audio data from the microphone, and fill the output buffer with audio data, call the following functions with data periodically. |
| 138 | + |
| 139 | + err = chirp_connect_process_input(chirp_connect, input_buffer, input_buffer_length); |
| 140 | + |
| 141 | + err = chirp_connect_process_output(chirp_connect, output_buffer, output_buffer_length); |
| 142 | + |
| 143 | + |
| 144 | +## Example |
| 145 | + |
| 146 | +Once ChirpSDK is installed as a library an example is supplied in the IDE menu ```File > Examples > ChirpSDK > Example```. |
| 147 | + |
| 148 | +This example script demonstrates how to receive data using the SPH0645 microphone. |
| 149 | + |
| 150 | +*** |
| 151 | + |
| 152 | +All content copyright © Asio Ltd, 2013-2019. All rights reserved. |
0 commit comments