Merge branch 'usb-switch-keymap' into dev

This commit is contained in:
László Monda
2018-04-09 21:12:07 +02:00
8 changed files with 63 additions and 6 deletions

View File

@@ -6,12 +6,19 @@
#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;
void SwitchKeymap(uint8_t index)
void SwitchKeymapById(uint8_t index)
{
CurrentKeymapIndex = index;
ValidatedUserConfigBuffer.offset = AllKeymaps[index].offset;
@@ -19,6 +26,18 @@ void SwitchKeymap(uint8_t index)
LedDisplay_SetCurrentKeymapText();
}
bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev)
{
for (uint8_t i=0; i<MAX_KEYMAP_NUM; i++) {
keymap_reference_t *keymap = AllKeymaps + i;
if (keymap->abbreviationLen == length && strcmp(keymap->abbreviation, abbrev) == 0) {
SwitchKeymapById(i);
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

View File

@@ -29,6 +29,7 @@
// Functions:
void SwitchKeymap(uint8_t index);
void SwitchKeymapById(uint8_t index);
bool SwitchKeymapByAbbreviation(uint8_t length, char *abbrev);
#endif

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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;

View File

@@ -33,6 +33,7 @@
UsbCommandId_GetModuleProperty = 0x0e,
UsbCommandId_GetSlaveI2cErrors = 0x0f,
UsbCommandId_SetI2cBaudRate = 0x10,
UsbCommandId_SwitchKeymap = 0x11,
} usb_command_id_t;
typedef enum {

View File

@@ -256,7 +256,7 @@ static 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;
}