66 @file MKRZeroSend.ino
77
88 @brief Create a developer account at https://developers.chirp.io,
9- and copy and paste your key, secret and config string for chosen
10- protocol into the credentials.h file.
9+ and copy and paste your key, secret and config string for the
10+ "16khz-mono-embedded" protocol into the credentials.h file.
1111
1212 This example will start sending a random chirp payload on start up.
1313
1919 *Note*: This board is send-only as it does not have a floating-point unit
2020 which is required to receive data.
2121
22+ *Important*: The example will not start until this Serial Monitor is opened.
23+ To disable this behaviour, comment out the while(!Serial) line.
24+
2225 Circuit:
2326 - Arduino MKRZero
2427 - Adafruit UDA1334
3942#include " chirp_connect.h"
4043#include " credentials.h"
4144
42- #define VOLUME 0.1
45+ #define VOLUME 0.1 // Between 0 and 1
4346
4447#define NUM_BUFFERS 2
45- #define BUFFER_SIZE 512
48+ #define BUFFER_SIZE 1024
4649#define SAMPLE_RATE 44100
4750
48- // Global variables -------------------------------------------
51+ // Global variables ------------------------------------------------------------
4952
5053int buffer[NUM_BUFFERS][BUFFER_SIZE];
5154short tmpBuffer[BUFFER_SIZE / 2 ];
5255uint8_t nextBufferIndex, currentBufferIndex;
53- uint16_t volumeInt;
5456
5557Adafruit_ZeroI2S i2s;
5658Adafruit_ZeroDMA dma;
@@ -59,19 +61,19 @@ DmacDescriptor *desc;
5961static chirp_connect_t *chirp = NULL ;
6062static volatile bool dma_complete = true ;
6163
62- // Function definitions ---------------------------------------
64+ // Function definitions --------------------------------------------------------
6365
6466void dmaCallback (Adafruit_ZeroDMA *dma);
6567void dmaErrorCallback (Adafruit_ZeroDMA *dma);
6668void setupDMA (void );
6769void setupChirp (void );
70+ void sendChirp (void );
6871
69- // Function declarations --------------------------------------
72+ // Function declarations -------------------------------------------------------
7073
7174void setup ()
7275{
7376 currentBufferIndex = 0 ;
74- volumeInt = VOLUME * INT16_MAX;
7577
7678 Serial.begin (115200 );
7779 while (!Serial); // Wait for Serial monitor before continuing
@@ -83,16 +85,7 @@ void setup()
8385 i2s.enableMCLK ();
8486 i2s.enableTx ();
8587
86- size_t payload_len = 5 ;
87- uint8_t *payload = chirp_connect_random_payload (chirp, &payload_len);
88-
89- char *hex = chirp_connect_as_string (chirp, payload, payload_len);
90- Serial.print (" Generated payload: " );
91- Serial.println (hex);
92- chirp_connect_free (hex);
93-
94- chirp_connect_error_code_t err = chirp_connect_send (chirp, payload, payload_len);
95- chirpErrorHandler (err);
88+ sendChirp ();
9689
9790 ZeroDMAstatus stat = dma.startJob ();
9891 if (stat != DMA_STATUS_OK)
@@ -113,17 +106,18 @@ void loop()
113106 chirpErrorHandler (err);
114107
115108 // Copy the data into a stereo buffer for audio output
116- for (int i = 0 ; i < BUFFER_SIZE / 2 ; i++) {
117- int value = tmpBuffer[i] * volumeInt;
118- buffer[nextBufferIndex][i * 2 ] = value;
119- buffer[nextBufferIndex][i * 2 + 1 ] = value;
109+ for (int i = 0 ; i < BUFFER_SIZE / 2 ; i++)
110+ {
111+ int value = tmpBuffer[i] * INT16_MAX;
112+ buffer[next][i * 2 ] = value;
113+ buffer[next][i * 2 + 1 ] = value;
120114 }
121115
122116 dma_complete = false ;
123117 }
124118}
125119
126- // Chirp -----------------------------------------------------
120+ // Chirp -----------------------------------------------------------------------
127121
128122void chirpErrorHandler (chirp_connect_error_code_t code)
129123{
@@ -145,7 +139,19 @@ void onSentCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channe
145139 Serial.println (" Sent data..." );
146140}
147141
148- void setupChirp (void )
142+ void sendChirp ()
143+ {
144+ chirp_connect_error_code_t err;
145+
146+ char *payload = " hello" ;
147+ err = chirp_connect_send (chirp, (uint8_t *)payload, strlen (payload));
148+ chirpErrorHandler (err);
149+
150+ Serial.print (" Sending data: " );
151+ Serial.println (payload);
152+ }
153+
154+ void setupChirp ()
149155{
150156 chirp = new_chirp_connect (CHIRP_APP_KEY, CHIRP_APP_SECRET);
151157 if (chirp == NULL )
@@ -171,14 +177,17 @@ void setupChirp(void)
171177 err = chirp_connect_set_output_sample_rate (chirp, SAMPLE_RATE);
172178 chirpErrorHandler (err);
173179
180+ err = chirp_connect_set_volume (chirp, VOLUME);
181+ chirpErrorHandler (err);
182+
174183 err = chirp_connect_start (chirp);
175184 chirpErrorHandler (err);
176185
177186 Serial.println (" Chirp SDK initialised." );
178187 Serial.flush ();
179188}
180189
181- // I2S DMA ---------------------------------------------------
190+ // I2S DMA ---------------------------------------------------------------------
182191
183192void dmaCallback (Adafruit_ZeroDMA *dma)
184193{
0 commit comments