Merge pull request #1226 from tlyu/focus-gather

Focus: correctly handle delayed newline
pull/1227/head
Jesse Vincent 2 years ago committed by GitHub
commit becf816dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

Loading…
Cancel
Save