diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index b9660a1..0127732 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -1,6 +1,8 @@ #include "usb_protocol_handler.h" #include "system_properties.h" #include "test_led.h" +#include "i2c_addresses.h" +#include "led_driver.h" void SetError(uint8_t error); void SetGenericError(); @@ -96,6 +98,20 @@ void GetSetTestLed() void WriteLedDriver() { + uint8_t i2cAddress = GenericHidInBuffer[1]; + uint8_t i2cPayloadSize = GenericHidInBuffer[2]; + + if (!IS_I2C_LED_DRIVER_ADDRESS(i2cAddress)) { + SetError(WRITE_LED_DRIVER_RESPONSE_INVALID_ADDRESS); + return; + } + + if (i2cPayloadSize > USB_GENERIC_HID_OUT_BUFFER_LENGTH-3) { + SetError(WRITE_LED_DRIVER_RESPONSE_INVALID_PAYLOAD_SIZE); + return; + } + + LedDriver_WriteBuffer(i2cAddress, GenericHidInBuffer+3, i2cPayloadSize); } void ReadLedDriver() diff --git a/right/src/usb_protocol_handler.h b/right/src/usb_protocol_handler.h index a49fcba..4448c5b 100644 --- a/right/src/usb_protocol_handler.h +++ b/right/src/usb_protocol_handler.h @@ -18,6 +18,8 @@ #define USB_COMMAND_JUMP_TO_BOOTLOADER 1 #define USB_COMMAND_TEST_LED 2 #define USB_COMMAND_WRITE_LED_DRIVER 3 + #define WRITE_LED_DRIVER_RESPONSE_INVALID_ADDRESS 1 + #define WRITE_LED_DRIVER_RESPONSE_INVALID_PAYLOAD_SIZE 2 #define USB_COMMAND_READ_LED_DRIVER 4 // Functions: diff --git a/shared/i2c_addresses.h b/shared/i2c_addresses.h index aecd0b3..08bfe58 100644 --- a/shared/i2c_addresses.h +++ b/shared/i2c_addresses.h @@ -5,11 +5,10 @@ #define I2C_ADDRESS_LEFT_KEYBOARD_HALF 8 -// IS31FL3731 LED drivers range from 0x74 to 0x77 #define I2C_ADDRESS_LED_DRIVER_LEFT 0b1110100 #define I2C_ADDRESS_LED_DRIVER_RIGHT 0b1110111 -#define IS_I2C_LED_DRIVER_ADDRESS (address) \ - (I2C_ADDRESS_LED_DRIVER_LEFT <= address && address <= I2C_ADDRESS_LED_DRIVER_RIGHT) +#define IS_I2C_LED_DRIVER_ADDRESS(address) \ + (I2C_ADDRESS_LED_DRIVER_LEFT <= (address) && (address) <= I2C_ADDRESS_LED_DRIVER_RIGHT) #endif