From 1d3c7b339926db05d1d7552c07e427df26c76c63 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Thu, 10 Feb 2022 11:56:48 -0800 Subject: [PATCH] Replace 'drain' with a slightly more correct implementation --- .../src/kaleidoscope/plugin/FocusSerial.cpp | 18 ++++++++---------- .../src/kaleidoscope/plugin/FocusSerial.h | 1 - 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp index 268cc4db..d209beb9 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp @@ -26,12 +26,6 @@ namespace plugin { char FocusSerial::command_[32]; -void FocusSerial::drain(void) { - if (Runtime.serialPort().available()) - while (Runtime.serialPort().peek() != '\n') - Runtime.serialPort().read(); -} - EventHandlerResult FocusSerial::afterEachCycle() { // If the serial buffer is empty, we don't have any work to do if (Runtime.serialPort().available() == 0) @@ -51,12 +45,16 @@ EventHandlerResult FocusSerial::afterEachCycle() { Runtime.onFocusEvent(command_); + while (Runtime.serialPort().available()) { + char c = Runtime.serialPort().read(); + if (c == NEWLINE) { + // newline serves as an end-of-command marker + // don't drain the buffer past there + break; + } + } Runtime.serialPort().println(F("\r\n.")); - drain(); - - if (Runtime.serialPort().peek() == '\n') - Runtime.serialPort().read(); return EventHandlerResult::OK; } diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h index 0c39937b..98652fba 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h @@ -97,7 +97,6 @@ class FocusSerial : public kaleidoscope::Plugin { private: static char command_[32]; - static void drain(void); static void printBool(bool b); }; }