|
|
|
/* -*- 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Arduino.h> // for __FlashStringHelper
|
|
|
|
#include <HardwareSerial.h> // for HardwareSerial
|
|
|
|
#include <stdint.h> // for uint8_t, uint16_t
|
|
|
|
|
|
|
|
#include "kaleidoscope/Runtime.h" // for Runtime, Runtime_
|
|
|
|
#include "kaleidoscope/device/device.h" // for cRGB
|
|
|
|
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
|
|
|
|
#include "kaleidoscope/key_defs.h" // for Key
|
|
|
|
#include "kaleidoscope/plugin.h" // for Plugin
|
|
|
|
|
|
|
|
namespace kaleidoscope {
|
|
|
|
namespace plugin {
|
|
|
|
class FocusSerial : public kaleidoscope::Plugin {
|
|
|
|
public:
|
|
|
|
FocusSerial(void) {}
|
|
|
|
|
|
|
|
static constexpr char COMMENT = '#';
|
|
|
|
static constexpr char SEPARATOR = ' ';
|
|
|
|
static constexpr char NEWLINE = '\n';
|
|
|
|
|
|
|
|
bool handleHelp(const char *command,
|
|
|
|
const char *help_message);
|
|
|
|
|
|
|
|
EventHandlerResult sendName(const __FlashStringHelper *name) {
|
|
|
|
Runtime.serialPort().print(name);
|
|
|
|
delayAfterPrint();
|
|
|
|
Runtime.serialPort().print(NEWLINE);
|
|
|
|
delayAfterPrint();
|
|
|
|
return EventHandlerResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {}
|
|
|
|
void send(const cRGB color) {
|
|
|
|
send(color.r, color.g, color.b);
|
|
|
|
}
|
|
|
|
void send(const Key key) {
|
|
|
|
send(key.getRaw());
|
|
|
|
}
|
|
|
|
void send(const bool b) {
|
|
|
|
printBool(b);
|
|
|
|
delayAfterPrint();
|
|
|
|
Runtime.serialPort().print(SEPARATOR);
|
|
|
|
delayAfterPrint();
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void send(V v) {
|
|
|
|
Runtime.serialPort().print(v);
|
|
|
|
delayAfterPrint();
|
|
|
|
Runtime.serialPort().print(SEPARATOR);
|
|
|
|
delayAfterPrint();
|
|
|
|
}
|
|
|
|
template<typename Var, typename... Vars>
|
|
|
|
void send(Var v, Vars... vars) {
|
|
|
|
send(v);
|
|
|
|
send(vars...);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendRaw() {}
|
|
|
|
template<typename Var, typename... Vars>
|
|
|
|
void sendRaw(Var v, Vars... vars) {
|
|
|
|
Runtime.serialPort().print(v);
|
|
|
|
delayAfterPrint();
|
|
|
|
sendRaw(vars...);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char peek() {
|
|
|
|
return Runtime.serialPort().peek();
|
|
|
|
}
|
|
|
|
|
|
|
|
void read(Key &key) {
|
|
|
|
key.setRaw(Runtime.serialPort().parseInt());
|
|
|
|
}
|
|
|
|
void read(cRGB &color) {
|
|
|
|
color.r = Runtime.serialPort().parseInt();
|
|
|
|
color.g = Runtime.serialPort().parseInt();
|
|
|
|
color.b = Runtime.serialPort().parseInt();
|
|
|
|
}
|
|
|
|
void read(uint8_t &u8) {
|
|
|
|
u8 = Runtime.serialPort().parseInt();
|
|
|
|
}
|
|
|
|
void read(uint16_t &u16) {
|
|
|
|
u16 = Runtime.serialPort().parseInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEOL() {
|
|
|
|
return Runtime.serialPort().peek() == NEWLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Hooks */
|
|
|
|
EventHandlerResult afterEachCycle();
|
|
|
|
EventHandlerResult onFocusEvent(const char *command);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static char command_[32];
|
|
|
|
static uint8_t buf_cursor_;
|
|
|
|
static void printBool(bool b);
|
|
|
|
|
|
|
|
// This is a hacky workaround for the host seemingly dropping characters
|
|
|
|
// when a client spams its serial port too quickly
|
|
|
|
// Verified on GD32 and macOS 12.3 2022-03-29
|
|
|
|
static constexpr uint8_t focus_delay_us_after_character_ = 100;
|
|
|
|
static void delayAfterPrint() { delayMicroseconds(focus_delay_us_after_character_); }
|
|
|
|
};
|
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
|
|
|
|
|
|
|
|
extern kaleidoscope::plugin::FocusSerial Focus;
|