Skip to content

Commit

Permalink
Merge pull request #1369 from keyboardio/f/flow-control-after-read
Browse files Browse the repository at this point in the history
Reduce the chances of xon/xoff causing comms to wedge
  • Loading branch information
obra authored Dec 13, 2023
2 parents 31ab385 + 81851f7 commit db675ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ void FocusSerial::manageFlowControl() {
if (xon == true) {
if (avail > RECV_BUFFER_THRESHOLD) {
Runtime.serialPort().write(XOFF); // Send XOFF to stop data
Runtime.serialPort().flush();
xon = false;
}
} else {
if (avail < RECV_BUFFER_RESUME) {
Runtime.serialPort().write(XON); // Send XON to resume data
Runtime.serialPort().flush();
xon = true;
}
}
Expand All @@ -65,6 +67,7 @@ EventHandlerResult FocusSerial::afterEachCycle() {
break;
}
c = Runtime.serialPort().read();
manageFlowControl();
// Don't store the separator; just stash it
if (c == SEPARATOR) {
break;
Expand All @@ -82,6 +85,7 @@ EventHandlerResult FocusSerial::afterEachCycle() {
Runtime.onFocusEvent(input_);
while (Runtime.serialPort().available()) {
c = Runtime.serialPort().read();
manageFlowControl();
if (c == NEWLINE) {
// newline serves as an end-of-command marker
// don't drain the buffer past there
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,29 @@ class FocusSerial : public kaleidoscope::Plugin {
void read(Key &key) {
manageFlowControl();
key.setRaw(Runtime.serialPort().parseInt());
manageFlowControl();
}
void read(cRGB &color) {
manageFlowControl();
color.r = Runtime.serialPort().parseInt();
color.g = Runtime.serialPort().parseInt();
color.b = Runtime.serialPort().parseInt();
manageFlowControl();
}
void read(char &c) {
manageFlowControl();
Runtime.serialPort().readBytes(&c, 1);
manageFlowControl();
}
void read(uint8_t &u8) {
manageFlowControl();
u8 = Runtime.serialPort().parseInt();
manageFlowControl();
}
void read(uint16_t &u16) {
manageFlowControl();
u16 = Runtime.serialPort().parseInt();
manageFlowControl();
}


Expand Down

0 comments on commit db675ec

Please sign in to comment.