Skip to content

Commit 06ba2db

Browse files
Bl4d3sCalcProgrammer1
authored andcommitted
Hue+ integration (pull request #84) from Bl4d3s/KeyboardVisualizer
1 parent a4c220b commit 06ba2db

File tree

6 files changed

+172
-1
lines changed

6 files changed

+172
-1
lines changed

KeyboardVisualizerCommon/LEDStrip.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,54 @@ void LEDStrip::Initialize(char* ledstring)
8585

8686
}
8787

88+
void LEDStrip::InitializeHuePlus(char* ledstring)
89+
{
90+
strcpy(led_string, ledstring);
91+
92+
LPSTR source = NULL;
93+
LPSTR numleds = NULL;
94+
LPSTR next = NULL;
95+
96+
source = strtok_s(ledstring, ",", &next);
97+
98+
//Check for the number of LEDs, sets the corresponding variable with the counter for the fans
99+
if (strlen(next))
100+
{
101+
numleds = strtok_s(next, ",", &next);
102+
}
103+
104+
switch (atoi(numleds) / 8)
105+
{
106+
case 1:
107+
fans = 0x00;
108+
break;
109+
110+
case 2:
111+
fans = 0x01;
112+
break;
113+
114+
case 3:
115+
fans = 0x02;
116+
break;
117+
118+
case 4:
119+
fans = 0x03;
120+
break;
121+
122+
case 5:
123+
fans = 0x04;
124+
break;
125+
}
126+
127+
//Initialize with default baud rate
128+
InitializeSerial(source, 256000);
129+
130+
if (numleds != NULL && strlen(numleds))
131+
{
132+
SetNumLEDs(atoi(numleds));
133+
}
134+
}
135+
88136
void LEDStrip::InitializeSerial(char* portname, int baud)
89137
{
90138
portname = strtok(portname, "\r");
@@ -220,3 +268,40 @@ void LEDStrip::SetLEDsXmas(COLORREF pixels[64][256])
220268
serialport->serial_write((char *)xmas_buf, 5*25);
221269
serialport->serial_flush_tx();
222270
};
271+
272+
void LEDStrip::SetLEDsHuePlus(COLORREF pixels[64][256])
273+
{
274+
if (serialport != NULL)
275+
{
276+
unsigned char *serial_buf;
277+
278+
serial_buf = new unsigned char[250]; //Size of Message always 5 XX Blocks (Mode Selection) + 3 XX for each LED (1 color)
279+
//-> max of 40 LEDs per Channel (or 5 Fans a 8 LEDs) -> 125 Blocks (empty LEDs are written, too)
280+
281+
serial_buf[0] = 0x4b;
282+
serial_buf[1] = 0x00;
283+
serial_buf[2] = 0x0e;
284+
serial_buf[3] = fans;
285+
serial_buf[4] = 0x00;
286+
287+
for (int i = 5; i < 250; i++)
288+
{
289+
//clearing the buf otherwise sometimes strange things are written to the COM Port
290+
serial_buf[i] = 0x00;
291+
}
292+
293+
for (int idx = 0; idx < (num_leds * 3); idx += 3)
294+
{
295+
int pixel_idx = idx / 3;
296+
COLORREF color = pixels[LEDStripYIndex[pixel_idx]][LEDStripXIndex[pixel_idx]];
297+
serial_buf[idx + 5] = GetGValue(color);
298+
serial_buf[idx + 6] = GetRValue(color);
299+
serial_buf[idx + 7] = GetBValue(color);
300+
}
301+
302+
serialport->serial_write((char *)serial_buf,250);
303+
serialport->serial_flush_tx();
304+
305+
delete[] serial_buf;
306+
}
307+
}

KeyboardVisualizerCommon/LEDStrip.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ class LEDStrip
2424
~LEDStrip();
2525

