Merge pull request #45 from UltimateHackingKeyboard/configuration-parser

Improve the configuration parser
This commit is contained in:
László Monda
2017-07-07 19:56:16 +02:00
committed by GitHub
3 changed files with 88 additions and 26 deletions
+71 -24
View File
@@ -25,7 +25,6 @@ static uint16_t readCompactLength(serialized_buffer_t *buffer) {
return length; return length;
} }
/*
static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { static const char *readString(serialized_buffer_t *buffer, uint16_t *len) {
const char *str = (const char *)&(buffer->buffer[buffer->offset]); const char *str = (const char *)&(buffer->buffer[buffer->offset]);
@@ -34,13 +33,13 @@ static const char *readString(serialized_buffer_t *buffer, uint16_t *len) {
return str; return str;
} }
*/
static void parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { static parser_error_t parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) {
keyAction->type = KeyActionType_None; keyAction->type = KeyActionType_None;
return ParserError_Success;
} }
static void parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { static parser_error_t parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) {
keyAction->type = KeyActionType_Keystroke; keyAction->type = KeyActionType_Keystroke;
uint8_t keystrokeType = (SERIALIZED_KEYSTROKE_TYPE_MASK_KEYSTROKE_TYPE & keyStrokeAction) >> SERIALIZED_KEYSTROKE_TYPE_OFFSET_KEYSTROKE_TYPE; uint8_t keystrokeType = (SERIALIZED_KEYSTROKE_TYPE_MASK_KEYSTROKE_TYPE & keyStrokeAction) >> SERIALIZED_KEYSTROKE_TYPE_OFFSET_KEYSTROKE_TYPE;
@@ -55,6 +54,8 @@ static void parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeActio
case SerializedKeystrokeType_System: case SerializedKeystrokeType_System:
keyAction->keystroke.keystrokeType = KeystrokeType_System; keyAction->keystroke.keystrokeType = KeystrokeType_System;
break; break;
default:
return ParserError_InvalidSerializedKeystrokeType;
} }
if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_SCANCODE) { if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_SCANCODE) {
keyAction->keystroke.scancode = keystrokeType == SerializedKeystrokeType_LongMedia ? readUInt16(buffer) : readUInt8(buffer); keyAction->keystroke.scancode = keystrokeType == SerializedKeystrokeType_LongMedia ? readUInt16(buffer) : readUInt8(buffer);
@@ -65,27 +66,30 @@ static void parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeActio
if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_LONGPRESS) { if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_LONGPRESS) {
keyAction->keystroke.longPressAction = readUInt8(buffer); keyAction->keystroke.longPressAction = readUInt8(buffer);
} }
return ParserError_Success;
} }
static void parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { static parser_error_t parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) {
uint8_t layer = readUInt8(buffer) + 1; uint8_t layer = readUInt8(buffer) + 1;
bool isToggle = readBool(buffer); bool isToggle = readBool(buffer);
KeyAction->type = KeyActionType_SwitchLayer; KeyAction->type = KeyActionType_SwitchLayer;
KeyAction->switchLayer.layer = layer; KeyAction->switchLayer.layer = layer;
KeyAction->switchLayer.isToggle = isToggle; KeyAction->switchLayer.isToggle = isToggle;
return ParserError_Success;
} }
static void parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { static parser_error_t parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) {
// uint16_t len; uint16_t keymapAbbreviationLen;
// const char *keymap = readString(buffer, &len); const char *keymapAbbreviation = readString(buffer, &keymapAbbreviationLen);
(void)keymapAbbreviation;
keyAction->type = KeyActionType_SwitchKeymap; keyAction->type = KeyActionType_SwitchKeymap;
// TODO: Implement this // TODO: Implement this
return ParserError_Success;
} }
static void parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { static parser_error_t parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) {
uint8_t mouseAction = readUInt8(buffer); uint8_t mouseAction = readUInt8(buffer);
keyAction->type = KeyActionType_Mouse; keyAction->type = KeyActionType_Mouse;
@@ -129,10 +133,13 @@ static void parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffe
case SerializedMouseAction_Decelerate: case SerializedMouseAction_Decelerate:
keyAction->mouse.moveActions |= MouseMove_Decelerate; keyAction->mouse.moveActions |= MouseMove_Decelerate;
break; break;
default:
return ParserError_InvalidSerializedMouseAction;
} }
return ParserError_Success;
} }
static void parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { static parser_error_t parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) {
uint8_t keyActionType = readUInt8(buffer); uint8_t keyActionType = readUInt8(buffer);
switch (keyActionType) { switch (keyActionType) {
@@ -147,39 +154,79 @@ static void parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer)
case SerializedKeyActionType_Mouse: case SerializedKeyActionType_Mouse:
return parseMouseAction(keyAction, buffer); return parseMouseAction(keyAction, buffer);
default: default:
// TODO: Handle the case where the actionType is unknown. return ParserError_InvalidSerializedKeyActionType;
break;
} }
return ParserError_Success;
} }
static void parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { static parser_error_t parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) {
parser_error_t errorCode;
uint8_t actionCount = readCompactLength(buffer); uint8_t actionCount = readCompactLength(buffer);
if (actionCount > MAX_KEY_COUNT_PER_MODULE) {
return ParserError_InvalidActionCount;
}
for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) {
key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]); key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]);
parseKeyAction(keyAction, buffer); errorCode = parseKeyAction(keyAction, buffer);
if (errorCode != ParserError_Success) {
return errorCode;
}
} }
return ParserError_Success;
} }
static void parseModule(serialized_buffer_t *buffer, uint8_t layer) { static parser_error_t parseModule(serialized_buffer_t *buffer, uint8_t layer) {
uint8_t moduleId = readUInt8(buffer); uint8_t moduleId = readUInt8(buffer);
uint8_t pointerRole = readUInt8(buffer); uint8_t pointerRole = readUInt8(buffer);
parseKeyActions(layer, buffer, moduleId, pointerRole); return parseKeyActions(layer, buffer, moduleId, pointerRole);
} }
static void clearModule(uint8_t layer, uint8_t moduleId) { static void clearModule(uint8_t layer, uint8_t moduleId) {
memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t));
} }
void ParseLayer(uint8_t *data, uint8_t layer) { static parser_error_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) {
serialized_buffer_t buffer; parser_error_t errorCode;
buffer.buffer = data; uint8_t moduleCount = readCompactLength(buffer);
buffer.offset = 0;
uint8_t moduleCount = readCompactLength(&buffer);
if (moduleCount > SLOT_COUNT) {
return ParserError_InvalidModuleCount;
}
for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) {
clearModule(layer, moduleIdx); clearModule(layer, moduleIdx);
parseModule(&buffer, layer); errorCode = parseModule(buffer, layer);
if (errorCode != ParserError_Success) {
return errorCode;
}
} }
return ParserError_Success;
} }
parser_error_t ParseKeymap(serialized_buffer_t *buffer) {;
parser_error_t errorCode;
uint16_t abbreviationLen;
uint16_t nameLen;
uint16_t descriptionLen;
const char *abbreviation = readString(buffer, &abbreviationLen);
bool isDefault = readBool(buffer);
const char *name = readString(buffer, &nameLen);
const char *description = readString(buffer, &descriptionLen);
uint8_t layerCount = readCompactLength(buffer);
(void)abbreviation;
(void)isDefault;
(void)name;
(void)description;
if (layerCount != LAYER_COUNT) {
return ParserError_InvalidLayerCount;
}
for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) {
errorCode = parseLayer(buffer, layerIdx);
if (errorCode != ParserError_Success) {
return errorCode;
}
}
return ParserError_Success;
}
+12 -1
View File
@@ -4,6 +4,7 @@
// Includes: // Includes:
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
// Macros: // Macros:
@@ -53,8 +54,18 @@
uint16_t offset; uint16_t offset;
} serialized_buffer_t; } serialized_buffer_t;
typedef enum {
ParserError_Success,
ParserError_InvalidSerializedKeystrokeType,
ParserError_InvalidSerializedMouseAction,
ParserError_InvalidSerializedKeyActionType,
ParserError_InvalidLayerCount,
ParserError_InvalidModuleCount,
ParserError_InvalidActionCount,
} parser_error_t;
// Functions: // Functions:
void ParseLayer(uint8_t *data, uint8_t layer); parser_error_t ParseKeymap(serialized_buffer_t *buffer);
#endif #endif
+5 -1
View File
@@ -193,7 +193,11 @@ void uploadConfig()
void applyConfig() void applyConfig()
{ {
ParseLayer(ConfigBuffer, 0); serialized_buffer_t buffer = { ConfigBuffer, 0 };
GenericHidOutBuffer[0] = ParseKeymap(&buffer);
GenericHidOutBuffer[1] = buffer.offset;
GenericHidOutBuffer[2] = buffer.offset >> 8;
} }
void setLedPwm() void setLedPwm()