From 887208badd2e4ad46b20982564ee726547444aa0 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Fri, 7 Jul 2017 14:05:19 -0700 Subject: [PATCH 1/6] Load only the default keymap into CurrentKeymap --- right/src/config/parse_keymap.c | 40 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index bf53581..1c8b102 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -4,6 +4,8 @@ #define longCompactLengthPrefix 0xff +static bool isDryRun; + static uint8_t readUInt8(serialized_buffer_t *buffer) { return buffer->buffer[buffer->offset++]; } @@ -93,45 +95,46 @@ static parser_error_t parseMouseAction(key_action_t *keyAction, serialized_buffe uint8_t mouseAction = readUInt8(buffer); keyAction->type = KeyActionType_Mouse; + memset(&keyAction->mouse, 0, sizeof keyAction->mouse); switch (mouseAction) { case SerializedMouseAction_LeftClick: - keyAction->mouse.buttonActions |= MouseButton_Left; + keyAction->mouse.buttonActions = MouseButton_Left; break; case SerializedMouseAction_MiddleClick: - keyAction->mouse.buttonActions |= MouseButton_Middle; + keyAction->mouse.buttonActions = MouseButton_Middle; break; case SerializedMouseAction_RightClick: - keyAction->mouse.buttonActions |= MouseButton_Right; + keyAction->mouse.buttonActions = MouseButton_Right; break; case SerializedMouseAction_MoveUp: - keyAction->mouse.moveActions |= MouseMove_Up; + keyAction->mouse.moveActions = MouseMove_Up; break; case SerializedMouseAction_MoveDown: - keyAction->mouse.moveActions |= MouseMove_Down; + keyAction->mouse.moveActions = MouseMove_Down; break; case SerializedMouseAction_MoveLeft: - keyAction->mouse.moveActions |= MouseMove_Left; + keyAction->mouse.moveActions = MouseMove_Left; break; case SerializedMouseAction_MoveRight: - keyAction->mouse.moveActions |= MouseMove_Right; + keyAction->mouse.moveActions = MouseMove_Right; break; case SerializedMouseAction_ScrollUp: - keyAction->mouse.scrollActions |= MouseScroll_Up; + keyAction->mouse.scrollActions = MouseScroll_Up; break; case SerializedMouseAction_ScrollDown: - keyAction->mouse.scrollActions |= MouseScroll_Down; + keyAction->mouse.scrollActions = MouseScroll_Down; break; case SerializedMouseAction_ScrollLeft: - keyAction->mouse.scrollActions |= MouseScroll_Left; + keyAction->mouse.scrollActions = MouseScroll_Left; break; case SerializedMouseAction_ScrollRight: - keyAction->mouse.scrollActions |= MouseScroll_Right; + keyAction->mouse.scrollActions = MouseScroll_Right; break; case SerializedMouseAction_Accelerate: - keyAction->mouse.moveActions |= MouseMove_Accelerate; + keyAction->mouse.moveActions = MouseMove_Accelerate; break; case SerializedMouseAction_Decelerate: - keyAction->mouse.moveActions |= MouseMove_Decelerate; + keyAction->mouse.moveActions = MouseMove_Decelerate; break; default: return ParserError_InvalidSerializedMouseAction; @@ -162,13 +165,13 @@ static parser_error_t parseKeyAction(key_action_t *keyAction, serialized_buffer_ 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); + key_action_t dummyKeyAction; if (actionCount > MAX_KEY_COUNT_PER_MODULE) { return ParserError_InvalidActionCount; } for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { - key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]); - errorCode = parseKeyAction(keyAction, buffer); + errorCode = parseKeyAction(isDryRun ? &dummyKeyAction : &CurrentKeymap[targetLayer][moduleId][actionIdx], buffer); if (errorCode != ParserError_Success) { return errorCode; } @@ -182,10 +185,6 @@ static parser_error_t parseModule(serialized_buffer_t *buffer, uint8_t layer) { return parseKeyActions(layer, buffer, moduleId, pointerRole); } -static void clearModule(uint8_t layer, uint8_t moduleId) { - memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); -} - static parser_error_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { parser_error_t errorCode; uint8_t moduleCount = readCompactLength(buffer); @@ -194,7 +193,6 @@ static parser_error_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { return ParserError_InvalidModuleCount; } for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { - clearModule(layer, moduleIdx); errorCode = parseModule(buffer, layer); if (errorCode != ParserError_Success) { return errorCode; @@ -215,12 +213,12 @@ parser_error_t ParseKeymap(serialized_buffer_t *buffer) {; uint8_t layerCount = readCompactLength(buffer); (void)abbreviation; - (void)isDefault; (void)name; (void)description; if (layerCount != LAYER_COUNT) { return ParserError_InvalidLayerCount; } + isDryRun = !isDefault; for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); if (errorCode != ParserError_Success) { From f4c5a7e969896c8c871a24a2872680d90d0c284b Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Fri, 7 Jul 2017 14:34:12 -0700 Subject: [PATCH 2/6] Revert "Add ConfigPtr" This reverts commit de013f6afa92faf392588c4487a4c8521e57a642. --- right/src/config/config_state.c | 1 - right/src/config/config_state.h | 1 - 2 files changed, 2 deletions(-) diff --git a/right/src/config/config_state.c b/right/src/config/config_state.c index caba350..8d44809 100644 --- a/right/src/config/config_state.c +++ b/right/src/config/config_state.c @@ -1,4 +1,3 @@ #include "config_state.h" uint8_t ConfigBuffer[EEPROM_SIZE]; -uint8_t *ConfigPtr; diff --git a/right/src/config/config_state.h b/right/src/config/config_state.h index 74f7fa1..2dc1552 100644 --- a/right/src/config/config_state.h +++ b/right/src/config/config_state.h @@ -12,6 +12,5 @@ // Variables: extern uint8_t ConfigBuffer[EEPROM_SIZE]; - extern uint8_t *ConfigPtr; #endif From 7a9659f7c0995359a2eea5e843956468e159ac84 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Fri, 7 Jul 2017 14:54:39 -0700 Subject: [PATCH 3/6] Move some code from parse_keymap.* to config_state.* --- right/src/config/config_state.c | 30 ++++++++++++++++++++++++++++++ right/src/config/config_state.h | 25 +++++++++++++++++++++++++ right/src/config/parse_keymap.c | 32 -------------------------------- right/src/config/parse_keymap.h | 16 +--------------- 4 files changed, 56 insertions(+), 47 deletions(-) diff --git a/right/src/config/config_state.c b/right/src/config/config_state.c index 8d44809..9ceb7f4 100644 --- a/right/src/config/config_state.c +++ b/right/src/config/config_state.c @@ -1,3 +1,33 @@ #include "config_state.h" uint8_t ConfigBuffer[EEPROM_SIZE]; + +uint8_t readUInt8(serialized_buffer_t *buffer) { + return buffer->buffer[buffer->offset++]; +} + +uint16_t readUInt16(serialized_buffer_t *buffer) { + uint8_t firstByte = buffer->buffer[buffer->offset++]; + return firstByte + (buffer->buffer[buffer->offset++] << 8); +} + +bool readBool(serialized_buffer_t *buffer) { + return buffer->buffer[buffer->offset++] == 1; +} + +uint16_t readCompactLength(serialized_buffer_t *buffer) { + uint16_t length = readUInt8(buffer); + if (length == 0xFF) { + length = readUInt16(buffer); + } + return length; +} + +const char *readString(serialized_buffer_t *buffer, uint16_t *len) { + const char *str = (const char *)&(buffer->buffer[buffer->offset]); + + *len = readCompactLength(buffer); + buffer->offset += *len; + + return str; +} diff --git a/right/src/config/config_state.h b/right/src/config/config_state.h index 2dc1552..2541363 100644 --- a/right/src/config/config_state.h +++ b/right/src/config/config_state.h @@ -9,8 +9,33 @@ #define EEPROM_SIZE (32*1024) +// Typedefs: + + typedef struct { + uint8_t *buffer; + uint16_t offset; + } serialized_buffer_t; + + typedef enum { + ParserError_Success, + ParserError_InvalidSerializedKeystrokeType, + ParserError_InvalidSerializedMouseAction, + ParserError_InvalidSerializedKeyActionType, + ParserError_InvalidLayerCount, + ParserError_InvalidModuleCount, + ParserError_InvalidActionCount, + } parser_error_t; + // Variables: extern uint8_t ConfigBuffer[EEPROM_SIZE]; +// Functions: + + uint8_t readUInt8(serialized_buffer_t *buffer); + uint16_t readUInt16(serialized_buffer_t *buffer); + bool readBool(serialized_buffer_t *buffer); + uint16_t readCompactLength(serialized_buffer_t *buffer); + const char *readString(serialized_buffer_t *buffer, uint16_t *len); + #endif diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 1c8b102..b6d92fd 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -2,40 +2,8 @@ #include "key_action.h" #include "current_keymap.h" -#define longCompactLengthPrefix 0xff - static bool isDryRun; -static uint8_t readUInt8(serialized_buffer_t *buffer) { - return buffer->buffer[buffer->offset++]; -} - -static uint16_t readUInt16(serialized_buffer_t *buffer) { - uint8_t firstByte = buffer->buffer[buffer->offset++]; - return firstByte + (buffer->buffer[buffer->offset++] << 8); -} - -static bool readBool(serialized_buffer_t *buffer) { - return buffer->buffer[buffer->offset++] == 1; -} - -static uint16_t readCompactLength(serialized_buffer_t *buffer) { - uint16_t length = readUInt8(buffer); - if (length == longCompactLengthPrefix) { - length = readUInt16(buffer); - } - return length; -} - -static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { - const char *str = (const char *)&(buffer->buffer[buffer->offset]); - - *len = readCompactLength(buffer); - buffer->offset += *len; - - return str; -} - static parser_error_t parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_None; return ParserError_Success; diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 3e0360b..63f6252 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -5,6 +5,7 @@ #include #include + #include "config_state.h" // Macros: @@ -49,21 +50,6 @@ SerializedMouseAction_Decelerate, } serialized_mouse_action_t; - typedef struct { - uint8_t *buffer; - uint16_t offset; - } serialized_buffer_t; - - typedef enum { - ParserError_Success, - ParserError_InvalidSerializedKeystrokeType, - ParserError_InvalidSerializedMouseAction, - ParserError_InvalidSerializedKeyActionType, - ParserError_InvalidLayerCount, - ParserError_InvalidModuleCount, - ParserError_InvalidActionCount, - } parser_error_t; - // Functions: parser_error_t ParseKeymap(serialized_buffer_t *buffer); From fa79957ab23846ca3d176280485c3616ecee600f Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 9 Jul 2017 10:23:51 -0700 Subject: [PATCH 4/6] Convert ConfigBuffer to serialized_buffer_t --- right/src/config/config_state.c | 3 ++- right/src/config/config_state.h | 4 ++-- right/src/usb_protocol_handler.c | 11 +++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/right/src/config/config_state.c b/right/src/config/config_state.c index 9ceb7f4..458d676 100644 --- a/right/src/config/config_state.c +++ b/right/src/config/config_state.c @@ -1,6 +1,7 @@ #include "config_state.h" -uint8_t ConfigBuffer[EEPROM_SIZE]; +static uint8_t config[EEPROM_SIZE]; +serialized_buffer_t ConfigBuffer = { config }; uint8_t readUInt8(serialized_buffer_t *buffer) { return buffer->buffer[buffer->offset++]; diff --git a/right/src/config/config_state.h b/right/src/config/config_state.h index 2541363..84065d0 100644 --- a/right/src/config/config_state.h +++ b/right/src/config/config_state.h @@ -12,7 +12,7 @@ // Typedefs: typedef struct { - uint8_t *buffer; + uint8_t *const buffer; uint16_t offset; } serialized_buffer_t; @@ -28,7 +28,7 @@ // Variables: - extern uint8_t ConfigBuffer[EEPROM_SIZE]; + extern serialized_buffer_t ConfigBuffer; // Functions: diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 8b5f391..a021999 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -188,16 +188,15 @@ void uploadConfig() return; } - memcpy(ConfigBuffer+memoryOffset, GenericHidInBuffer+4, byteCount); + memcpy(ConfigBuffer.buffer+memoryOffset, GenericHidInBuffer+4, byteCount); } void applyConfig() { - serialized_buffer_t buffer = { ConfigBuffer, 0 }; - - GenericHidOutBuffer[0] = ParseKeymap(&buffer); - GenericHidOutBuffer[1] = buffer.offset; - GenericHidOutBuffer[2] = buffer.offset >> 8; + ConfigBuffer.offset = 0; + GenericHidOutBuffer[0] = ParseKeymap(&ConfigBuffer); + GenericHidOutBuffer[1] = ConfigBuffer.offset; + GenericHidOutBuffer[2] = ConfigBuffer.offset >> 8; } void setLedPwm() From bbdb09b5f5efc9dde18a8cf1ec66fe03806cadcd Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 9 Jul 2017 10:24:28 -0700 Subject: [PATCH 5/6] Handle cases where readCompactLength returns a value which cannot fit in 8 bits --- right/src/config/parse_keymap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index b6d92fd..75442ae 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -132,13 +132,13 @@ static parser_error_t parseKeyAction(key_action_t *keyAction, serialized_buffer_ 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); + uint16_t actionCount = readCompactLength(buffer); key_action_t dummyKeyAction; if (actionCount > MAX_KEY_COUNT_PER_MODULE) { return ParserError_InvalidActionCount; } - for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { + for (uint16_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { errorCode = parseKeyAction(isDryRun ? &dummyKeyAction : &CurrentKeymap[targetLayer][moduleId][actionIdx], buffer); if (errorCode != ParserError_Success) { return errorCode; @@ -155,12 +155,12 @@ static parser_error_t parseModule(serialized_buffer_t *buffer, uint8_t layer) { static parser_error_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { parser_error_t errorCode; - uint8_t moduleCount = readCompactLength(buffer); + uint16_t moduleCount = readCompactLength(buffer); if (moduleCount > SLOT_COUNT) { return ParserError_InvalidModuleCount; } - for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { + for (uint16_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { errorCode = parseModule(buffer, layer); if (errorCode != ParserError_Success) { return errorCode; @@ -178,7 +178,7 @@ parser_error_t ParseKeymap(serialized_buffer_t *buffer) {; bool isDefault = readBool(buffer); const char *name = readString(buffer, &nameLen); const char *description = readString(buffer, &descriptionLen); - uint8_t layerCount = readCompactLength(buffer); + uint16_t layerCount = readCompactLength(buffer); (void)abbreviation; (void)name; @@ -187,7 +187,7 @@ parser_error_t ParseKeymap(serialized_buffer_t *buffer) {; return ParserError_InvalidLayerCount; } isDryRun = !isDefault; - for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { + for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); if (errorCode != ParserError_Success) { return errorCode; From ec5f77494450b0f3a3892b55a453dd15f713daec Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 9 Jul 2017 10:35:52 -0700 Subject: [PATCH 6/6] Add the parser for the top-level configuration --- right/src/config/config_state.h | 11 +------- right/src/config/parse_config.c | 46 ++++++++++++++++++++++++++++++++ right/src/config/parse_config.h | 24 +++++++++++++++++ right/src/config/parse_keymap.h | 2 +- right/src/usb_protocol_handler.c | 4 +-- 5 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 right/src/config/parse_config.c create mode 100644 right/src/config/parse_config.h diff --git a/right/src/config/config_state.h b/right/src/config/config_state.h index 84065d0..931f391 100644 --- a/right/src/config/config_state.h +++ b/right/src/config/config_state.h @@ -3,6 +3,7 @@ // Includes: + #include #include "fsl_common.h" // Macros: @@ -16,16 +17,6 @@ uint16_t offset; } serialized_buffer_t; - typedef enum { - ParserError_Success, - ParserError_InvalidSerializedKeystrokeType, - ParserError_InvalidSerializedMouseAction, - ParserError_InvalidSerializedKeyActionType, - ParserError_InvalidLayerCount, - ParserError_InvalidModuleCount, - ParserError_InvalidActionCount, - } parser_error_t; - // Variables: extern serialized_buffer_t ConfigBuffer; diff --git a/right/src/config/parse_config.c b/right/src/config/parse_config.c new file mode 100644 index 0000000..cde0ecc --- /dev/null +++ b/right/src/config/parse_config.c @@ -0,0 +1,46 @@ +#include "parse_config.h" +#include "parse_keymap.h" + +static parser_error_t parseModuleConfiguration(serialized_buffer_t *buffer) { + uint8_t id = readUInt8(buffer); + uint8_t initialPointerSpeed = readUInt8(buffer); + uint8_t pointerAcceleration = readUInt8(buffer); + uint8_t maxPointerSpeed = readUInt8(buffer); + + (void)id; + (void)initialPointerSpeed; + (void)pointerAcceleration; + (void)maxPointerSpeed; + return ParserError_Success; +} + +parser_error_t ParseConfig(serialized_buffer_t *buffer) { + uint16_t dataModelVersion = readUInt16(buffer); + parser_error_t errorCode; + uint16_t moduleConfigurationCount = readCompactLength(buffer); + uint16_t macroCount; + uint16_t keymapCount; + + (void)dataModelVersion; + for (uint8_t moduleConfigurationIdx = 0; moduleConfigurationIdx < moduleConfigurationCount; moduleConfigurationIdx++) { + errorCode = parseModuleConfiguration(buffer); + if (errorCode != ParserError_Success) { + return errorCode; + } + } + macroCount = readCompactLength(buffer); + for (uint8_t macroIdx = 0; macroIdx < macroCount; macroIdx++) { + // errorCode = ParseMacro(buffer); + // if (errorCode != ParserError_Success) { + // return errorCode; + // } + } + keymapCount = readCompactLength(buffer); + for (uint8_t keymapIdx = 0; keymapIdx < keymapCount; keymapIdx++) { + errorCode = ParseKeymap(buffer); + if (errorCode != ParserError_Success) { + return errorCode; + } + } + return ParserError_Success; +} diff --git a/right/src/config/parse_config.h b/right/src/config/parse_config.h new file mode 100644 index 0000000..494d721 --- /dev/null +++ b/right/src/config/parse_config.h @@ -0,0 +1,24 @@ +#ifndef __PARSE_CONFIG_H__ +#define __PARSE_CONFIG_H__ + +// Includes: + + #include "config_state.h" + +// Typedefs: + + typedef enum { + ParserError_Success, + ParserError_InvalidSerializedKeystrokeType, + ParserError_InvalidSerializedMouseAction, + ParserError_InvalidSerializedKeyActionType, + ParserError_InvalidLayerCount, + ParserError_InvalidModuleCount, + ParserError_InvalidActionCount, + } parser_error_t; + +// Functions: + + parser_error_t ParseConfig(serialized_buffer_t *buffer); + +#endif diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 63f6252..8e2bd1e 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -5,7 +5,7 @@ #include #include - #include "config_state.h" + #include "parse_config.h" // Macros: diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index a021999..68b8cea 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -4,7 +4,7 @@ #include "i2c_addresses.h" #include "led_driver.h" #include "peripherals/merge_sensor.h" -#include "config/parse_keymap.h" +#include "config/parse_config.h" #include "config/config_state.h" #include "led_pwm.h" #include "slave_scheduler.h" @@ -194,7 +194,7 @@ void uploadConfig() void applyConfig() { ConfigBuffer.offset = 0; - GenericHidOutBuffer[0] = ParseKeymap(&ConfigBuffer); + GenericHidOutBuffer[0] = ParseConfig(&ConfigBuffer); GenericHidOutBuffer[1] = ConfigBuffer.offset; GenericHidOutBuffer[2] = ConfigBuffer.offset >> 8; }