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