Skip to content

Commit c88c9e5

Browse files
Damien Laidinjoextodd
authored andcommitted
updated examples
1 parent 51a2537 commit c88c9e5

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

examples/MXChipSendReceive/MXChipSendReceive.ino

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ int lastButtonBState;
8686

8787
RGB_LED rgbLed;
8888

89+
/*
90+
* Convert a payload to an hexadecimal identifer terminating with '\0'
91+
*/
92+
void payload_to_hex(char *payload, int payload_length, char *hex_str)
93+
{
94+
for (int i = 0; i < payload_length; i++)
95+
{
96+
sprintf(hex_str + i * 2, "%02x", payload[i]);
97+
}
98+
hex_str[payload_length * 2] = '\\0';
99+
}
100+
89101
/*
90102
* Global pointer to the SDK structure. This is global as this pointer is
91103
* needed when processing the audio in the loop() function.
@@ -141,17 +153,14 @@ void on_sending_callback(void *data, uint8_t *payload, size_t length, uint8_t ch
141153
*/
142154
void on_sent_callback(void *data, uint8_t *payload, size_t length, uint8_t channel)
143155
{
144-
char *identifier = chirp_sdk_as_string(chirp, payload, length);
145-
char strLength[8] = {0};
146-
itoa(length, strLength, 10);
156+
char identifier[length * 2 + 1];
157+
payload_to_hex(payload, length, identifier);
147158

148159
Screen.clean();
149160
Screen.print(0, "Sent !");
150161
Screen.print(1, (const char *) identifier, true);
151-
Screen.print(3, strLength);
162+
Screen.print(3, strlen(identifer));
152163
rgbLed.setColor(0, 255, 255);
153-
154-
chirp_sdk_free(identifier);
155164
}
156165

157166
/*
@@ -170,17 +179,14 @@ void on_received_callback(void *data, uint8_t *payload, size_t length, uint8_t c
170179
// A pointer not null with a length different than 0 means the decode has succedeed.
171180
if (payload && length != 0)
172181
{
173-
char *identifier = chirp_sdk_as_string(chirp, payload, length);
174-
char strLength[8] = {0};
175-
itoa(length, strLength, 10);
182+
char identifier[length * 2 + 1];
183+
payload_to_hex(payload, length, identifier);
176184

177185
Screen.clean();
178186
Screen.print(0, "Received !");
179187
Screen.print(1, (const char *) identifier, true);
180-
Screen.print(3, strLength);
188+
Screen.print(3, strlen(identifer));
181189
rgbLed.setColor(0, 255, 0);
182-
183-
chirp_sdk_free(identifier);
184190
}
185191
// A null pointer with a length of 0 means the decode has failed.
186192
else
@@ -216,11 +222,11 @@ void setup()
216222
chirp_sdk_error_code_t errorCode = chirp_sdk_set_config(chirp, CHIRP_APP_CONFIG);
217223
errorHandler(errorCode);
218224

219-
printf("Licence set correctly\n");
225+
printf("Config set correctly\n");
220226

221227
char *info = chirp_sdk_get_info(chirp);
222228
printf("%s - V%s\n", info, chirp_sdk_get_version());
223-
free(info);
229+
chirp_sdk_free(info);
224230

225231
errorCode = chirp_sdk_set_input_sample_rate(chirp, SAMPLE_RATE);
226232
errorHandler(errorCode);
@@ -320,7 +326,7 @@ void loop()
320326
uint8_t *randomPayload = chirp_sdk_random_payload(chirp, &randomPayloadLength);
321327
errorCode = chirp_sdk_send(chirp, randomPayload, randomPayloadLength);
322328
errorHandler(errorCode);
323-
free(randomPayload);
329+
chirp_sdk_free(randomPayload);
324330
}
325331

326332
chirp_sdk_state_t state = chirp_sdk_get_state(chirp);

0 commit comments

Comments
 (0)