-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathEVisionKeyboardController.cpp
More file actions
279 lines (238 loc) · 9.24 KB
/
EVisionKeyboardController.cpp
File metadata and controls
279 lines (238 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*---------------------------------------------------------*\
| EVisionKeyboardController.cpp |
| |
| Driver for EVision keyboard (Redragon, Glorious, Ajazz, |
| Tecware, and many other brands) |
| |
| Adam Honse (CalcProgrammer1) 15 Mar 2020 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <cstring>
#include "EVisionKeyboardController.h"
#include "StringUtils.h"
EVisionKeyboardController::EVisionKeyboardController(hid_device* dev_handle, const char* path, std::string dev_name)
{
dev = dev_handle;
location = path;
name = dev_name;
}
EVisionKeyboardController::~EVisionKeyboardController()
{
hid_close(dev);
}
std::string EVisionKeyboardController::GetDeviceLocation()
{
return("HID: " + location);
}
std::string EVisionKeyboardController::GetNameString()
{
return(name);
}
std::string EVisionKeyboardController::GetSerialString()
{
wchar_t serial_string[128];
int ret = hid_get_serial_number_string(dev, serial_string, 128);
if(ret != 0)
{
return("");
}
return(StringUtils::wstring_to_string(serial_string));
}
void EVisionKeyboardController::SetKeyboardColors
(
unsigned char * color_data,
unsigned int size
)
{
unsigned int packet_size = 0;
unsigned int packet_offset = 0;
while(size > 0)
{
if(size >= EVISION_KB_MAX_PACKET_SIZE)
{
packet_size = EVISION_KB_MAX_PACKET_SIZE;
}
else
{
packet_size = size;
}
SendKeyboardData
(
&color_data[packet_offset],
packet_size,
packet_offset
);
size -= packet_size;
packet_offset += packet_size;
}
}
void EVisionKeyboardController::SendKeyboardMode
(
unsigned char mode
)
{
SendKeyboardParameter(EVISION_KB_PARAMETER_MODE, 1, &mode);
}
void EVisionKeyboardController::SendKeyboardModeEx
(
unsigned char mode,
unsigned char brightness,
unsigned char speed,
unsigned char direction,
unsigned char random_flag,
unsigned char red,
unsigned char green,
unsigned char blue
)
{
/*-----------------------------------------------------*\
| Build 27-byte compound parameter (param_id = 0x00) |
| as observed in Windows firmware captures |
\*-----------------------------------------------------*/
unsigned char profile[27];
memset(profile, 0x00, sizeof(profile));
profile[0] = 0x00; /* profile index */
profile[1] = mode;
profile[2] = brightness;
profile[3] = speed;
profile[4] = direction;
profile[5] = random_flag;
profile[6] = red;
profile[7] = green;
profile[8] = blue;
profile[18] = 0x03;
profile[22] = 0x03;
SendKeyboardBegin();
SendKeyboardParameter(EVISION_KB_PARAMETER_MODE, sizeof(profile), profile);
SendKeyboardEnd();
}
/*-------------------------------------------------------------------------------------------------*\
| Private packet sending functions. |
\*-------------------------------------------------------------------------------------------------*/
void EVisionKeyboardController::ComputeChecksum
(
char usb_buf[64]
)
{
unsigned short checksum = 0;
for(unsigned int byte_idx = 0x03; byte_idx < 64; byte_idx++)
{
checksum += usb_buf[byte_idx];
}
usb_buf[0x01] = checksum & 0xFF;
usb_buf[0x02] = checksum >> 8;
}
void EVisionKeyboardController::SendKeyboardBegin()
{
char usb_buf[64];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Keyboard Begin (0x01) packet |
| Note: Not computing checksum as packet contents are |
| fixed |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x04;
usb_buf[0x01] = EVISION_KB_COMMAND_BEGIN;
usb_buf[0x02] = 0x00;
usb_buf[0x03] = EVISION_KB_COMMAND_BEGIN;
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 64);
hid_read_timeout(dev, (unsigned char *)usb_buf, 64, 100);
}
void EVisionKeyboardController::SendKeyboardEnd()
{
char usb_buf[64];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Keyboard End (0x02) packet |
| Note: Not computing checksum as packet contents are |
| fixed |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x04;
usb_buf[0x01] = EVISION_KB_COMMAND_END;
usb_buf[0x02] = 0x00;
usb_buf[0x03] = EVISION_KB_COMMAND_END;
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 64);
hid_read_timeout(dev, (unsigned char *)usb_buf, 64, 100);
}
void EVisionKeyboardController::SendKeyboardData
(
unsigned char * data,
unsigned char data_size,
unsigned int data_offset
)
{
char usb_buf[64];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Keyboard Color Data (0x12) packet |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x04;
usb_buf[0x03] = 0x12;
usb_buf[0x04] = data_size;
usb_buf[0x05] = data_offset & 0xFF;
usb_buf[0x06] = (data_offset >> 8) & 0xFF;
usb_buf[0x07] = (data_offset >> 16) & 0xFF;
/*-----------------------------------------------------*\
| Copy in data bytes |
\*-----------------------------------------------------*/
memcpy(&usb_buf[0x08], data, data_size);
/*-----------------------------------------------------*\
| Compute Checksum |
\*-----------------------------------------------------*/
ComputeChecksum(usb_buf);
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 64);
hid_read_timeout(dev, (unsigned char *)usb_buf, 64, 100);
}
void EVisionKeyboardController::SendKeyboardParameter
(
unsigned char parameter,
unsigned char parameter_size,
unsigned char* parameter_data
)
{
char usb_buf[64];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Keyboard Parameter (0x06) packet |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x04;
usb_buf[0x03] = EVISION_KB_COMMAND_SET_PARAMETER;
usb_buf[0x04] = parameter_size;
usb_buf[0x05] = parameter;
/*-----------------------------------------------------*\
| Copy in data bytes |
\*-----------------------------------------------------*/
memcpy(&usb_buf[0x08], parameter_data, parameter_size);
/*-----------------------------------------------------*\
| Compute Checksum |
\*-----------------------------------------------------*/
ComputeChecksum(usb_buf);
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 64);
hid_read_timeout(dev, (unsigned char *)usb_buf, 64, 100);
}