Merge pull request #1105 from keyboardio/f/focus-parser

Reimplement focus' parser to work on GD32
pull/1097/head
Gergely Nagy 3 years ago committed by GitHub
commit ab4ee0babd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -25,39 +25,59 @@ namespace kaleidoscope {
namespace plugin { namespace plugin {
char FocusSerial::command_[32]; char FocusSerial::command_[32];
uint8_t FocusSerial::buf_cursor_ = 0;
void FocusSerial::drain(void) {
if (Runtime.serialPort().available())
while (Runtime.serialPort().peek() != '\n')
Runtime.serialPort().read();
}
EventHandlerResult FocusSerial::afterEachCycle() { EventHandlerResult FocusSerial::afterEachCycle() {
if (Runtime.serialPort().available() == 0) // If the serial buffer is empty, we don't have any work to do
if (Runtime.serialPort().available() == 0) {
return EventHandlerResult::OK; return EventHandlerResult::OK;
}
uint8_t i = 0;
do { do {
command_[i++] = Runtime.serialPort().read(); command_[buf_cursor_++] = Runtime.serialPort().read();
} while (command_[buf_cursor_ - 1] != SEPARATOR
&& buf_cursor_ < sizeof(command_)
&& Runtime.serialPort().available()
&& (Runtime.serialPort().peek() != NEWLINE));
if (Runtime.serialPort().peek() == '\n')
break;
} while (command_[i - 1] != ' ' && i < 32);
if (command_[i - 1] == ' ')
command_[i - 1] = '\0';
else
command_[i] = '\0';
Runtime.onFocusEvent(command_); // If there was no command, there's nothing to do
if (command_[0] == '\0') {
Runtime.serialPort().println(F("\r\n.")); buf_cursor_ = 0;
memset(command_, 0, sizeof(command_));
return EventHandlerResult::OK;
}
drain(); 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 (Runtime.serialPort().peek() == '\n') // If this was a command with a space-delimited payload,
Runtime.serialPort().read(); // 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) {
// newline serves as an end-of-command marker
// don't drain the buffer past there
break;
}
}
// 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; return EventHandlerResult::OK;
} }
bool FocusSerial::handleHelp(const char *command, bool FocusSerial::handleHelp(const char *command,

@ -25,6 +25,10 @@ class FocusSerial : public kaleidoscope::Plugin {
public: public:
FocusSerial(void) {} FocusSerial(void) {}
static constexpr char COMMENT = '#';
static constexpr char SEPARATOR = ' ';
static constexpr char NEWLINE = '\n';
bool handleHelp(const char *command, bool handleHelp(const char *command,
const char *help_message); const char *help_message);
@ -83,12 +87,9 @@ class FocusSerial : public kaleidoscope::Plugin {
} }
bool isEOL() { bool isEOL() {
return Runtime.serialPort().peek() == '\n'; return Runtime.serialPort().peek() == NEWLINE;
} }
static constexpr char COMMENT = '#';
static constexpr char SEPARATOR = ' ';
static constexpr char NEWLINE = '\n';
/* Hooks */ /* Hooks */
EventHandlerResult afterEachCycle(); EventHandlerResult afterEachCycle();
@ -96,8 +97,7 @@ class FocusSerial : public kaleidoscope::Plugin {
private: private:
static char command_[32]; static char command_[32];
static uint8_t buf_cursor_;
static void drain(void);
static void printBool(bool b); static void printBool(bool b);
}; };
} }

Loading…
Cancel
Save