From bcc95abe5314e83bb37eaab69afddb6730d05e4d Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Sun, 12 Jan 2020 22:57:05 -0600 Subject: [PATCH] Add keepalive thread to Corsair Lighting Node Pro driver --- .../CorsairNodeProController.cpp | 87 +++++++++++++++++++ .../CorsairNodeProController.h | 4 +- .../RGBController_CorsairNodePro.cpp | 3 +- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/Controllers/CorsairNodeProController/CorsairNodeProController.cpp b/Controllers/CorsairNodeProController/CorsairNodeProController.cpp index 6c130aa8..05d4a913 100644 --- a/Controllers/CorsairNodeProController/CorsairNodeProController.cpp +++ b/Controllers/CorsairNodeProController/CorsairNodeProController.cpp @@ -10,17 +10,104 @@ #include #include +//Include thread libraries for Windows or Linux +#ifdef WIN32 +#include +#else +#include "pthread.h" +#include "unistd.h" +#endif + +//Thread functions have different types in Windows and Linux +#ifdef WIN32 +#define THREAD static void +#define THREADRETURN +#else +#define THREAD static void* +#define THREADRETURN return(NULL); +#endif + +#ifdef WIN32 +#include +#else +#include + +static void Sleep(unsigned int milliseconds) +{ + usleep(1000 * milliseconds); +} +#endif + +THREAD keepalive_thread(void *param) +{ + CorsairNodeProController* corsair = static_cast(param); + corsair->KeepaliveThread(); + THREADRETURN +} + CorsairNodeProController::CorsairNodeProController(libusb_device_handle* dev_handle) { dev = dev_handle; channel_leds[0] = 60; channel_leds[1] = 60; + + /*-----------------------------------------------------*\ + | The Corsair Lighting Node Pro requires a packet within| + | 20 seconds of sending the lighting change in order | + | to not revert back into rainbow mode. Start a thread | + | to continuously send a keepalive packet every 5s | + \*-----------------------------------------------------*/ +#ifdef WIN32 + _beginthread(keepalive_thread, 0, this); +#else + pthread_t thread; + pthread_create(&thread, NULL, &keepalive_thread, this); +#endif } CorsairNodeProController::~CorsairNodeProController() { } +void CorsairNodeProController::SendKeepalive() +{ + unsigned char usb_apply[] = + { + 0x33, 0xFF, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x80, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + int actual; + + /*-----------------------------------------------------*\ + | Send apply packet | + \*-----------------------------------------------------*/ + libusb_interrupt_transfer(dev, 0x01, usb_apply, 64, &actual, 0); +} + +void CorsairNodeProController::KeepaliveThread() +{ + while(1) + { + SendKeepalive(); + + Sleep(5000); + } +} + void CorsairNodeProController::SetChannelLEDs(unsigned int channel, std::vector colors) { unsigned char usb_start[] = diff --git a/Controllers/CorsairNodeProController/CorsairNodeProController.h b/Controllers/CorsairNodeProController/CorsairNodeProController.h index 53e8f08a..27f8ccf8 100644 --- a/Controllers/CorsairNodeProController/CorsairNodeProController.h +++ b/Controllers/CorsairNodeProController/CorsairNodeProController.h @@ -27,9 +27,11 @@ public: unsigned int GetStripsOnChannel(unsigned int channel); void SetChannelEffect(unsigned int channel, unsigned int mode, std::vector colors); void SetChannelLEDs(unsigned int channel, std::vector colors); - + void SendKeepalive(); unsigned int channel_leds[CORSAIR_NODE_PRO_NUM_CHANNELS]; + void KeepaliveThread(); + private: libusb_device_handle* dev; }; diff --git a/RGBController/RGBController_CorsairNodePro.cpp b/RGBController/RGBController_CorsairNodePro.cpp index 395198bd..8c00f0b4 100644 --- a/RGBController/RGBController_CorsairNodePro.cpp +++ b/RGBController/RGBController_CorsairNodePro.cpp @@ -130,8 +130,9 @@ void RGBController_CorsairNodePro::UpdateZoneLEDs(int zone) if(channel_colors.size() > 0) { - corsair->SetChannelLEDs(channel, channel_colors); + //corsair->SetChannelLEDs(channel, channel_colors); } + corsair->SendKeepalive(); } void RGBController_CorsairNodePro::UpdateSingleLED(int led)