Add interface for NZXT Hue+ based on KeyboardVisualizer code and add support for UDP LED strips. Clean up LEDStrip code.
This commit is contained in:
parent
63bf13faac
commit
a8c83e5688
9 changed files with 468 additions and 65 deletions
136
Controllers/HuePlusController/HuePlusController.cpp
Normal file
136
Controllers/HuePlusController/HuePlusController.cpp
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
/*---------------------------------------------------------*\
|
||||
| Processing Code for NZXT Hue+ |
|
||||
| |
|
||||
| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include "HuePlusController.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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<RGBColor> 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;
|
||||
}
|
||||
|
||||
}
|
||||
48
Controllers/HuePlusController/HuePlusController.h
Normal file
48
Controllers/HuePlusController/HuePlusController.h
Normal file
|
|
@ -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 <vector>
|
||||
|
||||
|
||||
#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<RGBColor> 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
|
||||
89
Controllers/HuePlusController/HuePlusControllerDetect.cpp
Normal file
89
Controllers/HuePlusController/HuePlusControllerDetect.cpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#include "HuePlusController.h"
|
||||
#include "RGBController.h"
|
||||
#include "RGBController_HuePlus.h"
|
||||
#include <vector>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
/******************************************************************************************\
|
||||
* *
|
||||
* DetectHuePlusControllers *
|
||||
* *
|
||||
* Detect devices supported by the HuePlus driver *
|
||||
* * *
|
||||
\******************************************************************************************/
|
||||
|
||||
void DetectHuePlusControllers(std::vector<RGBController*> &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() */
|
||||
|
|
@ -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<RGBColor> 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;
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "RGBController.h"
|
||||
#include "serial_port.h"
|
||||
#include "net_port.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
|
@ -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<RGBColor> 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
|
||||
|
|
|
|||
|
|
@ -80,14 +80,6 @@ void DetectLEDStripControllers(std::vector<RGBController*> &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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue