Skip to content

Commit 6d1f896

Browse files
thombo2CalcProgrammer1
authored andcommitted
Support for Corsair K55 RGB PRO XT (fixes issue #2555)
1 parent 38dd53b commit 6d1f896

File tree

6 files changed

+888
-0
lines changed

6 files changed

+888
-0
lines changed
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
/*--------------------------------------------*\
2+
| CorsairK55RGBPROXTController.cpp |
3+
| |
4+
| Driver for Corsair K55 RGB PRO XT Keyboard |
5+
\*--------------------------------------------*/
6+
7+
#include "CorsairK55RGBPROXTController.h"
8+
#include "LogManager.h"
9+
10+
11+
#define COLOR_BANK_SIZE 137
12+
#define HID_PACKET_LENGTH 65
13+
#define HID_PAYLOAD_SIZE1 (HID_PACKET_LENGTH - 12)
14+
#define HID_PAYLOAD_SIZE2 (HID_PACKET_LENGTH - 4)
15+
16+
17+
static const unsigned int keys[] =
18+
{ 127, 128, 129, 130, 131, 132, 37, 49, 39, 53, 102, 101, 26, 96, 104, 54, 27, 16, 0, 25,
19+
103, 55, 28, 22, 18, 23, 56, 29, 4, 3, 2, 57, 30, 17, 5, 21, 31, 58, 32, 19,
20+
6, 1, 40, 59, 33, 24, 7, 60, 34, 20, 9, 13, 61, 35, 8, 10, 12, 14, 11, 50,
21+
62, 41, 15, 47, 51, 107, 63, 42, 43, 48, 52, 118, 64, 38, 44, 46, 106, 97, 65, 36,
22+
105, 66, 69, 72, 76, 67, 70, 73, 78, 77, 68, 71, 74, 75, 79, 91, 88, 85, 94, 80,
23+
92, 89, 86, 81, 93, 90, 87, 95, 82, 83, 84 };
24+
25+
26+
static unsigned char color_bank[3][COLOR_BANK_SIZE];
27+
28+
static const unsigned char filler[] =
29+
{ 0x70, 0x6E, 0x4E, 0x4D, 0x4C, 0x65, 0x6F, 0x2C, 0x4B, 0x4A, 0x49, 0x48, 0x47, 0x46, 0x45, 0x05,
30+
0x19, 0x06, 0x1B, 0x1D, 0x64, 0x6A, 0x34, 0x6B, 0x6C, 0x69, 0x38, 0x37, 0x36, 0x10, 0x11, 0x16,
31+
0x04, 0x39, 0x2F, 0x13, 0x12, 0x0C, 0x18, 0x33, 0x0F, 0x0E, 0x0D, 0x0B, 0x0A, 0x09, 0x07, 0x27,
32+
0x7A, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1C, 0x17, 0x15, 0x08, 0x1A, 0x14, 0x2B, 0x2D,
33+
0x3F, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x29, 0x1F, 0x1E, 0x35, 0x44, 0x43, 0x42, 0x41, 0x40, 0x62,
34+
0x00, 0x5B, 0x5A, 0x59, 0x5E, 0x5D, 0x5C, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x63, 0x53, 0x4F,
35+
0x61, 0x60, 0x5F, 0x58, 0x57, 0x56, 0x55, 0x54, 0x2A, 0x2E, 0x28, 0x32, 0x30, 0x51, 0x50, 0x52,
36+
0x6D };
37+
38+
39+
CorsairK55RGBPROXTController::CorsairK55RGBPROXTController(hid_device* dev_handle, const char* path)
40+
{
41+
dev = dev_handle;
42+
location = path;
43+
44+
LightingControl();
45+
}
46+
47+
CorsairK55RGBPROXTController::~CorsairK55RGBPROXTController()
48+
{
49+
hid_close(dev);
50+
}
51+
52+
std::string CorsairK55RGBPROXTController::GetDeviceLocation()
53+
{
54+
return("HID: " + location);
55+
}
56+
57+
std::string CorsairK55RGBPROXTController::GetFirmwareString()
58+
{
59+
return "";
60+
}
61+
62+
std::string CorsairK55RGBPROXTController::GetSerialString()
63+
{
64+
wchar_t serial_string[128];
65+
int ret = hid_get_serial_number_string(dev, serial_string, 128);
66+
67+
if(ret != 0)
68+
{
69+
return("");
70+
}
71+
72+
std::wstring return_wstring = serial_string;
73+
std::string return_string(return_wstring.begin(), return_wstring.end());
74+
75+
return(return_string);
76+
}
77+
78+
void CorsairK55RGBPROXTController::LightingControl()
79+
{
80+
unsigned char usb_buf[HID_PACKET_LENGTH];
81+
82+
memset(usb_buf, 0x00, sizeof(usb_buf));
83+
usb_buf[0x01] = 0x08;
84+
usb_buf[0x02] = 0x01;
85+
usb_buf[0x03] = 0x03;
86+
usb_buf[0x05] = 0x02;
87+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
88+
89+
memset(usb_buf, 0x00, sizeof(usb_buf));
90+
usb_buf[0x01] = 0x08;
91+
usb_buf[0x02] = 0x02;
92+
usb_buf[0x03] = 0x5F;
93+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
94+
95+
memset(usb_buf, 0x00, sizeof(usb_buf));
96+
usb_buf[0x01] = 0x08;
97+
usb_buf[0x02] = 0x0D;
98+
usb_buf[0x04] = 0x01;
99+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
100+
}
101+
102+
void CorsairK55RGBPROXTController::SetLEDs(std::vector<RGBColor>colors)
103+
{
104+
for(std::size_t color_idx = 0; color_idx < colors.size(); ++color_idx)
105+
{
106+
RGBColor color = colors[color_idx];
107+
color_bank[0][keys[color_idx]] = RGBGetRValue(color);
108+
color_bank[1][keys[color_idx]] = RGBGetGValue(color);
109+
color_bank[2][keys[color_idx]] = RGBGetBValue(color);
110+
}
111+
112+
unsigned char* color_ptr = &color_bank[0][0];
113+
unsigned char usb_buf[HID_PACKET_LENGTH];
114+
memset(usb_buf, 0x00, sizeof(usb_buf));
115+
116+
usb_buf[0x01] = 0x08;
117+
usb_buf[0x02] = 0x06;
118+
usb_buf[0x04] = 0x9B;
119+
usb_buf[0x05] = 0x01;
120+
121+
memcpy(&usb_buf[12], color_ptr, HID_PAYLOAD_SIZE1);
122+
color_ptr += HID_PAYLOAD_SIZE1;
123+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
124+
125+
usb_buf[0x02] = 0x07;
126+
127+
for(std::size_t i = 0; i < 6; ++i)
128+
{
129+
memcpy(&usb_buf[4], color_ptr, HID_PAYLOAD_SIZE2);
130+
color_ptr += HID_PAYLOAD_SIZE2;
131+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
132+
}
133+
}
134+
135+
void CorsairK55RGBPROXTController::SetHardwareMode
136+
(
137+
int mode_value,
138+
unsigned int color_mode,
139+
std::vector<RGBColor> colors,
140+
unsigned int speed,
141+
unsigned int direction
142+
)
143+
{
144+
LightingControl();
145+
146+
unsigned char usb_buf[HID_PACKET_LENGTH];
147+
memset(usb_buf, 0x00, sizeof(usb_buf));
148+
usb_buf[0x01] = 0x08;
149+
usb_buf[0x02] = 0x0D;
150+
usb_buf[0x03] = 0x01;
151+
usb_buf[0x04] = 0x61;
152+
usb_buf[0x05] = 0x6D;
153+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
154+
155+
memset(usb_buf, 0x00, sizeof(usb_buf));
156+
usb_buf[0x01] = 0x08;
157+
usb_buf[0x02] = 0x09;
158+
usb_buf[0x03] = 0x01;
159+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
160+
161+
memset(usb_buf, 0x00, sizeof(usb_buf));
162+
usb_buf[0x01] = 0x08;
163+
usb_buf[0x02] = 0x06;
164+
usb_buf[0x03] = 0x01;
165+
usb_buf[0x04] = 0x78;
166+
usb_buf[0x08] = mode_value & 0x00FF;
167+
usb_buf[0x09] = mode_value >> 8;
168+
169+
if(color_mode != MODE_COLORS_NONE)
170+
{
171+
usb_buf[0x0A] = color_mode == MODE_COLORS_RANDOM ? CORSAIR_HW_MODE_COLOR_RANDOM : CORSAIR_HW_MODE_COLOR_PREDEF;
172+
}
173+
174+
if(mode_value == CORSAIR_HW_MODE_WATER_COLOR_VALUE)
175+
{
176+
usb_buf[0x0A] = CORSAIR_HW_MODE_COLOR_UNKNOWN;
177+
}
178+
179+
usb_buf[0x0B] = speed;
180+
181+
if((mode_value == CORSAIR_HW_MODE_COLOR_WAVE_VALUE) ||
182+
(mode_value == CORSAIR_HW_MODE_RAINBOW_WAVE_VALUE) ||
183+
(mode_value == CORSAIR_HW_MODE_RAIN_VALUE) ||
184+
(mode_value == CORSAIR_HW_MODE_SPIRAL_VALUE) ||
185+
(mode_value == CORSAIR_HW_MODE_VISOR_VALUE))
186+
{
187+
switch(direction)
188+
{
189+
case MODE_DIRECTION_LEFT:
190+
if(mode_value == CORSAIR_HW_MODE_SPIRAL_VALUE)
191+
{
192+
usb_buf[0x0C] = CORSAIE_HW_MODE_DIR_COUNTER_CLOCK_WISE;
193+
}
194+
else
195+
{
196+
usb_buf[0x0C] = CORSAIR_HW_MODE_DIR_LEFT;
197+
}
198+
break;
199+
200+
case MODE_DIRECTION_RIGHT:
201+
if(mode_value == CORSAIR_HW_MODE_SPIRAL_VALUE)
202+
{
203+
usb_buf[0x0C] = CORSAIR_HW_MODE_DIR_CLOCK_WISE;
204+
}
205+
else
206+
{
207+
usb_buf[0x0C] = CORSAIR_HW_MODE_DIR_RIGHT;
208+
}
209+
break;
210+
211+
case MODE_DIRECTION_UP:
212+
usb_buf[0x0C] = CORSAIR_HW_MODE_DIR_UP;
213+
break;
214+
215+
case MODE_DIRECTION_DOWN:
216+
usb_buf[0x0C] = CORSAIR_HW_MODE_DIR_DOWN;
217+
break;
218+
219+
default:
220+
usb_buf[0x0C] = CORSAIR_HW_MODE_DIR_NONE;
221+
break;
222+
}
223+
}
224+
225+
int fill_dest_index = 0x0F;
226+
int fill_src_index = 0x00;
227+
228+
if(usb_buf[0x0A] != CORSAIR_HW_MODE_COLOR_RANDOM)
229+
{
230+
usb_buf[0x0E] = (unsigned char)colors.size();
231+
232+
for(size_t i = 0; i < colors.size(); ++i)
233+
{
234+
usb_buf[0x0F + i * 4] = 0xFF;
235+
usb_buf[0x10 + i * 4] = RGBGetBValue(colors[i]);
236+
usb_buf[0x11 + i * 4] = RGBGetGValue(colors[i]);
237+
usb_buf[0x12 + i * 4] = RGBGetRValue(colors[i]);
238+
usb_buf[4] += 4;
239+
fill_dest_index += 4;
240+
}
241+
}
242+
243+
memcpy(&usb_buf[fill_dest_index], &filler[fill_src_index], HID_PACKET_LENGTH - fill_dest_index);
244+
fill_src_index += (HID_PACKET_LENGTH - fill_dest_index);
245+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
246+
247+
memset(usb_buf, 0x00, sizeof(usb_buf));
248+
usb_buf[0x01] = 0x08;
249+
usb_buf[0x02] = 0x07;
250+
usb_buf[0x03] = 0x01;
251+
memcpy(&usb_buf[4], &filler[fill_src_index], HID_PACKET_LENGTH - 4);
252+
fill_src_index += (HID_PACKET_LENGTH - 4);
253+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
254+
255+
memset(usb_buf, 0x00, sizeof(usb_buf));
256+
usb_buf[0x01] = 0x08;
257+
usb_buf[0x02] = 0x07;
258+
usb_buf[0x03] = 0x01;
259+
memcpy(&usb_buf[4], &filler[fill_src_index], sizeof(filler) - fill_src_index);
260+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
261+
262+
memset(usb_buf, 0x00, sizeof(usb_buf));
263+
usb_buf[0x01] = 0x08;
264+
usb_buf[0x02] = 0x05;
265+
usb_buf[0x03] = 0x01;
266+
usb_buf[0x04] = 0x01;
267+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
268+
}
269+
270+
void CorsairK55RGBPROXTController::SwitchMode(bool software)
271+
{
272+
if(software)
273+
{
274+
LightingControl();
275+
}
276+
else
277+
{
278+
unsigned char usb_buf[HID_PACKET_LENGTH];
279+
280+
memset(usb_buf, 0x00, sizeof(usb_buf));
281+
usb_buf[0x01] = 0x00;
282+
usb_buf[0x02] = 0x01;
283+
usb_buf[0x03] = 0x03;
284+
usb_buf[0x05] = 0x01;
285+
hid_write(dev, (unsigned char *)usb_buf, HID_PACKET_LENGTH);
286+
}
287+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*--------------------------------------------*\
2+
| CorsairK55RGBPROXTController.h |
3+
| |
4+
| Driver for Corsair K55 RGB PRO XT Keyboard |
5+
\*--------------------------------------------*/
6+
7+
#ifndef CORSAIRK55RGBPROXTCONTROLLER_H
8+
#define CORSAIRK55RGBPROXTCONTROLLER_H
9+
10+
#include "RGBController.h"
11+
12+
#include <string>
13+
#include <hidapi/hidapi.h>
14+
15+
16+
class CorsairK55RGBPROXTController
17+
{
18+
public:
19+
CorsairK55RGBPROXTController(hid_device* dev_handle, const char* path);
20+
~CorsairK55RGBPROXTController();
21+
std::string GetDeviceLocation();
22+
std::string GetFirmwareString();
23+
std::string GetSerialString();
24+
25+
void SetLEDs(std::vector<RGBColor> colors);
26+
void SetHardwareMode
27+
(
28+
int mode_value,
29+
unsigned int color_mode,
30+
std::vector<RGBColor> colors,
31+
unsigned int speed,
32+
unsigned int direction
33+
);
34+
void SwitchMode(bool software);
35+
36+
37+
enum
38+
{
39+
CORSAIR_MODE_DIRECT_VALUE = 0xFFFF,
40+
CORSAIR_HW_MODE_STATIC_VALUE = 0x207E,
41+
CORSAIR_HW_MODE_COLOR_PULSE_VALUE = 0xAD4F,
42+
CORSAIR_HW_MODE_COLOR_SHIFT_VALUE = 0xA5FA,
43+
CORSAIR_HW_MODE_COLOR_WAVE_VALUE = 0x7BFF,
44+
CORSAIR_HW_MODE_RAINBOW_WAVE_VALUE = 0xB94C,
45+
CORSAIR_HW_MODE_RAIN_VALUE = 0xA07E,
46+
CORSAIR_HW_MODE_SPIRAL_VALUE = 0xAB87,
47+
CORSAIR_HW_MODE_WATER_COLOR_VALUE = 0x0022,
48+
CORSAIR_HW_MODE_TYPE_KEY_VALUE = 0xB1F9,
49+
CORSAIR_HW_MODE_TYPE_RIPPLE_VALUE = 0x09A2,
50+
CORSAIR_HW_MODE_VISOR_VALUE = 0x90c0
51+
};
52+
53+
enum
54+
{
55+
CORSAIR_HW_MODE_COLOR_NONE = 0x00,
56+
CORSAIR_HW_MODE_COLOR_PREDEF = 0x01,
57+
CORSAIR_HW_MODE_COLOR_RANDOM = 0x02,
58+
CORSAIR_HW_MODE_COLOR_UNKNOWN = 0x03
59+
};
60+
61+
enum
62+
{
63+
CORSAIR_HW_MODE_SPEED_NONE = 0x00,
64+
CORSAIR_HW_MODE_SPEED_MIN = 0x03,
65+
CORSAIR_HW_MODE_SPEED_MED = 0x04,
66+
CORSAIR_HW_MODE_SPEED_MAX = 0x05
67+
};
68+
69+
enum
70+
{
71+
CORSAIR_HW_MODE_DIR_NONE = 0x00,
72+
CORSAIR_HW_MODE_DIR_DOWN = 0x01,
73+
CORSAIR_HW_MODE_DIR_UP = 0x02,
74+
CORSAIR_HW_MODE_DIR_RIGHT = 0x04,
75+
CORSAIR_HW_MODE_DIR_LEFT = 0x05,
76+
CORSAIR_HW_MODE_DIR_CLOCK_WISE = 0x06,
77+
CORSAIE_HW_MODE_DIR_COUNTER_CLOCK_WISE = 0x07
78+
};
79+
80+
private:
81+
hid_device* dev;
82+
83+
std::string firmware_version;
84+
std::string location;
85+
device_type type;
86+
87+
void LightingControl();
88+
};
89+
90+
#endif // CORSAIRK55RGBPROXTCONTROLLER_H

0 commit comments

Comments
 (0)