A very rough WIP of a potential refactor of focus. It compiles but I

haven't even uploaded it to a device.
pull/1230/head
Jesse Vincent 2 years ago
parent f3fe7c9023
commit 961853e3f3
No known key found for this signature in database
GPG Key ID: 122F5DF7108E4046

@ -38,6 +38,7 @@ uint8_t EEPROMKeymap::progmem_layers_;
EventHandlerResult EEPROMKeymap::onSetup() {
::EEPROMSettings.onSetup();
progmem_layers_ = layer_count;
return EventHandlerResult::OK;
}
@ -45,6 +46,7 @@ EventHandlerResult EEPROMKeymap::onNameQuery() {
return ::Focus.sendName(F("EEPROMKeymap"));
}
void EEPROMKeymap::setup(uint8_t max) {
layer_count = max;
if (::EEPROMSettings.ignoreHardcodedLayers()) {
@ -101,14 +103,22 @@ void EEPROMKeymap::dumpKeymap(uint8_t layers, Key (*getkey)(uint8_t, KeyAddr)) {
}
}
EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("keymap.custom\r\nkeymap.default\r\nkeymap.onlyCustom")))
EventHandlerResult EEPROMKeymap::onFocusEvent(const char *input) {
const char *primary_focus_command_ = PSTR("keymap.");
const char *custom_subcommand_ = PSTR("custom");
const char *default_subcommand_ = PSTR("default");
const char *only_custom_subcommand_ = PSTR("onlyCustom");
if (::Focus.handleHelp(input, 8, primary_focus_command_, custom_subcommand_, Focus.CRLF, primary_focus_command_, default_subcommand_, Focus.CRLF, primary_focus_command_, only_custom_subcommand_))
return EventHandlerResult::OK;
if (strncmp_P(command, PSTR("keymap."), 7) != 0)
if (!::Focus.inputMatchesCommand(input, primary_focus_command_))
return EventHandlerResult::OK;
if (strcmp_P(command + 7, PSTR("onlyCustom")) == 0) {
if (::Focus.inputMatchesSubcommand(input, primary_focus_command_, only_custom_subcommand_)) {
if (::Focus.isEOL()) {
::Focus.send((uint8_t)::EEPROMSettings.ignoreHardcodedLayers());
} else {
@ -128,7 +138,7 @@ EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
return EventHandlerResult::EVENT_CONSUMED;
}
if (strcmp_P(command + 7, PSTR("default")) == 0) {
if (::Focus.inputMatchesSubcommand(input, primary_focus_command_, default_subcommand_)) {
// By using a cast to the appropriate function type,
// tell the compiler which overload of getKeyFromPROGMEM
// we actully want.
@ -138,9 +148,9 @@ EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
return EventHandlerResult::EVENT_CONSUMED;
}
if (strcmp_P(command + 7, PSTR("custom")) != 0)
if (!::Focus.inputMatchesSubcommand(input, primary_focus_command_, custom_subcommand_)) {
return EventHandlerResult::OK;
}
if (::Focus.isEOL()) {
// By using a cast to the appropriate function type,
// tell the compiler which overload of getKey

@ -164,9 +164,17 @@ EventHandlerResult FocusSettingsCommand::onFocusEvent(const char *command) {
GET_CRC,
} sub_command;
if (::Focus.handleHelp(command, PSTR("settings.defaultLayer\r\nsettings.valid?\r\nsettings.version\r\nsettings.crc")))
const char *focus_command_settings_ = PSTR("settings");
const char *focus_command_default_layer_ = PSTR("defaultLayer");
const char *focus_command_is_valid_ = PSTR("valid?");
const char *focus_command_version_ = PSTR("version");
const char *focus_command_crc_ = PSTR("crc");
if (::Focus.handleHelp(command, 11, focus_command_settings_, focus_command_default_layer_, Focus.CRLF, focus_command_settings_, focus_command_is_valid_, Focus.CRLF, focus_command_settings_, focus_command_version_, Focus.CRLF, focus_command_settings_, focus_command_crc_))
return EventHandlerResult::OK;
if (strncmp_P(command, PSTR("settings."), 9) != 0)
return EventHandlerResult::OK;

@ -32,21 +32,22 @@ namespace plugin {
class FirmwareVersion : public Plugin {
public:
EventHandlerResult onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("version")))
const char *focus_command_version_ = PSTR("version");
if (::Focus.handleHelp(command, focus_command_version_))
return EventHandlerResult::OK;
if (strcmp_P(command, PSTR("version")) != 0)
if (::Focus.inputMatchesCommand(command, focus_command_version_)) {
return EventHandlerResult::OK;
#ifdef KALEIDOSCOPE_FIRMWARE_VERSION
::Focus.send(F(KALEIDOSCOPE_FIRMWARE_VERSION));
::Focus.send(F(KALEIDOSCOPE_FIRMWARE_VERSION));
#else
::Focus.send(F("0.0.0"));
::Focus.send(F("0.0.0"));
#endif
return EventHandlerResult::OK;
}
};
return EventHandlerResult::OK;
}
};
} // namespace plugin
} // namespace kaleidoscope

