From 4a5e5019aa2f00150c5654856056db5a3349e6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sat, 18 Mar 2017 15:12:31 +0100 Subject: [PATCH] Update LED PWM and test LED states of UHK modules in an async manner. --- left/src/bridge_protocol_handler.c | 7 ++-- left/src/bridge_protocol_handler.h | 4 --- .../bridge_slaves/bridge_slave_uhk_module.c | 33 ++++++++++++++++++- .../bridge_slaves/bridge_slave_uhk_module.h | 22 +++++++++++++ shared/bridge_protocol.h | 12 +++++++ 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 shared/bridge_protocol.h diff --git a/left/src/bridge_protocol_handler.c b/left/src/bridge_protocol_handler.c index bf4e7b0..5449058 100644 --- a/left/src/bridge_protocol_handler.c +++ b/left/src/bridge_protocol_handler.c @@ -4,6 +4,7 @@ #include "i2c_addresses.h" #include "i2c.h" #include "led_pwm.h" +#include "bridge_protocol.h" void SetError(uint8_t error); void SetGenericError(); @@ -28,16 +29,16 @@ void BridgeProtocolHandler() { uint8_t commandId = BridgeRxBuffer[0]; switch (commandId) { - case BRIDGE_COMMAND_GET_KEY_STATES: + case BridgeCommand_GetKeyStates: BridgeTxSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM; memcpy(BridgeTxBuffer, keyMatrix.keyStates, BridgeTxSize); break; - case BRIDGE_COMMAND_SET_TEST_LED: + case BridgeCommand_SetTestLed: BridgeTxSize = 0; bool isLedOn = BridgeRxBuffer[1]; TEST_LED_SET(isLedOn); break; - case BRIDGE_COMMAND_SET_LED_PWM: + case BridgeCommand_SetLedPwmBrightness: BridgeTxSize = 0; uint8_t brightnessPercent = BridgeRxBuffer[1]; LedPwm_SetBrightness(brightnessPercent); diff --git a/left/src/bridge_protocol_handler.h b/left/src/bridge_protocol_handler.h index 539019c..b1d7ff6 100644 --- a/left/src/bridge_protocol_handler.h +++ b/left/src/bridge_protocol_handler.h @@ -13,10 +13,6 @@ #define PROTOCOL_RESPONSE_SUCCESS 0 #define PROTOCOL_RESPONSE_GENERIC_ERROR 1 - #define BRIDGE_COMMAND_GET_KEY_STATES 0 - #define BRIDGE_COMMAND_SET_TEST_LED 1 - #define BRIDGE_COMMAND_SET_LED_PWM 2 - // Variables: uint8_t BridgeRxBuffer[BRIDGE_RX_BUFFER_SIZE]; diff --git a/right/src/bridge_slaves/bridge_slave_uhk_module.c b/right/src/bridge_slaves/bridge_slave_uhk_module.c index 0ad1d3b..7822015 100644 --- a/right/src/bridge_slaves/bridge_slave_uhk_module.c +++ b/right/src/bridge_slaves/bridge_slave_uhk_module.c @@ -1,9 +1,40 @@ #include "i2c_addresses.h" #include "i2c.h" #include "bridge_slave_uhk_module.h" +#include "bridge_protocol.h" #include "main.h" +uhk_module_field_t currentUhkModuleField = UhkModuleField_SendKeystatesRequestCommand; +uhk_module_state_t uhkModuleExternalStates[UHK_MODULE_MAX_COUNT]; +uint8_t txBuffer[2]; + bool BridgeSlaveUhkModuleHandler(uint8_t uhkModuleId) { - I2cAsyncRead(I2C_ADDRESS_LEFT_KEYBOARD_HALF, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT); + uhk_module_state_t *uhkModuleInternalState = UhkModuleStates + uhkModuleId; + uhk_module_state_t *uhkModuleExternalState = uhkModuleExternalStates + uhkModuleId; + + switch (currentUhkModuleField) { + case UhkModuleField_SendKeystatesRequestCommand: + txBuffer[0] = BridgeCommand_GetKeyStates; + I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF, txBuffer, 1); + currentUhkModuleField = UhkModuleField_ReceiveKeystates; + break; + case UhkModuleField_ReceiveKeystates: + I2cAsyncRead(I2C_ADDRESS_LEFT_KEYBOARD_HALF, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT); + currentUhkModuleField = UhkModuleField_SendPwmBrightnessCommand; + break; + case UhkModuleField_SendPwmBrightnessCommand: + txBuffer[0] = BridgeCommand_SetLedPwmBrightness; + txBuffer[1] = uhkModuleExternalState->ledPwmBrightness; + I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF, txBuffer, 2); + currentUhkModuleField = UhkModuleField_SendTestLedCommand; + break; + case UhkModuleField_SendTestLedCommand: + txBuffer[0] = BridgeCommand_SetTestLed; + txBuffer[1] = uhkModuleExternalState->isTestLedOn; + I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF, txBuffer, 2); + currentUhkModuleField = UhkModuleField_SendKeystatesRequestCommand; + break; + } + return true; } diff --git a/right/src/bridge_slaves/bridge_slave_uhk_module.h b/right/src/bridge_slaves/bridge_slave_uhk_module.h index 3125ffe..bd71a40 100644 --- a/right/src/bridge_slaves/bridge_slave_uhk_module.h +++ b/right/src/bridge_slaves/bridge_slave_uhk_module.h @@ -5,6 +5,28 @@ #include "fsl_common.h" +// Macros: + + #define UHK_MODULE_MAX_COUNT 1 + +// Typedefs: + + typedef enum { + UhkModuleField_SendKeystatesRequestCommand, + UhkModuleField_ReceiveKeystates, + UhkModuleField_SendPwmBrightnessCommand, + UhkModuleField_SendTestLedCommand, + } uhk_module_field_t; + + typedef struct { + uint8_t ledPwmBrightness; + bool isTestLedOn; + } uhk_module_state_t; + +// Variables: + + uhk_module_state_t UhkModuleStates[UHK_MODULE_MAX_COUNT]; + // Functions: extern bool BridgeSlaveUhkModuleHandler(uint8_t uhkModuleId); diff --git a/shared/bridge_protocol.h b/shared/bridge_protocol.h new file mode 100644 index 0000000..807c064 --- /dev/null +++ b/shared/bridge_protocol.h @@ -0,0 +1,12 @@ +#ifndef __BRIDGE_PROTOCOL__ +#define __BRIDGE_PROTOCOL__ + +// Macros: + + typedef enum { + BridgeCommand_GetKeyStates, + BridgeCommand_SetTestLed, + BridgeCommand_SetLedPwmBrightness, + } bridge_command_t; + +#endif