|
|
|
/* -*- mode: c++ -*-
|
|
|
|
* Kaleidoscope-FocusSerial -- Bidirectional communication plugin
|
|
|
|
* Copyright (C) 2017, 2018, 2021 Keyboard.io, Inc
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kaleidoscope/plugin/FocusSerial.h"
|
|
|
|
|
|
|
|
#include <Arduino.h> // for __FlashStringHelper, F
|
|
|
|
#include <HardwareSerial.h> // for HardwareSerial
|
|
|
|
#include <stdint.h> // for uint8_t
|
|
|
|
#include <string.h> // for memset
|
|
|
|
|
|
|
|
#include "kaleidoscope/Runtime.h" // for Runtime, Runtime_
|
|
|
|
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
|
|
|
|
#include "kaleidoscope/hooks.h" // for Hooks
|
|
|
|
|
|
|
|
#ifdef __AVR__
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return EventHandlerResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
command_[buf_cursor_++] = Runtime.serialPort().read();
|
|
|
|
} while (command_[buf_cursor_ - 1] != SEPARATOR && buf_cursor_ < sizeof(command_) && Runtime.serialPort().available() && (Runtime.serialPort().peek() != NEWLINE));
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FocusSerial::handleHelp(const char *command,
|
|
|
|
const char *help_message) {
|
|
|
|
if (strcmp_P(command, PSTR("help")) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Runtime.serialPort().println((const __FlashStringHelper *)help_message);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventHandlerResult FocusSerial::onFocusEvent(const char *command) {
|
|
|
|
if (handleHelp(command, PSTR("help\ndevice.reset\nplugins")))
|
|
|
|
return EventHandlerResult::OK;
|
|
|
|
|
|
|
|
if (strcmp_P(command, PSTR("device.reset")) == 0) {
|
|
|
|
Runtime.device().rebootBootloader();
|
|
|
|
return EventHandlerResult::EVENT_CONSUMED;
|
|
|
|
}
|
|
|
|
if (strcmp_P(command, PSTR("plugins")) == 0) {
|
|
|
|
kaleidoscope::Hooks::onNameQuery();
|
|
|
|
return EventHandlerResult::EVENT_CONSUMED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EventHandlerResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FocusSerial::printBool(bool b) {
|
|
|
|
Runtime.serialPort().print((b) ? F("true") : F("false"));
|
|
|
|
}
|
|
|
|
|
Standardize namespace block closing comments
This standardizes namespace closing brackets for namespace blocks. Each one is
on its own line, with a comment clearly marking which namespace it closes.
Consecutive lines closing namespace blocks have no whitespace between them, but
there is one blank line before and after a set of namespace block closing lines.
To generate the namespace comments, I used clang-format, with
`FixNamespaceComments: true`. But since clang-format can't exactly duplicate
our astyle formatting, it made lots of other changes, too. To isolate the
namespace comments from the other formatting changes, I first ran clang-format
with `FixNamespaceComments: false`, committed those changes, then ran it again
to generate the namespace comments. Then I stashed the namespace comments,
reset `HEAD` to remove the other changes, applied the stashed namespace
comments, and committed the results (after examining them and making a few minor
adjustments by hand).
Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
3 years ago
|
|
|
} // namespace plugin
|
|
|
|
} // namespace kaleidoscope
|
|
|
|
|
|
|
|
kaleidoscope::plugin::FocusSerial Focus;
|