From b34fb9daa336980850fa1b82a01d744bdc5382a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 30 Mar 2018 11:18:31 +0200 Subject: [PATCH 1/3] Add switch keymap USB command. --- right/src/keymap.c | 20 ++++++++++++++++++- right/src/keymap.h | 1 + .../usb_commands/usb_command_switch_keymap.c | 17 ++++++++++++++++ .../usb_commands/usb_command_switch_keymap.h | 15 ++++++++++++++ right/src/usb_protocol_handler.c | 4 ++++ right/src/usb_protocol_handler.h | 1 + 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 right/src/usb_commands/usb_command_switch_keymap.c create mode 100644 right/src/usb_commands/usb_command_switch_keymap.h diff --git a/right/src/keymap.c b/right/src/keymap.c index e237f47..c0a8092 100644 --- a/right/src/keymap.c +++ b/right/src/keymap.c @@ -6,7 +6,14 @@ #include "config_parser/config_globals.h" #include "macros.h" -keymap_reference_t AllKeymaps[MAX_KEYMAP_NUM] = { { "FTY", 0, 3 } }; +keymap_reference_t AllKeymaps[MAX_KEYMAP_NUM] = { + { + .abbreviation = "FTY", + .offset = 0, + .abbreviationLen = 3 + } +}; + uint8_t AllKeymapsCount; uint8_t DefaultKeymapIndex; uint8_t CurrentKeymapIndex = 0; @@ -19,6 +26,17 @@ void SwitchKeymap(uint8_t index) LedDisplay_SetCurrentKeymapText(); } +bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev) +{ + for (uint8_t i=0; iabbreviationLen == length && strcmp(keymap->abbreviation, abbrev) == 0) { + return true; + } + } + return false; +} + // The factory keymap is initialized before it gets overwritten by the default keymap of the EEPROM. key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] = { // Base layer diff --git a/right/src/keymap.h b/right/src/keymap.h index 1f03772..3a737d2 100644 --- a/right/src/keymap.h +++ b/right/src/keymap.h @@ -30,5 +30,6 @@ // Functions: void SwitchKeymap(uint8_t index); + bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev); #endif diff --git a/right/src/usb_commands/usb_command_switch_keymap.c b/right/src/usb_commands/usb_command_switch_keymap.c new file mode 100644 index 0000000..f8e8475 --- /dev/null +++ b/right/src/usb_commands/usb_command_switch_keymap.c @@ -0,0 +1,17 @@ +#include "usb_protocol_handler.h" +#include "usb_commands/usb_command_switch_keymap.h" +#include "keymap.h" + +void UsbCommand_SwitchKeymap(void) +{ + uint32_t keymapLength = GetUsbRxBufferUint8(1); + char *keymapAbbrev = (char*)GenericHidInBuffer + 2; + + if (keymapLength > KEYMAP_ABBREVIATION_LENGTH) { + SetUsbTxBufferUint8(0, UsbStatusCode_SwitchKeymap_InvalidAbbreviationLength); + } + + if (!SwitchKeymapByAbbreviation(keymapLength, keymapAbbrev)) { + SetUsbTxBufferUint8(0, UsbStatusCode_SwitchKeymap_InvalidAbbreviation); + } +} diff --git a/right/src/usb_commands/usb_command_switch_keymap.h b/right/src/usb_commands/usb_command_switch_keymap.h new file mode 100644 index 0000000..be22c77 --- /dev/null +++ b/right/src/usb_commands/usb_command_switch_keymap.h @@ -0,0 +1,15 @@ +#ifndef __USB_COMMAND_SWITCH_KEYMAP_H__ +#define __USB_COMMAND_SWITCH_KEYMAP_H__ + +// Functions: + + void UsbCommand_SwitchKeymap(void); + +// Typedefs: + + typedef enum { + UsbStatusCode_SwitchKeymap_InvalidAbbreviationLength = 2, + UsbStatusCode_SwitchKeymap_InvalidAbbreviation = 3, + } usb_status_code_switch_keymap_t; + +#endif diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index b933aa2..20b23ed 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -16,6 +16,7 @@ #include "usb_commands/usb_command_send_kboot_command_to_module.h" #include "usb_commands/usb_command_get_slave_i2c_errors.h" #include "usb_commands/usb_command_set_i2c_baud_rate.h" +#include "usb_commands/usb_command_switch_keymap.h" void UsbProtocolHandler(void) { @@ -73,6 +74,9 @@ void UsbProtocolHandler(void) case UsbCommandId_SetI2cBaudRate: UsbCommand_SetI2cBaudRate(); break; + case UsbCommandId_SwitchKeymap: + UsbCommand_SwitchKeymap(); + break; default: SetUsbTxBufferUint8(0, UsbStatusCode_InvalidCommand); break; diff --git a/right/src/usb_protocol_handler.h b/right/src/usb_protocol_handler.h index 5ef3b99..7f48f29 100644 --- a/right/src/usb_protocol_handler.h +++ b/right/src/usb_protocol_handler.h @@ -33,6 +33,7 @@ UsbCommandId_GetModuleProperty = 0x0e, UsbCommandId_GetSlaveI2cErrors = 0x0f, UsbCommandId_SetI2cBaudRate = 0x10, + UsbCommandId_SwitchKeymap = 0x11, } usb_command_id_t; typedef enum { From ecf1f1ac32285ec30c6b11d0ca00141425c21c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 30 Mar 2018 12:44:43 +0200 Subject: [PATCH 2/3] Actually switch the keymap. --- right/src/keymap.c | 1 + 1 file changed, 1 insertion(+) diff --git a/right/src/keymap.c b/right/src/keymap.c index c0a8092..9422c64 100644 --- a/right/src/keymap.c +++ b/right/src/keymap.c @@ -31,6 +31,7 @@ bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev) for (uint8_t i=0; iabbreviationLen == length && strcmp(keymap->abbreviation, abbrev) == 0) { + SwitchKeymap(i); return true; } } From 308a71e4a3223f070326467cc6ba444f7d5e3251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 30 Mar 2018 12:46:30 +0200 Subject: [PATCH 3/3] Rename SwitchKeymap() to SwitchKeymapById() --- right/src/keymap.c | 4 ++-- right/src/keymap.h | 2 +- right/src/usb_commands/usb_command_apply_config.c | 4 ++-- right/src/usb_report_updater.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/right/src/keymap.c b/right/src/keymap.c index 9422c64..86e9a71 100644 --- a/right/src/keymap.c +++ b/right/src/keymap.c @@ -18,7 +18,7 @@ uint8_t AllKeymapsCount; uint8_t DefaultKeymapIndex; uint8_t CurrentKeymapIndex = 0; -void SwitchKeymap(uint8_t index) +void SwitchKeymapById(uint8_t index) { CurrentKeymapIndex = index; ValidatedUserConfigBuffer.offset = AllKeymaps[index].offset; @@ -31,7 +31,7 @@ bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev) for (uint8_t i=0; iabbreviationLen == length && strcmp(keymap->abbreviation, abbrev) == 0) { - SwitchKeymap(i); + SwitchKeymapById(i); return true; } } diff --git a/right/src/keymap.h b/right/src/keymap.h index 3a737d2..f17ea83 100644 --- a/right/src/keymap.h +++ b/right/src/keymap.h @@ -29,7 +29,7 @@ // Functions: - void SwitchKeymap(uint8_t index); + void SwitchKeymapById(uint8_t index); bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev); #endif diff --git a/right/src/usb_commands/usb_command_apply_config.c b/right/src/usb_commands/usb_command_apply_config.c index ede6c99..096e92c 100644 --- a/right/src/usb_commands/usb_command_apply_config.c +++ b/right/src/usb_commands/usb_command_apply_config.c @@ -50,10 +50,10 @@ void UsbCommand_ApplyConfig(void) if (AllKeymaps[keymapId].abbreviationLen == oldKeymapAbbreviationLen && !memcmp(oldKeymapAbbreviation, AllKeymaps[keymapId].abbreviation, oldKeymapAbbreviationLen)) { - SwitchKeymap(keymapId); + SwitchKeymapById(keymapId); return; } } - SwitchKeymap(DefaultKeymapIndex); + SwitchKeymapById(DefaultKeymapIndex); } diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index f4b0277..ce26a51 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -254,7 +254,7 @@ void applyKeyAction(key_state_t *keyState, key_action_t *action) break; case KeyActionType_SwitchKeymap: if (!keyState->previous) { - SwitchKeymap(action->switchKeymap.keymapId); + SwitchKeymapById(action->switchKeymap.keymapId); } break; }