Skip to content

Commit 539a468

Browse files
committed
Verify what's read is actually a delimiter. #200
1 parent 0a38742 commit 539a468

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

microcontroller-src/kv4p_ht_esp32_wroom_32/kv4p_ht_esp32_wroom_32.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,16 @@ void loop() {
278278
uint8_t tempBuffer[100]; // Big enough for a command and params, won't hold audio data
279279
int bytesRead = 0;
280280

281-
while (bytesRead < (DELIMITER_LENGTH + 1)) { // Read the delimiter and the command byte only (no params yet)
282-
if (Serial.available()) {
283-
tempBuffer[bytesRead++] = Serial.read();
281+
while (bytesRead < (DELIMITER_LENGTH)) { // Read the delimiter and the command byte only (no params yet)
282+
uint8_t tmp = Serial.read();
283+
if (tmp != COMMAND_DELIMITER[bytesRead]) {
284+
// Not a delimiter. Reset.
285+
bytesRead = 0;
286+
continue;
284287
}
288+
tempBuffer[bytesRead++] = tmp;
285289
}
290+
tempBuffer[DELIMITER_LENGTH] = Serial.read();
286291
switch (tempBuffer[DELIMITER_LENGTH]) {
287292

288293
case COMMAND_STOP:

0 commit comments

Comments
 (0)