Implement keymap switching

This commit is contained in:
Eric Tang
2017-08-08 09:40:31 -07:00
parent 599e701479
commit 2621707206
7 changed files with 69 additions and 5 deletions

View File

@@ -38,7 +38,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer) {
} }
keymapCount = readCompactLength(buffer); keymapCount = readCompactLength(buffer);
for (uint16_t keymapIdx = 0; keymapIdx < keymapCount; keymapIdx++) { for (uint16_t keymapIdx = 0; keymapIdx < keymapCount; keymapIdx++) {
errorCode = ParseKeymap(buffer); errorCode = ParseKeymap(buffer, keymapIdx, keymapCount);
if (errorCode != ParserError_Success) { if (errorCode != ParserError_Success) {
return errorCode; return errorCode;
} }

View File

@@ -174,7 +174,8 @@ static parser_error_t parseLayer(config_buffer_t *buffer, uint8_t layer) {
return ParserError_Success; return ParserError_Success;
} }
parser_error_t ParseKeymap(config_buffer_t *buffer) {; parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t keymapCount) {;
uint16_t offset = buffer->offset;
parser_error_t errorCode; parser_error_t errorCode;
uint16_t abbreviationLen; uint16_t abbreviationLen;
uint16_t nameLen; uint16_t nameLen;
@@ -191,7 +192,13 @@ parser_error_t ParseKeymap(config_buffer_t *buffer) {;
return ParserError_InvalidLayerCount; return ParserError_InvalidLayerCount;
} }
if (!ParserRunDry) { if (!ParserRunDry) {
LedDisplay_SetText(abbreviationLen, abbreviation); AllKeymapsCount = keymapCount;
AllKeymaps[keymapIdx].abbreviation = abbreviation;
AllKeymaps[keymapIdx].abbreviationLen = abbreviationLen;
AllKeymaps[keymapIdx].offset = offset;
if (isDefault) {
DefaultKeymapIndex = keymapIdx;
}
} }
for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) {
errorCode = parseLayer(buffer, layerIdx); errorCode = parseLayer(buffer, layerIdx);

View File

@@ -52,6 +52,6 @@
// Functions: // Functions:
parser_error_t ParseKeymap(config_buffer_t *buffer); parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t keymapCount);
#endif #endif

View File

@@ -1,9 +1,23 @@
#include "key_action.h"
#include "arduino_hid/ConsumerAPI.h" #include "arduino_hid/ConsumerAPI.h"
#include "arduino_hid/SystemAPI.h" #include "arduino_hid/SystemAPI.h"
#include "keymaps.h"
#include "led_display.h"
#include "config_parser/parse_keymap.h"
// TODO: Restore Ctrl and Super keys and Mod+N. // TODO: Restore Ctrl and Super keys and Mod+N.
keymap_reference_t AllKeymaps[MAX_KEYMAP_NUM] = { { "QTY", 3 } };
uint8_t AllKeymapsCount;
uint8_t DefaultKeymapIndex;
uint8_t CurrentKeymapIndex = 0;
void Keymaps_Switch(uint8_t index) {
CurrentKeymapIndex = index;
UserConfigBuffer.offset = AllKeymaps[index].offset;
ParseKeymap(&UserConfigBuffer, index, AllKeymapsCount);
LedDisplay_SetText(AllKeymaps[index].abbreviationLen, AllKeymaps[index].abbreviation);
}
key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] = { key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] = {
// Base layer // Base layer
{ {

View File

@@ -4,9 +4,30 @@
// Includes: // Includes:
#include <stdint.h> #include <stdint.h>
#include "key_action.h"
// Macros:
#define MAX_KEYMAP_NUM 255
// Typedefs:
typedef struct {
const char *abbreviation;
uint16_t abbreviationLen;
uint16_t offset;
} keymap_reference_t;
// Variables: // Variables:
extern keymap_reference_t AllKeymaps[MAX_KEYMAP_NUM];
extern uint8_t AllKeymapsCount;
extern uint8_t DefaultKeymapIndex;
extern uint8_t CurrentKeymapIndex;
extern key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE]; extern key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
// Functions:
void Keymaps_Switch(uint8_t index);
#endif #endif

View File

@@ -12,6 +12,7 @@
#include "wormhole.h" #include "wormhole.h"
#include "peripherals/adc.h" #include "peripherals/adc.h"
#include "eeprom.h" #include "eeprom.h"
#include "keymaps.h"
// Functions for setting error statuses // Functions for setting error statuses
@@ -86,6 +87,8 @@ void readMergeSensor(void)
void applyConfig(void) void applyConfig(void)
{ {
uint8_t *temp; uint8_t *temp;
char oldKeymapAbbreviation[3];
char oldKeymapAbbreviationLen;
ParserRunDry = true; ParserRunDry = true;
StagingUserConfigBuffer.offset = 0; StagingUserConfigBuffer.offset = 0;
@@ -96,6 +99,8 @@ void applyConfig(void)
if (GenericHidOutBuffer[0]) { if (GenericHidOutBuffer[0]) {
return; return;
} }
memcpy(oldKeymapAbbreviation, AllKeymaps[CurrentKeymapIndex].abbreviation, 3);
oldKeymapAbbreviationLen = AllKeymaps[CurrentKeymapIndex].abbreviationLen;
ParserRunDry = false; ParserRunDry = false;
temp = UserConfigBuffer.buffer; temp = UserConfigBuffer.buffer;
UserConfigBuffer.buffer = StagingUserConfigBuffer.buffer; UserConfigBuffer.buffer = StagingUserConfigBuffer.buffer;
@@ -105,6 +110,20 @@ void applyConfig(void)
GenericHidOutBuffer[1] = UserConfigBuffer.offset; GenericHidOutBuffer[1] = UserConfigBuffer.offset;
GenericHidOutBuffer[2] = UserConfigBuffer.offset >> 8; GenericHidOutBuffer[2] = UserConfigBuffer.offset >> 8;
GenericHidOutBuffer[3] = 1; GenericHidOutBuffer[3] = 1;
if (GenericHidOutBuffer[0]) {
return;
}
for (uint8_t i = 0; i < AllKeymapsCount; i++) {
if (AllKeymaps[i].abbreviationLen != oldKeymapAbbreviationLen) {
continue;
}
if (memcmp(oldKeymapAbbreviation, AllKeymaps[i].abbreviation, oldKeymapAbbreviationLen)) {
continue;
}
Keymaps_Switch(i);
return;
}
Keymaps_Switch(DefaultKeymapIndex);
} }
void setLedPwm(void) void setLedPwm(void)

View File

@@ -174,6 +174,9 @@ void UpdateActiveUsbReports()
case KeyActionType_Test: case KeyActionType_Test:
processTestAction(action); processTestAction(action);
break; break;
case KeyActionType_SwitchKeymap:
Keymaps_Switch(action.switchKeymap.keymapId);
break;
} }
} }
} }