2626
void Initialize(char* ledstring);
27+
void InitializeHuePlus(char * ledstring);
2728
void InitializeSerial(char* portname, int baud);
2829
void InitializeUDP(char* clientname, char* port);
2930
char* GetLEDString();
3031
void SetNumLEDs(int numleds);
3132
void SetLEDs(COLORREF pixels[64][256]);
3233
void SetLEDsXmas(COLORREF pixels[64][256]);
34+
void SetLEDsHuePlus(COLORREF pixels[64][256]);
3335

3436
private:
3537
int baud_rate;
3638
int num_leds;
39+
int fans;
3740

3841
int * LEDStripXIndex;
3942
int * LEDStripYIndex;

KeyboardVisualizerCommon/Visualizer.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ SteelSeriesGameSense skb;
5656
MSIKeyboard mkb;
5757
std::vector<LEDStrip *> str;
5858
std::vector<LEDStrip *> xmas;
59+
std::vector<LEDStrip *> huePlus;
5960

6061
std::vector<char *> device_properties;
6162

@@ -168,6 +169,13 @@ void Visualizer::AddLEDStrip(char* ledstring)
168169
return;
169170
}
170171
}
172+
for (unsigned int i = 0; i < huePlus.size(); i++)
173+
{
174+
if (strcmp(huePlus[i]->GetLEDString(), ledstring) == 0)
175+
{
176+
return;
177+
}
178+
}
171179

172180
LEDStrip *newstr = new LEDStrip();
173181
newstr->Initialize(ledstring);
@@ -192,12 +200,50 @@ void Visualizer::AddLEDStripXmas(char* ledstring)
192200
return;
193201
}
194202
}
203+
for (unsigned int i = 0; i < huePlus.size(); i++)
204+
{
205+
if (strcmp(huePlus[i]->GetLEDString(), ledstring) == 0)
206+
{
207+
return;
208+
}
209+
}
195210

196211
LEDStrip *newstr = new LEDStrip();
197212
newstr->Initialize(ledstring);
198213
xmas.push_back(newstr);
199214
}
200215

216+
void Visualizer::AddLEDStripHuePlus(char* ledstring)
217+
{
218+
//Scan through already registered LED strips and
219+
//verify that the port name is not already in use
220+
for (unsigned int i = 0; i < str.size(); i++)
221+
{
222+
if (strcmp(str[i]->GetLEDString(), ledstring) == 0)
223+
{
224+
return;
225+
}
226+
}
227+
for (unsigned int i = 0; i < xmas.size(); i++)
228+
{
229+
if (strcmp(xmas[i]->GetLEDString(), ledstring) == 0)
230+
{
231+
return;
232+
}
233+
}
234+
for (unsigned int i = 0; i < huePlus.size(); i++)
235+
{
236+
if (strcmp(huePlus[i]->GetLEDString(), ledstring) == 0)
237+
{
238+
return;
239+
}
240+
}
241+
242+
LEDStrip *newstr = new LEDStrip();
243+
newstr->InitializeHuePlus(ledstring);
244+
huePlus.push_back(newstr);
245+
}
246+
201247
void Visualizer::SetDeviceProperty(char * devprop)
202248
{
203249
//Save device property to list of device properties
@@ -612,6 +658,14 @@ void Visualizer::SaveSettings()
612658
outfile.write(out_str, strlen(out_str));
613659
}
614660

