Update LED PWM and test LED states of UHK modules in an async manner.

This commit is contained in:
László Monda
2017-03-18 15:12:31 +01:00
parent 14a856f8a6
commit 4a5e5019aa
5 changed files with 70 additions and 8 deletions

View File

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

View File

@@ -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];

View File

@@ -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;
}

View File

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

12
shared/bridge_protocol.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef __BRIDGE_PROTOCOL__
#define __BRIDGE_PROTOCOL__
// Macros:
typedef enum {
BridgeCommand_GetKeyStates,
BridgeCommand_SetTestLed,
BridgeCommand_SetLedPwmBrightness,
} bridge_command_t;
#endif