From 62f668fd25ec924b2d278360416125592a34b5c1 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Thu, 10 Feb 2022 12:04:24 -0800 Subject: [PATCH] Don't block waiting for input blindly. Be more efficient about how we check for a newline to end processing. Replace a magic value with what it really means. TODO: We may want to block for a few fractions of a to make sure that we don't read half a command Signed-off-by: Jesse Vincent --- .../src/kaleidoscope/plugin/FocusSerial.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp index a6f89b9b..de347ff8 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp @@ -31,15 +31,17 @@ EventHandlerResult FocusSerial::afterEachCycle() { if (Runtime.serialPort().available() == 0) return EventHandlerResult::OK; + uint8_t i = 0; memset(command_, 0, sizeof(command_)); do { command_[i++] = Runtime.serialPort().read(); + } while (command_[i - 1] != SEPARATOR + && i < sizeof(command_) + && Runtime.serialPort().available() + && (Runtime.serialPort().peek() != NEWLINE )); - if (Runtime.serialPort().peek() == '\n') - break; - } while (command_[i - 1] != ' ' && i < 32); // If this was a command with a space-delimited payload, strip the space delimiter off if (command_[i - 1] == SEPARATOR )