6
6
@file MKRZeroSend.ino
7
7
8
8
@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.
11
11
12
12
This example will start sending a random chirp payload on start up.
13
13
19
19
*Note*: This board is send-only as it does not have a floating-point unit
20
20
which is required to receive data.
21
21
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
+
22
25
Circuit:
23
26
- Arduino MKRZero
24
27
- Adafruit UDA1334
39
42
#include " chirp_connect.h"
40
43
#include " credentials.h"
41
44
42
- #define VOLUME 0.1
45
+ #define VOLUME 0.1 // Between 0 and 1
43
46
44
47
#define NUM_BUFFERS 2
45
- #define BUFFER_SIZE 512
48
+ #define BUFFER_SIZE 1024
46
49
#define SAMPLE_RATE 44100
47
50
48
- // Global variables -------------------------------------------
51
+ // Global variables ------------------------------------------------------------
49
52
50
53
int buffer[NUM_BUFFERS][BUFFER_SIZE];
51
54
short tmpBuffer[BUFFER_SIZE / 2 ];
52
55
uint8_t nextBufferIndex, currentBufferIndex;
53
- uint16_t volumeInt;
54
56
55
57
Adafruit_ZeroI2S i2s;
56
58
Adafruit_ZeroDMA dma;
@@ -59,19 +61,19 @@ DmacDescriptor *desc;
59
61
static chirp_connect_t *chirp = NULL ;
60
62
static volatile bool dma_complete = true ;
61
63
62
- // Function definitions ---------------------------------------
64
+ // Function definitions --------------------------------------------------------
63
65
64
66
void dmaCallback (Adafruit_ZeroDMA *dma);
65
67
void dmaErrorCallback (Adafruit_ZeroDMA *dma);
66
68
void setupDMA (void );
67
69
void setupChirp (void );
70
+ void sendChirp (void );
68
71
69
- // Function declarations --------------------------------------
72
+ // Function declarations -------------------------------------------------------
70
73
71
74
void setup ()
72
75
{
73
76
currentBufferIndex = 0 ;
74
- volumeInt = VOLUME * INT16_MAX;
75
77
76
78
Serial.begin (115200 );
77
79
while (!Serial); // Wait for Serial monitor before continuing
@@ -83,16 +85,7 @@ void setup()
83
85
i2s.enableMCLK ();
84
86
i2s.enableTx ();
85
87
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 ();
96
89
97
90
ZeroDMAstatus stat = dma.startJob ();
98
91
if (stat != DMA_STATUS_OK)
@@ -113,17 +106,18 @@ void loop()
113
106
chirpErrorHandler (err);
114
107
115
108
// 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;
120
114
}
121
115
122
116
dma_complete = false ;
123
117
}
124
118
}
125
119
126
- // Chirp -----------------------------------------------------
120
+ // Chirp -----------------------------------------------------------------------
127
121
128
122
void chirpErrorHandler (chirp_connect_error_code_t code)
129
123
{
@@ -145,7 +139,19 @@ void onSentCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channe
145
139
Serial.println (" Sent data..." );
146
140
}
147
141
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 ()
149
155
{
150
156
chirp = new_chirp_connect (CHIRP_APP_KEY, CHIRP_APP_SECRET);
151
157
if (chirp == NULL )
@@ -171,14 +177,17 @@ void setupChirp(void)
171
177
err = chirp_connect_set_output_sample_rate (chirp, SAMPLE_RATE);
172
178
chirpErrorHandler (err);
173
179
180
+ err = chirp_connect_set_volume (chirp, VOLUME);
181
+ chirpErrorHandler (err);
182
+
174
183
err = chirp_connect_start (chirp);
175
184
chirpErrorHandler (err);
176
185
177
186
Serial.println (" Chirp SDK initialised." );
178
187
Serial.flush ();
179
188
}
180
189
181
- // I2S DMA ---------------------------------------------------
190
+ // I2S DMA ---------------------------------------------------------------------
182
191
183
192
void dmaCallback (Adafruit_ZeroDMA *dma)
184
193
{
0 commit comments