From a8c83e568860d4791a26b4ab6d69d85a18fd8136 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Tue, 27 Aug 2019 12:23:13 -0500 Subject: [PATCH] Add interface for NZXT Hue+ based on KeyboardVisualizer code and add support for UDP LED strips. Clean up LEDStrip code. --- .../HuePlusController/HuePlusController.cpp | 136 ++++++++++++++++++ .../HuePlusController/HuePlusController.h | 48 +++++++ .../HuePlusControllerDetect.cpp | 89 ++++++++++++ .../LEDStripController/LEDStripController.cpp | 118 ++++++++------- .../LEDStripController/LEDStripController.h | 11 +- .../LEDStripControllerDetect.cpp | 8 -- OpenAuraSDK.pro | 9 ++ RGBController/RGBController_HuePlus.cpp | 86 +++++++++++ RGBController/RGBController_HuePlus.h | 28 ++++ 9 files changed, 468 insertions(+), 65 deletions(-) create mode 100644 Controllers/HuePlusController/HuePlusController.cpp create mode 100644 Controllers/HuePlusController/HuePlusController.h create mode 100644 Controllers/HuePlusController/HuePlusControllerDetect.cpp create mode 100644 RGBController/RGBController_HuePlus.cpp create mode 100644 RGBController/RGBController_HuePlus.h diff --git a/Controllers/HuePlusController/HuePlusController.cpp b/Controllers/HuePlusController/HuePlusController.cpp new file mode 100644 index 000000000..63c10afcb --- /dev/null +++ b/Controllers/HuePlusController/HuePlusController.cpp @@ -0,0 +1,136 @@ +/*---------------------------------------------------------*\ +| Processing Code for NZXT Hue+ | +| | +| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 | +\*---------------------------------------------------------*/ + +#include "HuePlusController.h" + +#include +#include +#include + +HuePlusController::HuePlusController() +{ + +} + + +HuePlusController::~HuePlusController() +{ +} + +void HuePlusController::Initialize(char* ledstring) +{ + strcpy(led_string, ledstring); + + LPSTR source = NULL; + LPSTR channels = NULL; + LPSTR numleds = NULL; + LPSTR next = NULL; + + source = strtok_s(ledstring, ",", &next); + + //Check for selected channel 0=both, 1= Ch.1, 2= Ch.2 + if (strlen(next)) + { + channels = strtok_s(next, ",", &next); + } + + switch (atoi(channels)) + { + case 0: + channel = 0x00; + break; + + case 1: + channel = 0x01; + break; + + case 2: + channel = 0x02; + break; + } + + //Check for the number of LEDs, sets the corresponding variable with the counter for the fans + if (strlen(next)) + { + numleds = strtok_s(next, ",", &next); + } + + switch (atoi(numleds) / 8) + { + case 1: + fans = 0x00; + break; + + case 2: + fans = 0x01; + break; + + case 3: + fans = 0x02; + break; + + case 4: + fans = 0x03; + break; + + case 5: + fans = 0x04; + break; + } + + //Initialize with default baud rate + source = strtok(source, "\r"); + strcpy(port_name, source); + baud_rate = 256000; + serialport = new serial_port(port_name, baud_rate); + + if (numleds != NULL && strlen(numleds)) + { + num_leds = atoi(numleds); + } +} + +char* HuePlusController::GetLEDString() +{ + return(led_string); +} + +void HuePlusController::SetLEDs(std::vector colors) +{ + if (serialport != NULL) + { + unsigned char *serial_buf; + + serial_buf = new unsigned char[hueSize]; //Size of Message always 5 XX Blocks (Mode Selection) + 3 XX for each LED (1 color) + //-> max of 40 LEDs per Channel (or 5 Fans a 8 LEDs) -> 125 Blocks (empty LEDs are written, too + serial_buf[0] = 0x4b; + serial_buf[1] = channel; + serial_buf[2] = 0x0e; + serial_buf[3] = fans; + serial_buf[4] = 0x00; + + for (int i = 5; i < hueSize; i++) + { + //clearing the buf otherwise sometimes strange things are written to the COM Port + serial_buf[i] = 0x00; + } + + for (int idx = 0; idx < (num_leds * 3); idx += 3) + { + int pixel_idx = idx / 3; + RGBColor color = colors[pixel_idx]; + serial_buf[idx + 5] = RGBGetGValue(color); + serial_buf[idx + 6] = RGBGetRValue(color); + serial_buf[idx + 7] = RGBGetBValue(color); + } + + serialport->serial_write((char *)serial_buf, hueSize); + serialport->serial_flush_tx(); + + delete[] serial_buf; + } + +} \ No newline at end of file diff --git a/Controllers/HuePlusController/HuePlusController.h b/Controllers/HuePlusController/HuePlusController.h new file mode 100644 index 000000000..3af1708a4 --- /dev/null +++ b/Controllers/HuePlusController/HuePlusController.h @@ -0,0 +1,48 @@ +/*---------------------------------------------------------*\ +| Definitions for NZXT Hue+ | +| | +| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 | +\*---------------------------------------------------------*/ + +#ifndef LED_STRIP_H +#define LED_STRIP_H + +#include "RGBController.h" +#include "serial_port.h" +#include + + +#ifndef TRUE +#define TRUE true +#define FALSE false +#endif + +#ifndef WIN32 +#define LPSTR char * +#define strtok_s strtok_r +#endif + +class HuePlusController +{ +public: + HuePlusController(); + ~HuePlusController(); + + void Initialize(char* ledstring); + char* GetLEDString(); + void SetLEDs(std::vector colors); + + int num_leds; + +private: + int baud_rate; + int fans; + int channel; + const int hueSize = 125; + + char led_string[1024]; + char port_name[128]; + serial_port *serialport; +}; + +#endif diff --git a/Controllers/HuePlusController/HuePlusControllerDetect.cpp b/Controllers/HuePlusController/HuePlusControllerDetect.cpp new file mode 100644 index 000000000..5a03db10f --- /dev/null +++ b/Controllers/HuePlusController/HuePlusControllerDetect.cpp @@ -0,0 +1,89 @@ +#include "HuePlusController.h" +#include "RGBController.h" +#include "RGBController_HuePlus.h" +#include +#include +#include + +#include +#include +#include +#include +#include + +#ifndef WIN32 +#include +#include +#endif + +/******************************************************************************************\ +* * +* DetectHuePlusControllers * +* * +* Detect devices supported by the HuePlus driver * +* * * +\******************************************************************************************/ + +void DetectHuePlusControllers(std::vector &rgb_controllers) +{ + HuePlusController* new_hueplus; + RGBController_HuePlus* new_controller; + + //Get file path in executable directory + std::ifstream infile; + char filename[2048]; + char arg1[64]; + +#ifdef WIN32 + GetModuleFileName(NULL, filename, 2048); + strcpy(filename, std::string(filename).substr(0, std::string(filename).find_last_of("\\/")).c_str()); + strcat(filename, "\\settings.txt"); +#else + snprintf(arg1, 64, "/proc/%d/exe", getpid()); + readlink(arg1, filename, 1024); + strcpy(filename, std::string(filename).substr(0, std::string(filename).find_last_of("\\/")).c_str()); +#endif + + strcat(filename, "/settings.txt"); + + //Open settings file + infile.open(filename); + + if (infile.good()) + { + for (std::string line; std::getline(infile, line); ) + { + if (line == "") + { + continue; + } + if ((line[0] != ';') && (line[0] != '#') && (line[0] != '/')) + { + char * argument; + char * value; + + value = (char *)line.c_str(); + + argument = strtok_s(value, "=", &value); + + //Strip off new line characters if present + argument = strtok(argument, "\r\n"); + value = strtok(value, "\r\n"); + + if(argument) + { + if (strcmp(argument, "hueplus") == 0) + { + new_hueplus = new HuePlusController(); + new_hueplus->Initialize(value); + + new_controller = new RGBController_HuePlus(new_hueplus); + rgb_controllers.push_back(new_controller); + } + } + } + } + } + + +} /* DetectHuePlusControllers() */ \ No newline at end of file diff --git a/Controllers/LEDStripController/LEDStripController.cpp b/Controllers/LEDStripController/LEDStripController.cpp index 785a5b49c..9bc10e7ca 100644 --- a/Controllers/LEDStripController/LEDStripController.cpp +++ b/Controllers/LEDStripController/LEDStripController.cpp @@ -26,9 +26,19 @@ void LEDStripController::Initialize(char* ledstring) LPSTR source = NULL; LPSTR udpport_baud = NULL; LPSTR next = NULL; - + + //Assume serial device unless a different protocol is specified + bool serial = TRUE; + source = strtok_s(ledstring, ",", &next); + //Check if we are setting up a Keyboard Visualizer UDP protocol device + if (strncmp(source, "udp:", 4) == 0) + { + source = source + 4; + serial = FALSE; + } + //Check for either the UDP port or the serial baud rate if (strlen(next)) { @@ -41,8 +51,31 @@ void LEDStripController::Initialize(char* ledstring) numleds = strtok_s(next, ",", &next); } - //Initialize with custom baud rate - InitializeSerial(source, atoi(udpport_baud)); + if (serial) + { + if (udpport_baud == NULL) + { + //Initialize with default baud rate + InitializeSerial(source, 115200); + } + else + { + //Initialize with custom baud rate + InitializeSerial(source, atoi(udpport_baud)); + } + } + else + { + if (udpport_baud == NULL) + { + //Do something + } + else + { + //Initialize UDP port + InitializeUDP(source, udpport_baud); + } + } if (numleds != NULL && strlen(numleds)) { @@ -56,6 +89,7 @@ void LEDStripController::InitializeSerial(char* portname, int baud) strcpy(port_name, portname); baud_rate = baud; serialport = new serial_port(port_name, baud_rate); + udpport = NULL; } void LEDStripController::InitializeUDP(char * clientname, char * port) @@ -63,16 +97,7 @@ void LEDStripController::InitializeUDP(char * clientname, char * port) strcpy(client_name, clientname); strcpy(port_name, port); - //udpport = new net_port(client_name, port_name); - serialport = NULL; -} - -void LEDStripController::InitializeEspurna(char * clientname, char * port, char * apikey) -{ - strcpy(client_name, clientname); - strcpy(port_name, port); - strcpy(espurna_apikey, apikey); - //tcpport = new net_port; + udpport = new net_port(client_name, port_name); serialport = NULL; } @@ -83,43 +108,40 @@ char* LEDStripController::GetLEDString() void LEDStripController::SetLEDs(std::vector colors) { - if (serialport != NULL ) + unsigned char *serial_buf; + + serial_buf = new unsigned char[(num_leds * 3) + 3]; + + serial_buf[0] = 0xAA; + + for (int idx = 0; idx < (num_leds * 3); idx += 3) { - unsigned char *serial_buf; - - serial_buf = new unsigned char[(num_leds * 3) + 3]; - - serial_buf[0] = 0xAA; - - for (int idx = 0; idx < (num_leds * 3); idx += 3) - { - int pixel_idx = idx / 3; - RGBColor color = colors[pixel_idx]; - serial_buf[idx + 1] = RGBGetRValue(color); - serial_buf[idx + 2] = RGBGetGValue(color); - serial_buf[idx + 3] = RGBGetBValue(color); - } - - unsigned short sum = 0; - - for (int i = 0; i < (num_leds * 3) + 1; i++) - { - sum += serial_buf[i]; - } - - serial_buf[(num_leds * 3) + 1] = sum >> 8; - serial_buf[(num_leds * 3) + 2] = sum & 0x00FF; - - if (serialport != NULL) - { - serialport->serial_write((char *)serial_buf, (num_leds * 3) + 3); - serialport->serial_flush_tx(); - } - - delete[] serial_buf; + int pixel_idx = idx / 3; + RGBColor color = colors[pixel_idx]; + serial_buf[idx + 1] = RGBGetRValue(color); + serial_buf[idx + 2] = RGBGetGValue(color); + serial_buf[idx + 3] = RGBGetBValue(color); } - else + + unsigned short sum = 0; + + for (int i = 0; i < (num_leds * 3) + 1; i++) { - //SetLEDsEspurna(pixels); + sum += serial_buf[i]; } + + serial_buf[(num_leds * 3) + 1] = sum >> 8; + serial_buf[(num_leds * 3) + 2] = sum & 0x00FF; + + if (serialport != NULL) + { + serialport->serial_write((char *)serial_buf, (num_leds * 3) + 3); + serialport->serial_flush_tx(); + } + else if (udpport != NULL) + { + udpport->udp_write((char *)serial_buf, (num_leds * 3) + 3); + } + + delete[] serial_buf; } \ No newline at end of file diff --git a/Controllers/LEDStripController/LEDStripController.h b/Controllers/LEDStripController/LEDStripController.h index dc71164e8..74a3c31e6 100644 --- a/Controllers/LEDStripController/LEDStripController.h +++ b/Controllers/LEDStripController/LEDStripController.h @@ -9,6 +9,7 @@ #include "RGBController.h" #include "serial_port.h" +#include "net_port.h" #include @@ -29,10 +30,8 @@ public: ~LEDStripController(); void Initialize(char* ledstring); - void InitializeHuePlus(char * ledstring); void InitializeSerial(char* portname, int baud); void InitializeUDP(char* clientname, char* port); - void InitializeEspurna(char* clientname, char* port, char * apikey); char* GetLEDString(); void SetLEDs(std::vector colors); @@ -40,18 +39,12 @@ public: private: int baud_rate; - int fans; - int channel; - const int hueSize = 125; - - int * LEDStripXIndex; - int * LEDStripYIndex; char led_string[1024]; char port_name[128]; char client_name[1024]; - char espurna_apikey[128]; serial_port *serialport; + net_port *udpport; }; #endif diff --git a/Controllers/LEDStripController/LEDStripControllerDetect.cpp b/Controllers/LEDStripController/LEDStripControllerDetect.cpp index aa1f42746..7ccdaf36a 100644 --- a/Controllers/LEDStripController/LEDStripControllerDetect.cpp +++ b/Controllers/LEDStripController/LEDStripControllerDetect.cpp @@ -80,14 +80,6 @@ void DetectLEDStripControllers(std::vector &rgb_controllers) new_controller = new RGBController_LEDStrip(new_ledstrip); rgb_controllers.push_back(new_controller); } - else if (strcmp(argument, "xmas") == 0) - { - - } - else if (strcmp(argument, "hueplus") == 0) - { - - } } } } diff --git a/OpenAuraSDK.pro b/OpenAuraSDK.pro index 0fa601c88..1cc653130 100644 --- a/OpenAuraSDK.pro +++ b/OpenAuraSDK.pro @@ -7,10 +7,12 @@ TEMPLATE = app INCLUDEPATH += \ i2c_smbus/ \ + net_port/ \ serial_port/ \ Controllers/AuraController/ \ Controllers/CorsairController/ \ Controllers/CorsairProController/ \ + Controllers/HuePlusController/ \ Controllers/HyperXController/ \ Controllers/LEDStripController/ \ RGBController/ \ @@ -22,6 +24,7 @@ SOURCES += \ qt/OpenAuraSDKQtDialog.cpp \ i2c_smbus/i2c_smbus.cpp \ i2c_smbus/i2c_smbus_linux.cpp \ + net_port/net_port.cpp \ serial_port/serial_port.cpp \ Controllers/AuraController/AuraController.cpp \ Controllers/AuraController/AuraControllerDetect.cpp \ @@ -29,6 +32,8 @@ SOURCES += \ Controllers/CorsairController/CorsairControllerDetect.cpp \ Controllers/CorsairProController/CorsairProController.cpp \ Controllers/CorsairProController/CorsairProControllerDetect.cpp \ + Controllers/HuePlusController/HuePlusController.cpp \ + Controllers/HuePlusController/HuePlusControllerDetect.cpp \ Controllers/HyperXController/HyperXController.cpp \ Controllers/HyperXController/HyperXControllerDetect.cpp \ Controllers/LEDStripController/LEDStripController.cpp \ @@ -37,6 +42,7 @@ SOURCES += \ RGBController/RGBController_Aura.cpp \ RGBController/RGBController_Corsair.cpp \ RGBController/RGBController_CorsairPro.cpp \ + RGBController/RGBController_HuePlus.cpp \ RGBController/RGBController_HyperX.cpp \ RGBController/RGBController_LEDStrip.cpp \ RGBController/RGBController_OpenRazer.cpp @@ -45,16 +51,19 @@ HEADERS += \ qt/OpenAuraSDKQtDialog.h \ i2c_smbus/i2c_smbus.h \ i2c_smbus/i2c_smbus_linux.h \ + net_port/net_port.h \ serial_port/serial_port.h \ Controllers/AuraController/AuraController.h \ Controllers/CorsairController/CorsairController.h \ Controllers/CorsairProController/CorsairProController.h \ + Controllers/HuePlusController/HuePlusController.h \ Controllers/HyperXController/HyperXController.h \ Controllers/LEDStripController/LEDStripController.h \ RGBController/RGBController.h \ RGBController/RGBController_Aura.h \ RGBController/RGBController_Corsair.h \ RGBController/RGBController_CorsairPro.h \ + RGBController/RGBController_HuePlus.h \ RGBController/RGBController_HyperX.h \ RGBController/RGBController_OpenRazer.h diff --git a/RGBController/RGBController_HuePlus.cpp b/RGBController/RGBController_HuePlus.cpp new file mode 100644 index 000000000..30c5611b9 --- /dev/null +++ b/RGBController/RGBController_HuePlus.cpp @@ -0,0 +1,86 @@ +/*-----------------------------------------*\ +| RGBController_HuePlus.cpp | +| | +| Generic RGB Interface for NZXT Hue+ | +| | +| Adam Honse (CalcProgrammer1) 6/20/2019 | +\*-----------------------------------------*/ + +#include "RGBController_HuePlus.h" + + +RGBController_HuePlus::RGBController_HuePlus(HuePlusController* hueplus_ptr) +{ + strip = hueplus_ptr; + + name = "LED Strip"; + + mode led_mode; + led_mode.name = "Custom"; + modes.push_back(led_mode); + + for (int i = 0; i < strip->num_leds; i++) + { + colors.push_back(0x00000000); + led new_led; + new_led.name = "LED Strip"; + leds.push_back(new_led); + } + + zone led_zone; + led_zone.name = "LED Strip"; + std::vector led_zone_map; + for (int i = 0; i < strip->num_leds; i++) + { + led_zone_map.push_back(i); + } + led_zone.map.push_back(led_zone_map); + zones.push_back(led_zone); +} + +int RGBController_HuePlus::GetMode() +{ + return 0; +} + +void RGBController_HuePlus::SetMode(int mode) +{ + +} + +void RGBController_HuePlus::SetCustomMode() +{ + +} + +void RGBController_HuePlus::SetAllLEDs(RGBColor color) +{ + for (int i = 0; i < colors.size(); i++) + { + colors[i] = color; + } + + strip->SetLEDs(colors); +} + +void RGBController_HuePlus::SetAllZoneLEDs(int zone, RGBColor color) +{ + for (int i = 0; i < colors.size(); i++) + { + colors[i] = color; + } + + strip->SetLEDs(colors); +} + +void RGBController_HuePlus::SetLED(int led, RGBColor color) +{ + colors[led] = color; + + strip->SetLEDs(colors); +} + +void RGBController_HuePlus::UpdateLEDs() +{ + strip->SetLEDs(colors); +} \ No newline at end of file diff --git a/RGBController/RGBController_HuePlus.h b/RGBController/RGBController_HuePlus.h new file mode 100644 index 000000000..e92de68f7 --- /dev/null +++ b/RGBController/RGBController_HuePlus.h @@ -0,0 +1,28 @@ +/*-----------------------------------------*\ +| RGBController_HuePlus.h | +| | +| Generic RGB Interface for NZXT Hue+ | +| | +| Adam Honse (CalcProgrammer1) 6/20/2019 | +\*-----------------------------------------*/ + +#pragma once +#include "RGBController.h" +#include "serial_port.h" +#include "HuePlusController.h" + +class RGBController_HuePlus : public RGBController +{ +public: + RGBController_HuePlus(HuePlusController* hueplus_ptr); + int GetMode(); + void SetMode(int mode); + void SetCustomMode(); + void SetAllLEDs(RGBColor color); + void SetAllZoneLEDs(int zone, RGBColor color); + void SetLED(int led, RGBColor color); + void UpdateLEDs(); + +private: + HuePlusController* strip; +}; \ No newline at end of file