From 54fbb04eb6f0c610babfbd41492175919fb7e971 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Thu, 10 Feb 2022 15:43:32 -0800 Subject: [PATCH] Give FocusSerial the ability to build a command string across multiple cycles by maintaining buffer state --- .../src/kaleidoscope/plugin/FocusSerial.cpp | 42 ++++++++++++------- .../src/kaleidoscope/plugin/FocusSerial.h | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp index 3b20d395..6594f82c 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp @@ -25,36 +25,45 @@ namespace kaleidoscope { namespace plugin { char FocusSerial::command_[32]; +uint8_t FocusSerial::buf_cursor_ = 0; EventHandlerResult FocusSerial::afterEachCycle() { // If the serial buffer is empty, we don't have any work to do - if (Runtime.serialPort().available() == 0) + 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_) + command_[buf_cursor_++] = Runtime.serialPort().read(); + } while ( command_[buf_cursor_ - 1] != SEPARATOR + && buf_cursor_ < sizeof(command_) && Runtime.serialPort().available() && (Runtime.serialPort().peek() != NEWLINE)); - // If this was a command with a space-delimited payload, strip the space delimiter off - if (command_[i - 1] == SEPARATOR) - command_[i - 1] = '\0'; - - // 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; } - Runtime.onFocusEvent(command_); + if ( (command_[buf_cursor_ - 1] != SEPARATOR) && (Runtime.serialPort().peek() != NEWLINE) + && buf_cursor_ < sizeof(command_) + ) { + // 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(); if (c == NEWLINE) { @@ -65,9 +74,10 @@ EventHandlerResult FocusSerial::afterEachCycle() { } // End of command processing is signalled with a CRLF followed by a single period Runtime.serialPort().println(F("\r\n.")); - - + buf_cursor_ = 0; + memset(command_, 0, sizeof(command_)); return EventHandlerResult::OK; + } bool FocusSerial::handleHelp(const char *command, diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h index e84e4d4b..231bd2a5 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h @@ -97,7 +97,7 @@ class FocusSerial : public kaleidoscope::Plugin { private: static char command_[32]; - + static uint8_t buf_cursor_; static void printBool(bool b); }; }