661+
//Save HuePlus Configurations
662+
for (unsigned int i = 0; i < huePlus.size(); i++)
663+
{
664+
//Save HuePlus Configuration
665+
snprintf(out_str, 1024, "hueplus=%s\r\n", huePlus[i]->GetLEDString());
666+
outfile.write(out_str, strlen(out_str));
667+
}
668+
615669
//Save Network Mode
616670
switch (netmode)
617671
{
@@ -1604,7 +1658,7 @@ void Visualizer::MSIKeyboardUpdateThread()
16041658

16051659
void Visualizer::LEDStripUpdateThread()
16061660
{
1607-
if (str.size() > 0 || xmas.size() > 0)
1661+
if (str.size() > 0 || xmas.size() > 0 || huePlus.size() > 0)
16081662
{
16091663
while (TRUE)
16101664
{
@@ -1618,6 +1672,11 @@ void Visualizer::LEDStripUpdateThread()
16181672
xmas[i]->SetLEDsXmas(pixels_out->pixels);
16191673
}
16201674

1675+
for (unsigned int i = 0; i < huePlus.size(); i++)
1676+
{
1677+
huePlus[i]->SetLEDsHuePlus(pixels_out->pixels);
1678+
}
1679+
16211680
if (delay < 15)
16221681
{
16231682
Sleep(15);

KeyboardVisualizerCommon/Visualizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Visualizer
121121
//Add LED strip
122122
void AddLEDStrip(char* ledstring);
123123
void AddLEDStripXmas(char * ledstring);
124+
void AddLEDStripHuePlus(char * ledstring);
124125

125126
//Function to set custom properties for devices
126127
void SetDeviceProperty(char * devprop);

KeyboardVisualizerQT/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ void parse_argument_string(char * argument, char * value)
118118
{
119119
vis.AddLEDStripXmas(value);
120120
}
121+
else if (strcmp(argument, "hueplus") == 0)
122+
{
123+
vis.AddLEDStripHuePlus(value);
124+
}
121125
else if (strcmp(argument, "server") == 0)
122126
{
123127
vis.InitServer(value);
@@ -241,6 +245,13 @@ bool parse_command_line(int argc, char *argv[])
241245
printf(" - UDP : ledstrip=udp:client,port,num_leds\r\n");
242246
printf(" - (ex.ledstrip=udp:192.168.1.5,1234,30)\r\n");
243247
printf(" xmas - COM port, ex. xmas=COM2\r\n");
248+
printf(" hueplus - HUE+ config:\r\n");
249+
printf(" - hueplus=port,num_leds\r\n");
250+
printf(" - num_leds: Fans * 8 ex. 3 Fans -> 24\r\n");
251+
printf(" - Important for Fans: If you have connected fans on both channels only count the fans on the channel with the most fans\r\n");
252+
printf(" ex. 3 Fans on Ch. 1 4 Fans on CH. 2: num_leds 32 for the 4 Fans\r\n");
253+
printf(" For best Visualizer results don`t connect on one channel 3 fans more than on the other channel\r\n");
254+
printf(" - (ex. hueplus=COM4,24\r\n");
244255
return false;
245256
}
246257

KeyboardVisualizerVC/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ void parse_argument_string(char * argument, char * value)
136136
vis.AddLEDStripXmas(value);
137137
}
138138

139+
if (strcmp(argument, "hueplus") == 0)
140+
{
141+
vis.AddLEDStripHuePlus(value);
142+
}
143+
139144
if (strcmp(argument, "server") == 0)
140145
{
141146
vis.InitServer(value);
@@ -275,6 +280,13 @@ boolean parse_command_line(char * command_line)
275280
printf(" - UDP : ledstrip=udp:client,port,num_leds\r\n");
276281
printf(" - (ex.ledstrip=udp:192.168.1.5,1234,30)\r\n");
277282
printf(" xmas - COM port, ex. xmas=COM2\r\n");
283+
printf(" hueplus - HUE+ config:\r\n");
284+
printf(" - hueplus=port,num_leds\r\n");
285+
printf(" - num_leds: Fans * 8 ex. 3 Fans -> 24\r\n");
286+
printf(" - Important for Fans: If you have connected fans on both channels only count the fans on the channel with the most fans\r\n");
287+
printf(" ex. 3 Fans on Ch. 1 4 Fans on CH. 2: num_leds 32 for the 4 Fans\r\n");
288+
printf(" For best Visualizer results don`t connect on one channel 3 fans more than on the other channel\r\n");
289+
printf(" - (ex. hueplus=COM4,24\r\n");
278290
return FALSE;
279291
}
280292

0 commit comments

Comments
 (0)