From 5beee0816671f3d89eebcc788d5d2168c93e1a60 Mon Sep 17 00:00:00 2001 From: Matt Harper Date: Fri, 8 May 2020 17:09:21 -0500 Subject: [PATCH] Write only the absolutely necessary LEDs --- .../RGBFusion2SMBusController.cpp | 52 +++++++++++-------- .../RGBFusion2SMBusController.h | 2 + .../RGBController_RGBFusion2SMBus.cpp | 5 +- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.cpp b/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.cpp index 636f2f10..5ae15a63 100644 --- a/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.cpp +++ b/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.cpp @@ -43,31 +43,38 @@ std::string RGBFusion2SMBusController::GetDeviceLocation() return(return_string); } -void RGBFusion2SMBusController::Apply() +/* Writes are performed in 32 byte chunks. If we need to write the second 16 bytes, +* we must necessarily write the first 16 as well. Given that reading the existing state +* from the device is not yet possible, unfortunately we may overwrite existing device +* states if the state transition did not occur within in the same OpenRGB instance. +* That is to say, the current behavior is non-deal but the best we have +*/ +void RGBFusion2SMBusController::WriteLED(int led) { - /* - * Given that reading the existing state from the device is not yet possible, - * unfortunately we may overwrite existing device states if the state transition did - * not occur within in the same OpenRGB instance. - * Current behavior is non-ideal but the best we have. - */ - for (int i = 0; i < 5; i++) { - #ifdef DEBUG - std::cout << "0x" << std::hex << (int)RGB_FUSION_2_LED_START_ADDR + 2*i << "\t"; - for (int j = 0; j < 2; j++) { - for (int k = 0; k < 16; k++) { - std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)led_data[2*i+j][k] << " "; - } - } - std::cout << std::endl; - #endif + unsigned short register_offset = led / 2; // Relying on integer division to truncate + unsigned short write_register = RGB_FUSION_2_LED_START_ADDR + 2*register_offset; - bus->i2c_smbus_write_block_data(RGB_FUSION_2_SMBUS_ADDR, - RGB_FUSION_2_LED_START_ADDR + 2*i, - 32, // Writes occur in 32 byte blocks - led_data[i*2]); + // Adjust if we are writing the second 16 bytes + if (led % 2) { + led -= 1; } + #ifdef DEBUG + std::cout << std::hex << write_register << "\t"; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 16; j++) { + std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)led_data[led+i][j] << " "; + } + std::cout << " "; + } + std::cout << std::endl; + #endif + + bus->i2c_smbus_write_block_data(RGB_FUSION_2_SMBUS_ADDR, write_register, 32, led_data[led]); +} + +void RGBFusion2SMBusController::Apply() +{ // Protocol expects terminating sequence 0x01ff written to register 0x17 bus->i2c_smbus_write_word_data(RGB_FUSION_2_SMBUS_ADDR, RGB_FUSION_2_APPLY_ADDR, @@ -121,7 +128,8 @@ void RGBFusion2SMBusController::SetLEDEffect led_data[led][RGB_FUSION_2_IDX_OPT_1] = speed; // Controls number of flashes break; - } + + WriteLED(led); } diff --git a/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.h b/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.h index c8a3c04b..dc6ad44e 100644 --- a/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.h +++ b/Controllers/RGBFusion2SMBusController/RGBFusion2SMBusController.h @@ -98,4 +98,6 @@ private: rgb_fusion_dev_id dev; unsigned char led_data[10][16]; + + void WriteLED(int); }; diff --git a/RGBController/RGBController_RGBFusion2SMBus.cpp b/RGBController/RGBController_RGBFusion2SMBus.cpp index 518939b1..b14f8507 100644 --- a/RGBController/RGBController_RGBFusion2SMBus.cpp +++ b/RGBController/RGBController_RGBFusion2SMBus.cpp @@ -167,10 +167,7 @@ void RGBController_RGBFusion2SMBus::UpdateZoneLEDs(int zone) void RGBController_RGBFusion2SMBus::UpdateSingleLED(int led) { - // Issuing updates of individual LEDs seems to cause odd speed behavior - // Mitigating by writing all LEDs every time - // TODO - Further investigation into individual updates may be warranted - UpdateLEDs(); + UpdateZoneLEDs(led); } // TODO - Research if possible to read device state