diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp index 70db42cd..486ad022 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp @@ -33,6 +33,7 @@ namespace kaleidoscope { namespace plugin { EventHandlerResult FocusSerial::afterEachCycle() { + int c; // GD32 doesn't currently autoflush the very last packet. So manually flush here Runtime.serialPort().flush(); // If the serial buffer is empty, we don't have any work to do @@ -41,33 +42,28 @@ EventHandlerResult FocusSerial::afterEachCycle() { } do { - command_[buf_cursor_++] = Runtime.serialPort().read(); - } while (command_[buf_cursor_ - 1] != SEPARATOR && buf_cursor_ < sizeof(command_) && Runtime.serialPort().available() && (Runtime.serialPort().peek() != NEWLINE)); - - - // If there was no command, there's nothing to do - if (command_[0] == '\0') { - buf_cursor_ = 0; - memset(command_, 0, sizeof(command_)); - return EventHandlerResult::OK; - } + // If there's a newline pending, don't read it + if (Runtime.serialPort().peek() == NEWLINE) { + break; + } + c = Runtime.serialPort().read(); + // Don't store the separator; just stash it + if (c == SEPARATOR) { + break; + } + command_[buf_cursor_++] = c; + } while (buf_cursor_ < (sizeof(command_) - 1) && Runtime.serialPort().available()); - if ((command_[buf_cursor_ - 1] != SEPARATOR) && (Runtime.serialPort().peek() != NEWLINE) && buf_cursor_ < sizeof(command_)) { + if ((c != SEPARATOR) && (Runtime.serialPort().peek() != NEWLINE) && buf_cursor_ < (sizeof(command_) - 1)) { // We don't have enough command to work with yet. // Let's leave the buffer around for another cycle return EventHandlerResult::OK; } - // If this was a command with a space-delimited payload, - // strip the space delimiter off - if ((command_[buf_cursor_ - 1] == SEPARATOR)) { - command_[buf_cursor_ - 1] = '\0'; - } - // Then process the command Runtime.onFocusEvent(command_); while (Runtime.serialPort().available()) { - char c = Runtime.serialPort().read(); + c = Runtime.serialPort().read(); if (c == NEWLINE) { // newline serves as an end-of-command marker // don't drain the buffer past there