A more efficient way to handle onFocusEvent commands

In the past, we were using `Focus.handleHelp()` to see if we're handling a
`help` command, and print the commands supported by the handler. We also used
`strcmp_P` directly to compare (parts of) our input against command supported by
the handler. This approach works, but had multiple major disadvantages: it
duplicated strings between `handleHelp` and the `strcmp_P` calls, and it relied
on fragile substring pointers to save space.

To replace all that, this patch implements a different approach. Help handling
is split between a check (`Focus.inputMatchesHelp()`) and a
reply (`Focus.printHelp()`), the latter of which takes a list of `PSTR()`
strings, rather than one single string. This allows us  to reuse the same
strings for comparing against the handler's input.

The new approach no longer uses the fragile substring pointers, nor does it use
`strcmp_P` directly, but goes through a wrapper (`Focus.inputMatchesCommand()`)
instead.

These changes lead to a more readable pattern. While we do use longer strings as
a result, there is less duplication, and the new patterns also require less
code, so we end up with saving space, at least on AVR devices.

The old methods are still available and usable, but they're deprecated.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/1231/head
Gergely Nagy 2 years ago
parent a9f4f16e31
commit 8ac4d9f5c1
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -47,10 +47,10 @@ class FocusTestCommand : public Plugin {
EventHandlerResult onFocusEvent(const char *command) { EventHandlerResult onFocusEvent(const char *command) {
const char *cmd = PSTR("test"); const char *cmd = PSTR("test");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) == 0) { if (::Focus.inputMatchesCommand(command, cmd)) {
::Focus.send(F("ok!")); ::Focus.send(F("ok!"));
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
@ -64,7 +64,8 @@ class FocusHelpCommand : public Plugin {
FocusHelpCommand() {} FocusHelpCommand() {}
EventHandlerResult onFocusEvent(const char *command) { EventHandlerResult onFocusEvent(const char *command) {
::Focus.handleHelp(command, PSTR("help")); if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(PSTR("help"));
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/AutoShift.h" // IWYU pragma: associated #include "kaleidoscope/plugin/AutoShift.h" // IWYU pragma: associated
#include <Arduino.h> // for PSTR, strcmp_P, strncmp_P #include <Arduino.h> // for PSTR
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t #include <stdint.h> // for uint8_t, uint16_t
@ -54,18 +54,18 @@ EventHandlerResult AutoShiftConfig::onFocusEvent(const char *command) {
CATEGORIES, CATEGORIES,
} subCommand; } subCommand;
if (::Focus.handleHelp(command, PSTR("autoshift.enabled\r\n" const char *cmd_enabled = PSTR("autoshift.enabled");
"autoshift.timeout\r\n" const char *cmd_timeout = PSTR("autoshift.timeout");
"autoshift.categories"))) const char *cmd_categories = PSTR("autoshift.categories");
return EventHandlerResult::OK;
if (strncmp_P(command, PSTR("autoshift."), 10) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_enabled, cmd_timeout, cmd_categories);
if (strcmp_P(command + 10, PSTR("enabled")) == 0)
if (::Focus.inputMatchesCommand(command, cmd_enabled))
subCommand = ENABLED; subCommand = ENABLED;
else if (strcmp_P(command + 10, PSTR("timeout")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_timeout))
subCommand = TIMEOUT; subCommand = TIMEOUT;
else if (strcmp_P(command + 10, PSTR("categories")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_categories))
subCommand = CATEGORIES; subCommand = CATEGORIES;
else else
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -78,10 +78,10 @@ EventHandlerResult DefaultColormap::onFocusEvent(const char *command) {
const char *cmd = PSTR("colormap.install"); const char *cmd = PSTR("colormap.install");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0) if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
install(); install();

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/DefaultLEDModeConfig.h" #include "kaleidoscope/plugin/DefaultLEDModeConfig.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t #include <stdint.h> // for uint8_t, uint16_t
@ -51,10 +51,10 @@ EventHandlerResult DefaultLEDModeConfig::onSetup() {
EventHandlerResult DefaultLEDModeConfig::onFocusEvent(const char *command) { EventHandlerResult DefaultLEDModeConfig::onFocusEvent(const char *command) {
const char *cmd = PSTR("led_mode.default"); const char *cmd = PSTR("led_mode.default");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0) if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {

@ -16,7 +16,7 @@
#include "kaleidoscope/plugin/DynamicMacros.h" #include "kaleidoscope/plugin/DynamicMacros.h"
#include <Arduino.h> // for delay, PSTR, strcmp_P, F, __FlashStri... #include <Arduino.h> // for delay, PSTR, F, __FlashStri...
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <Kaleidoscope-Ranges.h> // for DYNAMIC_MACRO_FIRST, DYNAMIC_MACRO_LAST #include <Kaleidoscope-Ranges.h> // for DYNAMIC_MACRO_FIRST, DYNAMIC_MACRO_LAST
@ -215,13 +215,13 @@ EventHandlerResult DynamicMacros::onNameQuery() {
} }
EventHandlerResult DynamicMacros::onFocusEvent(const char *command) { EventHandlerResult DynamicMacros::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("macros.map\r\nmacros.trigger"))) const char *cmd_map = PSTR("macros.map");
return EventHandlerResult::OK; const char *cmd_trigger = PSTR("macros.trigger");
if (strncmp_P(command, PSTR("macros."), 7) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_map, cmd_trigger);
if (strcmp_P(command + 7, PSTR("map")) == 0) { if (::Focus.inputMatchesCommand(command, cmd_map)) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
for (uint16_t i = 0; i < storage_size_; i++) { for (uint16_t i = 0; i < storage_size_; i++) {
uint8_t b; uint8_t b;
@ -240,17 +240,18 @@ EventHandlerResult DynamicMacros::onFocusEvent(const char *command) {
Runtime.storage().commit(); Runtime.storage().commit();
macro_count_ = updateDynamicMacroCache(); macro_count_ = updateDynamicMacroCache();
} }
} return EventHandlerResult::EVENT_CONSUMED;
} else if (::Focus.inputMatchesCommand(command, cmd_trigger)) {
if (strcmp_P(command + 7, PSTR("trigger")) == 0) {
uint8_t id = 0; uint8_t id = 0;
::Focus.read(id); ::Focus.read(id);
play(id); play(id);
}
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
return EventHandlerResult::OK;
}
// public // public
void DynamicMacros::reserve_storage(uint16_t size) { void DynamicMacros::reserve_storage(uint16_t size) {
storage_base_ = ::EEPROMSettings.requestSlice(size); storage_base_ = ::EEPROMSettings.requestSlice(size);

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/DynamicTapDance.h" #include "kaleidoscope/plugin/DynamicTapDance.h"
#include <Arduino.h> // for PSTR, F, __FlashStringHelper, strcmp_P #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint16_t, uint8_t #include <stdint.h> // for uint16_t, uint8_t
@ -94,13 +94,14 @@ EventHandlerResult DynamicTapDance::onNameQuery() {
} }
EventHandlerResult DynamicTapDance::onFocusEvent(const char *command) { EventHandlerResult DynamicTapDance::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("tapdance.map"))) const char *cmd_map = PSTR("tapdance.map");
return EventHandlerResult::OK;
if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_map);
if (strncmp_P(command, PSTR("tapdance."), 9) != 0) if (!::Focus.inputMatchesCommand(command, cmd_map))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (strcmp_P(command + 9, PSTR("map")) == 0) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
for (uint16_t i = 0; i < storage_size_; i += 2) { for (uint16_t i = 0; i < storage_size_; i += 2) {
Key k; Key k;
@ -120,7 +121,6 @@ EventHandlerResult DynamicTapDance::onFocusEvent(const char *command) {
Runtime.storage().commit(); Runtime.storage().commit();
updateDynamicTapDanceCache(); updateDynamicTapDanceCache();
} }
}
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/EEPROM-Keymap-Programmer.h" #include "kaleidoscope/plugin/EEPROM-Keymap-Programmer.h"
#include <Arduino.h> // for PSTR, strcmp_P #include <Arduino.h> // for PSTR
#include <Kaleidoscope-EEPROM-Keymap.h> // for EEPROMKeymap #include <Kaleidoscope-EEPROM-Keymap.h> // for EEPROMKeymap
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint16_t, uint8_t #include <stdint.h> // for uint16_t, uint8_t
@ -109,10 +109,10 @@ EventHandlerResult EEPROMKeymapProgrammer::onKeyEvent(KeyEvent &event) {
EventHandlerResult EEPROMKeymapProgrammer::onFocusEvent(const char *command) { EventHandlerResult EEPROMKeymapProgrammer::onFocusEvent(const char *command) {
const char *cmd = PSTR("keymap.toggleProgrammer"); const char *cmd = PSTR("keymap.toggleProgrammer");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0) if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (state_ == INACTIVE) if (state_ == INACTIVE)

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/EEPROM-Keymap.h" #include "kaleidoscope/plugin/EEPROM-Keymap.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t #include <stdint.h> // for uint8_t, uint16_t
@ -102,13 +102,14 @@ void EEPROMKeymap::dumpKeymap(uint8_t layers, Key (*getkey)(uint8_t, KeyAddr)) {
} }
EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) { EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("keymap.custom\r\nkeymap.default\r\nkeymap.onlyCustom"))) const char *cmd_custom = PSTR("keymap.custom");
return EventHandlerResult::OK; const char *cmd_default = PSTR("keymap.default");
const char *cmd_onlyCustom = PSTR("keymap.onlyCustom");
if (strncmp_P(command, PSTR("keymap."), 7) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_custom, cmd_default, cmd_onlyCustom);
if (strcmp_P(command + 7, PSTR("onlyCustom")) == 0) { if (::Focus.inputMatchesCommand(command, cmd_onlyCustom)) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
::Focus.send((uint8_t)::EEPROMSettings.ignoreHardcodedLayers()); ::Focus.send((uint8_t)::EEPROMSettings.ignoreHardcodedLayers());
} else { } else {
@ -128,7 +129,7 @@ EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
if (strcmp_P(command + 7, PSTR("default")) == 0) { if (::Focus.inputMatchesCommand(command, cmd_default)) {
// By using a cast to the appropriate function type, // By using a cast to the appropriate function type,
// tell the compiler which overload of getKeyFromPROGMEM // tell the compiler which overload of getKeyFromPROGMEM
// we actully want. // we actully want.
@ -138,7 +139,7 @@ EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
if (strcmp_P(command + 7, PSTR("custom")) != 0) if (!::Focus.inputMatchesCommand(command, cmd_custom))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/EEPROM-Settings.h" #include "kaleidoscope/plugin/EEPROM-Settings.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint16_t, uint8_t #include <stdint.h> // for uint16_t, uint8_t
@ -164,19 +164,21 @@ EventHandlerResult FocusSettingsCommand::onFocusEvent(const char *command) {
GET_CRC, GET_CRC,
} sub_command; } sub_command;
if (::Focus.handleHelp(command, PSTR("settings.defaultLayer\r\nsettings.valid?\r\nsettings.version\r\nsettings.crc"))) const char *cmd_defaultLayer = PSTR("settings.defaultLayer");
return EventHandlerResult::OK; const char *cmd_isValid = PSTR("settings.valid?");
const char *cmd_version = PSTR("settings.version");
const char *cmd_crc = PSTR("settings.crc");
if (strncmp_P(command, PSTR("settings."), 9) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_defaultLayer, cmd_isValid, cmd_version, cmd_crc);
if (strcmp_P(command + 9, PSTR("defaultLayer")) == 0) if (::Focus.inputMatchesCommand(command, cmd_defaultLayer))
sub_command = DEFAULT_LAYER; sub_command = DEFAULT_LAYER;
else if (strcmp_P(command + 9, PSTR("valid?")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_isValid))
sub_command = IS_VALID; sub_command = IS_VALID;
else if (strcmp_P(command + 9, PSTR("version")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_version))
sub_command = GET_VERSION; sub_command = GET_VERSION;
else if (strcmp_P(command + 9, PSTR("crc")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_crc))
sub_command = GET_CRC; sub_command = GET_CRC;
else else
return EventHandlerResult::OK; return EventHandlerResult::OK;
@ -213,14 +215,18 @@ EventHandlerResult FocusEEPROMCommand::onFocusEvent(const char *command) {
ERASE, ERASE,
} sub_command; } sub_command;
if (::Focus.handleHelp(command, PSTR("eeprom.contents\r\neeprom.free\r\neeprom.erase"))) const char *cmd_contents = PSTR("eeprom.contents");
return EventHandlerResult::OK; const char *cmd_free = PSTR("eeprom.free");
const char *cmd_erase = PSTR("eeprom.erase");
if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_contents, cmd_free, cmd_erase);
if (strcmp_P(command, PSTR("eeprom.contents")) == 0) if (::Focus.inputMatchesCommand(command, cmd_contents))
sub_command = CONTENTS; sub_command = CONTENTS;
else if (strcmp_P(command, PSTR("eeprom.free")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_free))
sub_command = FREE; sub_command = FREE;
else if (strcmp_P(command, PSTR("eeprom.erase")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_erase))
sub_command = ERASE; sub_command = ERASE;
else else
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/Escape-OneShot.h" // IWYU pragma: associated #include "kaleidoscope/plugin/Escape-OneShot.h" // IWYU pragma: associated
#include <Arduino.h> // for PSTR, F, __FlashStringHelper, strcmp_P #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
@ -49,10 +49,11 @@ EventHandlerResult EscapeOneShotConfig::onNameQuery() {
} }
EventHandlerResult EscapeOneShotConfig::onFocusEvent(const char *command) { EventHandlerResult EscapeOneShotConfig::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("escape_oneshot.cancel_key"))) const char *cmd_cancel_key = PSTR("escape_oneshot.cancel_key");
return EventHandlerResult::OK; if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_cancel_key);
if (strcmp_P(command, PSTR("escape_oneshot.cancel_key")) != 0) if (!::Focus.inputMatchesCommand(command, cmd_cancel_key))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/FingerPainter.h" #include "kaleidoscope/plugin/FingerPainter.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <Kaleidoscope-LED-Palette-Theme.h> // for LEDPaletteTheme #include <Kaleidoscope-LED-Palette-Theme.h> // for LEDPaletteTheme
#include <stdint.h> // for uint16_t, uint8_t #include <stdint.h> // for uint16_t, uint8_t
@ -100,15 +100,15 @@ EventHandlerResult FingerPainter::onFocusEvent(const char *command) {
CLEAR, CLEAR,
} sub_command; } sub_command;
if (::Focus.handleHelp(command, PSTR("fingerpainter.toggle\r\nfingerpainter.clear"))) const char *cmd_toggle = PSTR("fingerpainter.toggle");
return EventHandlerResult::OK; const char *cmd_clear = PSTR("fingerpainter.clear");
if (strncmp_P(command, PSTR("fingerpainter."), 14) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_toggle, cmd_clear);
if (strcmp_P(command + 14, PSTR("toggle")) == 0) if (::Focus.inputMatchesCommand(command, cmd_toggle))
sub_command = TOGGLE; sub_command = TOGGLE;
else if (strcmp_P(command + 14, PSTR("clear")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_clear))
sub_command = CLEAR; sub_command = CLEAR;
else else
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -53,10 +53,10 @@ EventHandlerResult FirmwareDump::onSetup() {
EventHandlerResult FirmwareDump::onFocusEvent(const char *command) { EventHandlerResult FirmwareDump::onFocusEvent(const char *command) {
const char *cmd = PSTR("firmware.dump"); const char *cmd = PSTR("firmware.dump");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0) if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
uint16_t flash_size = (FLASHEND + 1L); uint16_t flash_size = (FLASHEND + 1L);

@ -21,7 +21,7 @@
#define KALEIDOSCOPE_FIRMWARE_VERSION "0.0.0" #define KALEIDOSCOPE_FIRMWARE_VERSION "0.0.0"
#endif #endif
#include <Arduino.h> // for PSTR, F, __FlashStringHelper, strcmp_P #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include "Kaleidoscope-FocusSerial.h" // for Focus, FocusSerial #include "Kaleidoscope-FocusSerial.h" // for Focus, FocusSerial
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult #include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/plugin.h" // for Plugin #include "kaleidoscope/plugin.h" // for Plugin
@ -32,10 +32,12 @@ namespace plugin {
class FirmwareVersion : public Plugin { class FirmwareVersion : public Plugin {
public: public:
EventHandlerResult onFocusEvent(const char *command) { EventHandlerResult onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("version"))) const char *cmd_version = PSTR("version");
return EventHandlerResult::OK;
if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_version);
if (strcmp_P(command, PSTR("version")) != 0) if (!::Focus.inputMatchesCommand(command, cmd_version))
return EventHandlerResult::OK; return EventHandlerResult::OK;
#ifdef KALEIDOSCOPE_FIRMWARE_VERSION #ifdef KALEIDOSCOPE_FIRMWARE_VERSION

@ -25,7 +25,7 @@ class FocusTestCommand : public Plugin {
} }
EventHandlerResult onFocusEvent(const char *command) { EventHandlerResult onFocusEvent(const char *command) {
if (strcmp_P(command, PSTR("test")) != 0) if (!::Focus.inputMatchesCommand(command, PSTR("test")))
return EventHandlerResult::OK; return EventHandlerResult::OK;
::Focus.send(F("Congratulations, the test command works!")); ::Focus.send(F("Congratulations, the test command works!"));

@ -77,24 +77,19 @@ EventHandlerResult FocusSerial::afterEachCycle() {
return EventHandlerResult::OK; 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) { EventHandlerResult FocusSerial::onFocusEvent(const char *command) {
if (handleHelp(command, PSTR("help\r\ndevice.reset\r\nplugins"))) const char *cmd_help = PSTR("help");
return EventHandlerResult::OK; const char *cmd_reset = PSTR("device.reset");
const char *cmd_plugins = PSTR("plugins");
if (inputMatchesHelp(command))
return printHelp(cmd_help, cmd_reset, cmd_plugins);
if (strcmp_P(command, PSTR("device.reset")) == 0) { if (inputMatchesCommand(command, cmd_reset)) {
Runtime.device().rebootBootloader(); Runtime.device().rebootBootloader();
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
if (strcmp_P(command, PSTR("plugins")) == 0) { if (inputMatchesCommand(command, cmd_plugins)) {
kaleidoscope::Hooks::onNameQuery(); kaleidoscope::Hooks::onNameQuery();
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
@ -102,10 +97,27 @@ EventHandlerResult FocusSerial::onFocusEvent(const char *command) {
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
#ifndef NDEPRECATED
bool FocusSerial::handleHelp(const char *command, const char *help_message) {
if (!inputMatchesHelp(command)) return false;
printHelp(help_message);
return true;
}
#endif
void FocusSerial::printBool(bool b) { void FocusSerial::printBool(bool b) {
Runtime.serialPort().print((b) ? F("true") : F("false")); Runtime.serialPort().print((b) ? F("true") : F("false"));
} }
bool FocusSerial::inputMatchesHelp(const char *input) {
return inputMatchesCommand(input, PSTR("help"));
}
bool FocusSerial::inputMatchesCommand(const char *input, const char *expected) {
return strcmp_P(input, expected) == 0;
}
} // namespace plugin } // namespace plugin
} // namespace kaleidoscope } // namespace kaleidoscope

@ -26,6 +26,15 @@
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult, EventHandlerResult::OK #include "kaleidoscope/event_handler_result.h" // for EventHandlerResult, EventHandlerResult::OK
#include "kaleidoscope/key_defs.h" // for Key #include "kaleidoscope/key_defs.h" // for Key
#include "kaleidoscope/plugin.h" // for Plugin #include "kaleidoscope/plugin.h" // for Plugin
// -----------------------------------------------------------------------------
// Deprecation warning messages
#include "kaleidoscope_internal/deprecations.h" // for DEPRECATED
#define _DEPRECATED_MESSAGE_FOCUS_HANDLEHELP \
"The `Focus.handleHelp()` method is deprecated. Please use\n" \
"`Focus.inputMatchesHelp()` and `Focus.printHelp()` instead.\n" \
"This method will be removed after 2022-12-26."
// -----------------------------------------------------------------------------
// IWYU pragma: no_include "WString.h" // IWYU pragma: no_include "WString.h"
@ -37,8 +46,23 @@ class FocusSerial : public kaleidoscope::Plugin {
static constexpr char SEPARATOR = ' '; static constexpr char SEPARATOR = ' ';
static constexpr char NEWLINE = '\n'; static constexpr char NEWLINE = '\n';
bool handleHelp(const char *command, #ifndef NDEPRECATED
const char *help_message); DEPRECATED(FOCUS_HANDLEHELP)
bool handleHelp(const char *command, const char *help_message);
#endif
bool inputMatchesHelp(const char *input);
bool inputMatchesCommand(const char *input, const char *expected);
EventHandlerResult printHelp() {
return EventHandlerResult::OK;
}
template<typename... Vars>
EventHandlerResult printHelp(const char *h1, Vars... vars) {
Runtime.serialPort().println((const __FlashStringHelper *)h1);
delayAfterPrint();
return printHelp(vars...);
}
EventHandlerResult sendName(const __FlashStringHelper *name) { EventHandlerResult sendName(const __FlashStringHelper *name) {
Runtime.serialPort().print(name); Runtime.serialPort().print(name);

@ -34,23 +34,35 @@ namespace raise {
#endif #endif
EventHandlerResult Focus::onFocusEvent(const char *command) { EventHandlerResult Focus::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("hardware.version\r\nhardware.side_power\r\nhardware.side_ver\r\nhardware.sled_ver\r\nhardware.sled_current\r\nhardware.layout\r\nhardware.joint\r\nhardware.keyscan\r\nhardware.crc_errors\r\nhardware.firmware"))) const char *cmd_version = PSTR("hardware.version");
return EventHandlerResult::OK; const char *cmd_side_power = PSTR("hardware.side_power");
const char *cmd_side_ver = PSTR("hardware.side_ver");
if (strncmp_P(command, PSTR("hardware."), 9) != 0) const char *cmd_sled_ver = PSTR("hardware.sled_ver");
return EventHandlerResult::OK; const char *cmd_sled_current = PSTR("hardware.sled_current");
const char *cmd_layout = PSTR("hardware.layout");
if (strcmp_P(command + 9, PSTR("version")) == 0) { const char *cmd_joint = PSTR("hardware.joint");
const char *cmd_keyscan = PSTR("hardware.keyscan");
const char *cmd_crc_errors = PSTR("hardware.crc_errors");
const char *cmd_firmware = PSTR("hardware.firmware");
if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_version,
cmd_side_power,
cmd_side_ver,
cmd_sled_ver,
cmd_sled_current,
cmd_layout,
cmd_joint,
cmd_keyscan,
cmd_crc_errors,
cmd_firmware);
if (::Focus.inputMatchesCommand(command, cmd_version)) {
::Focus.send("Dygma Raise"); ::Focus.send("Dygma Raise");
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_firmware)) {
if (strcmp_P(command + 9, PSTR("firmware")) == 0) {
::Focus.send(RAISE_FIRMWARE_VERSION); ::Focus.send(RAISE_FIRMWARE_VERSION);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_side_power)) {
if (strcmp_P(command + 9, PSTR("side_power")) == 0) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
::Focus.send(Runtime.device().side.getPower()); ::Focus.send(Runtime.device().side.getPower());
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
@ -60,33 +72,25 @@ EventHandlerResult Focus::onFocusEvent(const char *command) {
Runtime.device().side.setPower(power); Runtime.device().side.setPower(power);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
} } else if (::Focus.inputMatchesCommand(command, cmd_side_ver)) {
if (strcmp_P(command + 9, PSTR("side_ver")) == 0) {
::Focus.send("left:"); ::Focus.send("left:");
::Focus.send(Runtime.device().side.leftVersion()); ::Focus.send(Runtime.device().side.leftVersion());
::Focus.send("\r\nright:"); ::Focus.send("\r\nright:");
::Focus.send(Runtime.device().side.rightVersion()); ::Focus.send(Runtime.device().side.rightVersion());
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_crc_errors)) {
if (strcmp_P(command + 9, PSTR("crc_errors")) == 0) {
::Focus.send("left:"); ::Focus.send("left:");
::Focus.send(Runtime.device().side.leftCRCErrors()); ::Focus.send(Runtime.device().side.leftCRCErrors());
::Focus.send("\r\nright:"); ::Focus.send("\r\nright:");
::Focus.send(Runtime.device().side.rightCRCErrors()); ::Focus.send(Runtime.device().side.rightCRCErrors());
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_sled_ver)) {
if (strcmp_P(command + 9, PSTR("sled_ver")) == 0) {
::Focus.send("left:"); ::Focus.send("left:");
::Focus.send(Runtime.device().side.leftSLEDVersion()); ::Focus.send(Runtime.device().side.leftSLEDVersion());
::Focus.send("\r\nright:"); ::Focus.send("\r\nright:");
::Focus.send(Runtime.device().side.rightSLEDVersion()); ::Focus.send(Runtime.device().side.rightSLEDVersion());
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_sled_current)) {
if (strcmp_P(command + 9, PSTR("sled_current")) == 0) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
::Focus.send("left:"); ::Focus.send("left:");
::Focus.send(Runtime.device().side.leftSLEDCurrent()); ::Focus.send(Runtime.device().side.leftSLEDCurrent());
@ -99,20 +103,14 @@ EventHandlerResult Focus::onFocusEvent(const char *command) {
Runtime.device().side.setSLEDCurrent(current); Runtime.device().side.setSLEDCurrent(current);
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} }
} } else if (::Focus.inputMatchesCommand(command, cmd_layout)) {
if (strcmp_P(command + 9, PSTR("layout")) == 0) {
static const auto ANSI = Runtime.device().settings.Layout::ANSI; static const auto ANSI = Runtime.device().settings.Layout::ANSI;
::Focus.send(Runtime.device().settings.layout() == ANSI ? "ANSI" : "ISO"); ::Focus.send(Runtime.device().settings.layout() == ANSI ? "ANSI" : "ISO");
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_joint)) {
if (strcmp_P(command + 9, PSTR("joint")) == 0) {
::Focus.send(Runtime.device().settings.joint()); ::Focus.send(Runtime.device().settings.joint());
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;
} } else if (::Focus.inputMatchesCommand(command, cmd_keyscan)) {
if (strcmp_P(command + 9, PSTR("keyscan")) == 0) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
::Focus.send(Runtime.device().settings.keyscanInterval()); ::Focus.send(Runtime.device().settings.keyscanInterval());
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;

@ -35,11 +35,12 @@ class SideFlash : public kaleidoscope::Plugin {
public: public:
EventHandlerResult onFocusEvent(const char *command) { EventHandlerResult onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("hardware.flash_left_side\nhardware.flash_right_side\nhardware.verify_left_side\nhardware.verify_right_side"))) const char *cmd_flash_left = PSTR("hardware.flash_left_side");
return EventHandlerResult::OK; const char *cmd_flash_right = PSTR("hardware.flash_right_side");
const char *cmd_verify_left = PSTR("hardware.verify_left_side");
if (strncmp_P(command, PSTR("hardware."), 9) != 0) const char *cmd_verify_right = PSTR("hardware.verify_right_side");
return EventHandlerResult::OK; if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_flash_left, cmd_flash_right, cmd_verify_left, cmd_verify_right);
auto sideFlasher = Runtime.device().sideFlasher(); auto sideFlasher = Runtime.device().sideFlasher();
uint8_t left_boot_address = Runtime.device().side.left_boot_address; uint8_t left_boot_address = Runtime.device().side.left_boot_address;
@ -50,16 +51,16 @@ class SideFlash : public kaleidoscope::Plugin {
} sub_command; } sub_command;
uint8_t address = 0; uint8_t address = 0;
if (strcmp_P(command + 9, PSTR("flash_left_side")) == 0) { if (::Focus.inputMatchesCommand(command, cmd_flash_left)) {
sub_command = FLASH; sub_command = FLASH;
address = left_boot_address; address = left_boot_address;
} else if (strcmp_P(command + 9, PSTR("flash_right_side")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_flash_right)) {
sub_command = FLASH; sub_command = FLASH;
address = right_boot_address; address = right_boot_address;
} else if (strcmp_P(command + 9, PSTR("verify_left_side")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_verify_left)) {
sub_command = VERIFY; sub_command = VERIFY;
address = left_boot_address; address = left_boot_address;
} else if (strcmp_P(command + 9, PSTR("verify_right_side")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_verify_right)) {
sub_command = VERIFY; sub_command = VERIFY;
address = right_boot_address; address = right_boot_address;
} else { } else {

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/HostOS-Focus.h" #include "kaleidoscope/plugin/HostOS-Focus.h"
#include <Arduino.h> // for PSTR, strcmp_P #include <Arduino.h> // for PSTR
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t #include <stdint.h> // for uint8_t
@ -30,9 +30,10 @@ namespace plugin {
EventHandlerResult FocusHostOSCommand::onFocusEvent(const char *command) { EventHandlerResult FocusHostOSCommand::onFocusEvent(const char *command) {
const char *cmd = PSTR("hostos.type"); const char *cmd = PSTR("hostos.type");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0)
if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {

@ -18,7 +18,7 @@
#include "kaleidoscope/plugin/IdleLEDs.h" #include "kaleidoscope/plugin/IdleLEDs.h"
#include <Arduino.h> // for F, PSTR, __FlashStringHelper, strcmp_P #include <Arduino.h> // for F, PSTR, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint32_t, uint16_t #include <stdint.h> // for uint32_t, uint16_t
@ -100,10 +100,10 @@ void PersistentIdleLEDs::setIdleTimeoutSeconds(uint32_t new_limit) {
EventHandlerResult PersistentIdleLEDs::onFocusEvent(const char *command) { EventHandlerResult PersistentIdleLEDs::onFocusEvent(const char *command) {
const char *cmd = PSTR("idleleds.time_limit"); const char *cmd = PSTR("idleleds.time_limit");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0) if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/LED-Palette-Theme.h" #include "kaleidoscope/plugin/LED-Palette-Theme.h"
#include <Arduino.h> // for strcmp_P, PSTR #include <Arduino.h> // for PSTR
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t #include <stdint.h> // for uint8_t, uint16_t
@ -128,10 +128,10 @@ EventHandlerResult LEDPaletteTheme::onFocusEvent(const char *command) {
const char *cmd = PSTR("palette"); const char *cmd = PSTR("palette");
if (::Focus.handleHelp(command, cmd)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd);
if (strcmp_P(command, cmd) != 0) if (!::Focus.inputMatchesCommand(command, cmd))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
@ -166,10 +166,10 @@ EventHandlerResult LEDPaletteTheme::themeFocusEvent(const char *command,
if (!Runtime.has_leds) if (!Runtime.has_leds)
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.handleHelp(command, expected_command)) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(expected_command);
if (strcmp_P(command, expected_command) != 0) if (!::Focus.inputMatchesCommand(command, expected_command))
return EventHandlerResult::OK; return EventHandlerResult::OK;
uint16_t max_index = (max_themes * Runtime.device().led_count) / 2; uint16_t max_index = (max_themes * Runtime.device().led_count) / 2;

@ -18,7 +18,7 @@
#include "kaleidoscope/plugin/LayerFocus.h" #include "kaleidoscope/plugin/LayerFocus.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t #include <stdint.h> // for uint8_t
@ -33,38 +33,44 @@ EventHandlerResult LayerFocus::onNameQuery() {
} }
EventHandlerResult LayerFocus::onFocusEvent(const char *command) { EventHandlerResult LayerFocus::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("layer.activate\r\nlayer.deactivate\r\nlayer.isActive" const char *cmd_activate = PSTR("layer.activate");
"\r\nlayer.moveTo\r\nlayer.state"))) const char *cmd_deactivate = PSTR("layer.deactivate");
return EventHandlerResult::OK; const char *cmd_isActive = PSTR("layer.isActive");
const char *cmd_moveTo = PSTR("layer.moveTo");
const char *cmd_state = PSTR("layer.state");
if (strncmp_P(command, PSTR("layer."), 6) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_activate,
cmd_deactivate,
cmd_isActive,
cmd_moveTo,
cmd_state);
if (strcmp_P(command + 6, PSTR("activate")) == 0) { if (::Focus.inputMatchesCommand(command, cmd_activate)) {
if (!::Focus.isEOL()) { if (!::Focus.isEOL()) {
uint8_t layer; uint8_t layer;
::Focus.read(layer); ::Focus.read(layer);
::Layer.activate(layer); ::Layer.activate(layer);
} }
} else if (strcmp_P(command + 6, PSTR("deactivate")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_deactivate)) {
if (!::Focus.isEOL()) { if (!::Focus.isEOL()) {
uint8_t layer; uint8_t layer;
::Focus.read(layer); ::Focus.read(layer);
::Layer.deactivate(layer); ::Layer.deactivate(layer);
} }
} else if (strcmp_P(command + 6, PSTR("isActive")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_isActive)) {
if (!::Focus.isEOL()) { if (!::Focus.isEOL()) {
uint8_t layer; uint8_t layer;
::Focus.read(layer); ::Focus.read(layer);
::Focus.send(::Layer.isActive(layer)); ::Focus.send(::Layer.isActive(layer));
} }
} else if (strcmp_P(command + 6, PSTR("moveTo")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_moveTo)) {
if (!::Focus.isEOL()) { if (!::Focus.isEOL()) {
uint8_t layer; uint8_t layer;
::Focus.read(layer); ::Focus.read(layer);
::Layer.move(layer); ::Layer.move(layer);
} }
} else if (strcmp_P(command + 6, PSTR("state")) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_state)) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i = 0; i < 32; i++) {
::Focus.send(::Layer.isActive(i) ? 1 : 0); ::Focus.send(::Layer.isActive(i) ? 1 : 0);
@ -80,6 +86,8 @@ EventHandlerResult LayerFocus::onFocusEvent(const char *command) {
::Layer.activate(i); ::Layer.activate(i);
} }
} }
} else {
return EventHandlerResult::OK;
} }
return EventHandlerResult::EVENT_CONSUMED; return EventHandlerResult::EVENT_CONSUMED;

@ -16,7 +16,7 @@
#include "kaleidoscope/plugin/LayerNames.h" #include "kaleidoscope/plugin/LayerNames.h"
#include <Arduino.h> // for delay, PSTR, strcmp_P, F, __FlashStri... #include <Arduino.h> // for delay, PSTR, F, __FlashStri...
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include "kaleidoscope/Runtime.h" // for Runtime, Runtime_ #include "kaleidoscope/Runtime.h" // for Runtime, Runtime_
@ -32,10 +32,12 @@ EventHandlerResult LayerNames::onNameQuery() {
} }
EventHandlerResult LayerNames::onFocusEvent(const char *command) { EventHandlerResult LayerNames::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("keymap.layerNames"))) const char *cmd_layerNames = PSTR("keymap.layerNames");
return EventHandlerResult::OK;
if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_layerNames);
if (strcmp_P(command, PSTR("keymap.layerNames")) != 0) if (!::Focus.inputMatchesCommand(command, cmd_layerNames))
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.isEOL()) { if (::Focus.isEOL()) {

@ -18,7 +18,7 @@
#include "kaleidoscope/plugin/PersistentLEDMode.h" #include "kaleidoscope/plugin/PersistentLEDMode.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t #include <stdint.h> // for uint8_t, uint16_t

@ -19,7 +19,7 @@
#include "SpaceCadet.h" #include "SpaceCadet.h"
#include "kaleidoscope/plugin/SpaceCadet.h" #include "kaleidoscope/plugin/SpaceCadet.h"
#include <Arduino.h> // for F, __FlashStringHelper #include <Arduino.h> // for PSTR
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <stdint.h> // for uint16_t, int8_t, uint8_t #include <stdint.h> // for uint16_t, int8_t, uint8_t
@ -51,15 +51,10 @@ EventHandlerResult SpaceCadetConfig::onFocusEvent(const char *command) {
const char *cmd_mode = PSTR("spacecadet.mode"); const char *cmd_mode = PSTR("spacecadet.mode");
const char *cmd_timeout = PSTR("spacecadet.timeout"); const char *cmd_timeout = PSTR("spacecadet.timeout");
// Note: These two calls are intentionally separate, because we want the side if (::Focus.inputMatchesHelp(command))
// effects. If we were to use `||`, only one of them would run. return ::Focus.printHelp(cmd_mode, cmd_timeout);
bool help_handled = ::Focus.handleHelp(command, cmd_mode);
help_handled |= ::Focus.handleHelp(command, cmd_timeout);
if (help_handled) if (::Focus.inputMatchesCommand(command, cmd_mode)) {
return EventHandlerResult::OK;
if (strcmp_P(command, cmd_mode) == 0) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
::Focus.send(::SpaceCadet.settings_.mode); ::Focus.send(::SpaceCadet.settings_.mode);
} else { } else {
@ -81,7 +76,7 @@ EventHandlerResult SpaceCadetConfig::onFocusEvent(const char *command) {
Runtime.storage().put(settings_base_, ::SpaceCadet.settings_); Runtime.storage().put(settings_base_, ::SpaceCadet.settings_);
Runtime.storage().commit(); Runtime.storage().commit();
} }
} else if (strcmp_P(command, cmd_timeout) == 0) { } else if (::Focus.inputMatchesCommand(command, cmd_timeout)) {
if (::Focus.isEOL()) { if (::Focus.isEOL()) {
::Focus.send(::SpaceCadet.settings_.timeout); ::Focus.send(::SpaceCadet.settings_.timeout);
} else { } else {

@ -17,7 +17,7 @@
#include "kaleidoscope/plugin/TypingBreaks.h" #include "kaleidoscope/plugin/TypingBreaks.h"
#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper #include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings #include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint32_t, uint16_t #include <stdint.h> // for uint32_t, uint16_t
@ -148,24 +148,27 @@ EventHandlerResult TypingBreaks::onFocusEvent(const char *command) {
RIGHT_MAX, RIGHT_MAX,
} subCommand; } subCommand;
if (::Focus.handleHelp(command, PSTR("typingbreaks.idleTimeLimit\r\n" const char *cmd_idleTimeLimit = PSTR("typingbreaks.idleTimeLimit");
"typingbreaks.lockTimeOut\r\n" const char *cmd_lockTimeOut = PSTR("typingbreaks.lockTimeOut");
"typingbreaks.lockLength\r\n" const char *cmd_lockLength = PSTR("typingbreaks.lockLength");
"typingbreaks.leftMaxKeys\r\n" const char *cmd_leftMaxKeys = PSTR("typingbreaks.leftMaxKeys");
"typingbreaks.rightMaxKeys"))) const char *cmd_rightMaxKeys = PSTR("typingbreaks.rightMaxKeys");
return EventHandlerResult::OK; if (::Focus.inputMatchesHelp(command))
return ::Focus.printHelp(cmd_idleTimeLimit,
if (strncmp_P(command, PSTR("typingbreaks."), 13) != 0) cmd_lockTimeOut,
return EventHandlerResult::OK; cmd_lockLength,
if (strcmp_P(command + 13, PSTR("idleTimeLimit")) == 0) cmd_leftMaxKeys,
cmd_rightMaxKeys);
if (::Focus.inputMatchesCommand(command, cmd_idleTimeLimit))
subCommand = IDLE_TIME_LIMIT; subCommand = IDLE_TIME_LIMIT;
else if (strcmp_P(command + 13, PSTR("lockTimeOut")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_lockTimeOut))
subCommand = LOCK_TIMEOUT; subCommand = LOCK_TIMEOUT;
else if (strcmp_P(command + 13, PSTR("lockLength")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_lockLength))
subCommand = LOCK_LENGTH; subCommand = LOCK_LENGTH;
else if (strcmp_P(command + 13, PSTR("leftMaxKeys")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_leftMaxKeys))
subCommand = LEFT_MAX; subCommand = LEFT_MAX;
else if (strcmp_P(command + 13, PSTR("rightMaxKeys")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_rightMaxKeys))
subCommand = RIGHT_MAX; subCommand = RIGHT_MAX;
else else
return EventHandlerResult::OK; return EventHandlerResult::OK;

@ -16,7 +16,7 @@
#include "kaleidoscope/plugin/LEDControl.h" #include "kaleidoscope/plugin/LEDControl.h"
#include <Arduino.h> // for PSTR, strcmp_P, strncmp_P #include <Arduino.h> // for PSTR, strncmp_P
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial #include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include "kaleidoscope/KeyAddrMap.h" // for KeyAddrMap<>::Iterator, KeyAddrMap #include "kaleidoscope/KeyAddrMap.h" // for KeyAddrMap<>::Iterator, KeyAddrMap
@ -224,24 +224,28 @@ EventHandlerResult FocusLEDCommand::onFocusEvent(const char *command) {
if (!Runtime.has_leds) if (!Runtime.has_leds)
return EventHandlerResult::OK; return EventHandlerResult::OK;
if (::Focus.handleHelp(command, PSTR("led.at\r\n" const char *cmd_at = PSTR("led.at");
"led.setAll\r\n" const char *cmd_setAll = PSTR("led.setAll");
"led.mode\r\n" const char *cmd_mode = PSTR("led.mode");
"led.brightness\r\n" const char *cmd_brightness = PSTR("led.brightness");
"led.theme"))) const char *cmd_theme = PSTR("led.theme");
return EventHandlerResult::OK;
if (strncmp_P(command, PSTR("led."), 4) != 0) if (::Focus.inputMatchesHelp(command))
return EventHandlerResult::OK; return ::Focus.printHelp(cmd_at,
if (strcmp_P(command + 4, PSTR("at")) == 0) cmd_setAll,
cmd_mode,
cmd_brightness,
cmd_theme);
if (::Focus.inputMatchesCommand(command, cmd_at))
subCommand = AT; subCommand = AT;
else if (strcmp_P(command + 4, PSTR("setAll")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_setAll))
subCommand = SETALL; subCommand = SETALL;
else if (strcmp_P(command + 4, PSTR("mode")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_mode))
subCommand = MODE; subCommand = MODE;
else if (strcmp_P(command + 4, PSTR("theme")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_theme))
subCommand = THEME; subCommand = THEME;
else if (strcmp_P(command + 4, PSTR("brightness")) == 0) else if (::Focus.inputMatchesCommand(command, cmd_brightness))
subCommand = BRIGHTNESS; subCommand = BRIGHTNESS;
else else
return EventHandlerResult::OK; return EventHandlerResult::OK;

Loading…
Cancel
Save