diff --git a/right/src/config/config_state.c b/right/src/config/config_state.c index caba350..458d676 100644 --- a/right/src/config/config_state.c +++ b/right/src/config/config_state.c @@ -1,4 +1,34 @@ #include "config_state.h" -uint8_t ConfigBuffer[EEPROM_SIZE]; -uint8_t *ConfigPtr; +static uint8_t config[EEPROM_SIZE]; +serialized_buffer_t ConfigBuffer = { config }; + +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 74f7fa1..931f391 100644 --- a/right/src/config/config_state.h +++ b/right/src/config/config_state.h @@ -3,15 +3,30 @@ // Includes: + #include #include "fsl_common.h" // Macros: #define EEPROM_SIZE (32*1024) +// Typedefs: + + typedef struct { + uint8_t *const buffer; + uint16_t offset; + } serialized_buffer_t; + // Variables: - extern uint8_t ConfigBuffer[EEPROM_SIZE]; - extern uint8_t *ConfigPtr; + extern serialized_buffer_t ConfigBuffer; + +// 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_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.c b/right/src/config/parse_keymap.c index bf53581..75442ae 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -2,37 +2,7 @@ #include "key_action.h" #include "current_keymap.h" -#define longCompactLengthPrefix 0xff - -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 bool isDryRun; static parser_error_t parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_None; @@ -93,45 +63,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; @@ -161,14 +132,14 @@ 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++) { - key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]); - errorCode = parseKeyAction(keyAction, buffer); + for (uint16_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { + errorCode = parseKeyAction(isDryRun ? &dummyKeyAction : &CurrentKeymap[targetLayer][moduleId][actionIdx], buffer); if (errorCode != ParserError_Success) { return errorCode; } @@ -182,19 +153,14 @@ 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); + uint16_t moduleCount = readCompactLength(buffer); if (moduleCount > SLOT_COUNT) { return ParserError_InvalidModuleCount; } - for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { - clearModule(layer, moduleIdx); + for (uint16_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { errorCode = parseModule(buffer, layer); if (errorCode != ParserError_Success) { return errorCode; @@ -212,16 +178,16 @@ 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)isDefault; (void)name; (void)description; if (layerCount != LAYER_COUNT) { return ParserError_InvalidLayerCount; } - for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { + isDryRun = !isDefault; + for (uint16_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); if (errorCode != ParserError_Success) { return errorCode; diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 3e0360b..8e2bd1e 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -5,6 +5,7 @@ #include #include + #include "parse_config.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); diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 8b5f391..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" @@ -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] = ParseConfig(&ConfigBuffer); + GenericHidOutBuffer[1] = ConfigBuffer.offset; + GenericHidOutBuffer[2] = ConfigBuffer.offset >> 8; } void setLedPwm()