@ -32,6 +32,11 @@
namespace kaleidoscope {
namespace plugin {
EventHandlerResult FocusSerial::onSetup() {
const char *focus_command_help_ = PSTR("help");
return EventHandlerResult::OK;
}
EventHandlerResult FocusSerial::afterEachCycle() {
int c;
// GD32 doesn't currently autoflush the very last packet. So manually flush here
@ -77,24 +82,63 @@ EventHandlerResult FocusSerial::afterEachCycle() {
return EventHandlerResult::OK;
}
bool FocusSerial::inputMatchesCommand(const char *input, const char *to_match) {
if (strncmp_P(input, to_match, strlen_P(to_match)) == 0) {
return true;
} else {
return false;
}
}
bool FocusSerial::inputMatchesSubcommand(const char *input, const char *command_to_match, const char *subcommand_to_match) {
if (strcmp_P(input + strlen_P(command_to_match), subcommand_to_match) == 0) {
return true;
} else {
return false;
}
}
bool FocusSerial::handleHelp(const char *command,
const char *help_message) {
if (strcmp_P(command, PSTR("help")) != 0)
if (!inputMatchesCommand(command, focus_command_help_))
return false;
Runtime.serialPort().println((const __FlashStringHelper *)help_message);
return true;
}
bool FocusSerial::handleHelp(const char *command,
uint8_t help_message_count,
...) {
if (!inputMatchesCommand(command, focus_command_help_))
return false;
va_list messages;
va_start(messages, help_message_count);
for (int i = 0; i < help_message_count; ++i) {
char *message = va_arg(messages, char *);
Runtime.serialPort().print((const __FlashStringHelper *)message);
}
Runtime.serialPort().println();
va_end(messages);
return true;
}
EventHandlerResult FocusSerial::onFocusEvent(const char *command) {
if (handleHelp(command, PSTR("help\r\ndevice.reset\r\nplugins")))
const char *focus_command_device_reset_ = PSTR("device.reset");
const char *focus_command_plugins_ = PSTR("plugins");
if (handleHelp(command, 5, focus_command_help_, Focus.CRLF, focus_command_device_reset_, Focus.CRLF, focus_command_plugins_))
return EventHandlerResult::OK;
if (strcmp_P(command, PSTR("device.reset")) == 0) {
if (inputMatchesCommand(command, focus_command_device_reset_)) {
Runtime.device().rebootBootloader();
return EventHandlerResult::EVENT_CONSUMED;
}
if (strcmp_P(command, PSTR("plugins")) == 0) {
if (inputMatchesCommand(command, focus_command_plugins_)) {
kaleidoscope::Hooks::onNameQuery();
return EventHandlerResult::EVENT_CONSUMED;
}

@ -36,10 +36,20 @@ class FocusSerial : public kaleidoscope::Plugin {
static constexpr char COMMENT = '#';
static constexpr char SEPARATOR = ' ';
static constexpr char NEWLINE = '\n';
static constexpr char CR = '\r';
static constexpr char *CRLF = "\r\n";
bool handleHelp(const char *command,
const char *help_message);
bool handleHelp(const char *command,
uint8_t help_message_count,
...);
bool inputMatchesCommand(const char *input, const char *to_match);
bool inputMatchesSubcommand(const char *input, const char *command_to_match, const char *subcommand_to_match);
EventHandlerResult onSetup(void);
EventHandlerResult sendName(const __FlashStringHelper *name) {
Runtime.serialPort().print(name);
delayAfterPrint();
@ -118,13 +128,16 @@ class FocusSerial : public kaleidoscope::Plugin {
private:
char command_[32];
uint8_t buf_cursor_ = 0;
const char *focus_command_help_;
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;
void delayAfterPrint() { delayMicroseconds(focus_delay_us_after_character_); }
void delayAfterPrint() {
delayMicroseconds(focus_delay_us_after_character_);
}
};
} // namespace plugin

@ -32,10 +32,11 @@ EventHandlerResult LayerNames::onNameQuery() {
}
EventHandlerResult LayerNames::onFocusEvent(const char *command) {
if (::Focus.handleHelp(command, PSTR("keymap.layerNames")))
const char *focus_command_layer_names_ = PSTR("layer_names");
if (::Focus.handleHelp(command, focus_command_layer_names_))
return EventHandlerResult::OK;
if (strcmp_P(command, PSTR("keymap.layerNames")) != 0)
if (!::Focus.inputMatchesCommand(command, focus_command_layer_names_))
return EventHandlerResult::OK;
if (::Focus.isEOL()) {

Loading…
Cancel
Save