Serialize key states much more efficiently by using bits instead of bytes.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "slave_protocol.h"
|
||||
#include "main.h"
|
||||
#include "init_peripherals.h"
|
||||
#include "bool_array_converter.h"
|
||||
|
||||
void SetError(uint8_t error);
|
||||
void SetGenericError(void);
|
||||
@@ -33,7 +34,8 @@ void SlaveProtocolHandler(void)
|
||||
switch (commandId) {
|
||||
case SlaveCommand_GetKeyStates:
|
||||
SlaveTxSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM;
|
||||
memcpy(SlaveTxBuffer, keyMatrix.keyStates, SlaveTxSize);
|
||||
BoolBytesToBits(keyMatrix.keyStates, SlaveTxBuffer, SlaveTxSize);
|
||||
// memcpy(SlaveTxBuffer, keyMatrix.keyStates, SlaveTxSize);
|
||||
break;
|
||||
case SlaveCommand_SetTestLed:
|
||||
SlaveTxSize = 0;
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
#include "main.h"
|
||||
#include "peripherals/test_led.h"
|
||||
#include "test_states.h"
|
||||
#include "bool_array_converter.h"
|
||||
|
||||
uhk_module_state_t UhkModuleStates[UHK_MODULE_MAX_COUNT];
|
||||
uhk_module_field_t currentUhkModuleField = UhkModuleField_SendKeystatesRequestCommand;
|
||||
uhk_module_state_t uhkModuleExternalStates[UHK_MODULE_MAX_COUNT];
|
||||
uint8_t txBuffer[2];
|
||||
uint8_t rxBuffer[LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1];
|
||||
|
||||
void UhkModuleSlaveDriver_Init()
|
||||
{
|
||||
@@ -32,10 +34,14 @@ void UhkModuleSlaveDriver_Update(uint8_t uhkModuleId)
|
||||
currentUhkModuleField = UhkModuleField_ReceiveKeystates;
|
||||
break;
|
||||
case UhkModuleField_ReceiveKeystates:
|
||||
I2cAsyncRead(I2C_ADDRESS_LEFT_KEYBOARD_HALF, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||
// I2cAsyncRead(I2C_ADDRESS_LEFT_KEYBOARD_HALF, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||
I2cAsyncRead(I2C_ADDRESS_LEFT_KEYBOARD_HALF, rxBuffer, LEFT_KEYBOARD_HALF_KEY_COUNT/8+1);
|
||||
currentUhkModuleField = UhkModuleField_SendPwmBrightnessCommand;
|
||||
break;
|
||||
case UhkModuleField_SendPwmBrightnessCommand:
|
||||
// memset(rxBuffer, 0, LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1);
|
||||
BoolBitsToBytes(rxBuffer, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||
|
||||
txBuffer[0] = SlaveCommand_SetLedPwmBrightness;
|
||||
txBuffer[1] = uhkModuleInternalState->ledPwmBrightness;
|
||||
I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF, txBuffer, 2);
|
||||
|
||||
17
shared/bool_array_converter.c
Normal file
17
shared/bool_array_converter.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <string.h>
|
||||
#include "bool_array_converter.h"
|
||||
|
||||
void BoolBytesToBits(uint8_t *srcBytes, uint8_t *dstBits, uint8_t byteCount)
|
||||
{
|
||||
memset(dstBits, 0, byteCount/8 + 1);
|
||||
for (uint8_t i=0; i<byteCount; i++) {
|
||||
dstBits[i/8] |= (srcBytes[i] ? 1 : 0) << (i % 8);
|
||||
}
|
||||
}
|
||||
|
||||
void BoolBitsToBytes(uint8_t *srcBits, uint8_t *dstBytes, uint8_t byteCount)
|
||||
{
|
||||
for (uint8_t i=0; i<byteCount; i++) {
|
||||
dstBytes[i] = (srcBits[i/8] & (1 << (i % 8))) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
13
shared/bool_array_converter.h
Normal file
13
shared/bool_array_converter.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef __BOOL_ARRAY_CONVERTER_H__
|
||||
#define __BOOL_ARRAY_CONVERTER_H__
|
||||
|
||||
// Includes:
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Functions:
|
||||
|
||||
void BoolBytesToBits(uint8_t *srcBytes, uint8_t *dstBits, uint8_t byteCount);
|
||||
void BoolBitsToBytes(uint8_t *srcBits, uint8_t *dstBytes, uint8_t byteCount);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user