From ae3efce452209f490224975cd53669102668b26b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sat, 4 Nov 2017 01:32:03 +0100 Subject: [PATCH] Extract the applyConfig USB command into usb_command_apply_config.[ch] --- right/src/main.c | 3 +- .../usb_commands/usb_command_apply_config.c | 58 +++++++++++++++++++ .../usb_commands/usb_command_apply_config.h | 8 +++ right/src/usb_protocol_handler.c | 56 +----------------- right/src/usb_protocol_handler.h | 2 +- 5 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 right/src/usb_commands/usb_command_apply_config.c create mode 100644 right/src/usb_commands/usb_command_apply_config.h diff --git a/right/src/main.c b/right/src/main.c index dc96930..9f4b120 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -17,6 +17,7 @@ #include "right_key_matrix.h" #include "key_scanner.h" #include "key_states.h" +#include "usb_commands/usb_command_apply_config.h" void updateUsbReports(void) { @@ -77,7 +78,7 @@ void main(void) while (1) { if (!IsConfigInitialized && IsEepromInitialized) { - ApplyConfig(); + UsbCommand_ApplyConfig(); IsConfigInitialized = true; } updateUsbReports(); diff --git a/right/src/usb_commands/usb_command_apply_config.c b/right/src/usb_commands/usb_command_apply_config.c new file mode 100644 index 0000000..36ca77e --- /dev/null +++ b/right/src/usb_commands/usb_command_apply_config.c @@ -0,0 +1,58 @@ +#include "fsl_common.h" +#include "usb_commands/usb_command_apply_config.h" +#include "config_parser/config_globals.h" +#include "config_parser/parse_config.h" +#include "usb_interfaces/usb_interface_generic_hid.h" +#include "usb_protocol_handler.h" +#include "keymap.h" + +void UsbCommand_ApplyConfig(void) +{ + // Validate the staging configuration. + + ParserRunDry = true; + StagingUserConfigBuffer.offset = 0; + GenericHidOutBuffer[0] = ParseConfig(&StagingUserConfigBuffer); + *(uint16_t*)(GenericHidOutBuffer+1) = StagingUserConfigBuffer.offset; + GenericHidOutBuffer[3] = 0; + + if (GenericHidOutBuffer[0] != UsbResponse_Success) { + return; + } + + // Make the staging configuration the current one. + + char oldKeymapAbbreviation[KEYMAP_ABBREVIATION_LENGTH]; + uint8_t oldKeymapAbbreviationLen; + memcpy(oldKeymapAbbreviation, AllKeymaps[CurrentKeymapIndex].abbreviation, KEYMAP_ABBREVIATION_LENGTH); + oldKeymapAbbreviationLen = AllKeymaps[CurrentKeymapIndex].abbreviationLen; + + uint8_t *temp = ValidatedUserConfigBuffer.buffer; + ValidatedUserConfigBuffer.buffer = StagingUserConfigBuffer.buffer; + StagingUserConfigBuffer.buffer = temp; + + ParserRunDry = false; + ValidatedUserConfigBuffer.offset = 0; + GenericHidOutBuffer[0] = ParseConfig(&ValidatedUserConfigBuffer); + *(uint16_t*)(GenericHidOutBuffer+1) = ValidatedUserConfigBuffer.offset; + GenericHidOutBuffer[3] = 1; + + if (GenericHidOutBuffer[0] != UsbResponse_Success) { + return; + } + + // Switch to the keymap of the updated configuration of the same name or the default keymap. + + for (uint8_t keymapId = 0; keymapId < AllKeymapsCount; keymapId++) { + if (AllKeymaps[keymapId].abbreviationLen != oldKeymapAbbreviationLen) { + continue; + } + if (memcmp(oldKeymapAbbreviation, AllKeymaps[keymapId].abbreviation, oldKeymapAbbreviationLen)) { + continue; + } + SwitchKeymap(keymapId); + return; + } + + SwitchKeymap(DefaultKeymapIndex); +} diff --git a/right/src/usb_commands/usb_command_apply_config.h b/right/src/usb_commands/usb_command_apply_config.h new file mode 100644 index 0000000..fb20482 --- /dev/null +++ b/right/src/usb_commands/usb_command_apply_config.h @@ -0,0 +1,8 @@ +#ifndef __USB_COMMAND_APPLY_CONFIG_H__ +#define __USB_COMMAND_APPLY_CONFIG_H__ + +// Functions: + + void UsbCommand_ApplyConfig(void); + +#endif diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 3f3f03a..dbbf870 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -16,6 +16,7 @@ #include "keymap.h" #include "microseconds/microseconds_pit.c" #include "i2c_watchdog.h" +#include "usb_commands/usb_command_apply_config.h" uint8_t UsbDebugInfo[USB_GENERIC_HID_OUT_BUFFER_LENGTH]; @@ -93,57 +94,6 @@ void readMergeSensor(void) SetResponseByte(MERGE_SENSOR_IS_MERGED); } -void ApplyConfig(void) -{ - // Validate the staging configuration. - - ParserRunDry = true; - StagingUserConfigBuffer.offset = 0; - GenericHidOutBuffer[0] = ParseConfig(&StagingUserConfigBuffer); - *(uint16_t*)(GenericHidOutBuffer+1) = StagingUserConfigBuffer.offset; - GenericHidOutBuffer[3] = 0; - - if (GenericHidOutBuffer[0] != UsbResponse_Success) { - return; - } - - // Make the staging configuration the current one. - - char oldKeymapAbbreviation[KEYMAP_ABBREVIATION_LENGTH]; - uint8_t oldKeymapAbbreviationLen; - memcpy(oldKeymapAbbreviation, AllKeymaps[CurrentKeymapIndex].abbreviation, KEYMAP_ABBREVIATION_LENGTH); - oldKeymapAbbreviationLen = AllKeymaps[CurrentKeymapIndex].abbreviationLen; - - uint8_t *temp = ValidatedUserConfigBuffer.buffer; - ValidatedUserConfigBuffer.buffer = StagingUserConfigBuffer.buffer; - StagingUserConfigBuffer.buffer = temp; - - ParserRunDry = false; - ValidatedUserConfigBuffer.offset = 0; - GenericHidOutBuffer[0] = ParseConfig(&ValidatedUserConfigBuffer); - *(uint16_t*)(GenericHidOutBuffer+1) = ValidatedUserConfigBuffer.offset; - GenericHidOutBuffer[3] = 1; - - if (GenericHidOutBuffer[0] != UsbResponse_Success) { - return; - } - - // Switch to the keymap of the updated configuration of the same name or the default keymap. - - for (uint8_t keymapId = 0; keymapId < AllKeymapsCount; keymapId++) { - if (AllKeymaps[keymapId].abbreviationLen != oldKeymapAbbreviationLen) { - continue; - } - if (memcmp(oldKeymapAbbreviation, AllKeymaps[keymapId].abbreviation, oldKeymapAbbreviationLen)) { - continue; - } - SwitchKeymap(keymapId); - return; - } - - SwitchKeymap(DefaultKeymapIndex); -} - void setLedPwm(void) { uint8_t brightnessPercent = GenericHidInBuffer[1]; @@ -285,8 +235,8 @@ void UsbProtocolHandler(void) case UsbCommand_WriteUserConfiguration: writeConfiguration(false); break; - case UsbCommand_ApplyConfig: - ApplyConfig(); + case UsbCommandId_ApplyConfig: + UsbCommand_ApplyConfig(); break; case UsbCommand_SetLedPwm: setLedPwm(); diff --git a/right/src/usb_protocol_handler.h b/right/src/usb_protocol_handler.h index 6ced15e..f521ccc 100644 --- a/right/src/usb_protocol_handler.h +++ b/right/src/usb_protocol_handler.h @@ -14,7 +14,7 @@ UsbCommand_WriteLedDriver = 3, UsbCommand_ReadMergeSensor = 7, UsbCommand_WriteUserConfiguration = 8, - UsbCommand_ApplyConfig = 9, + UsbCommandId_ApplyConfig = 9, UsbCommand_SetLedPwm = 10, UsbCommand_GetAdcValue = 11, UsbCommand_LaunchEepromTransfer = 12,