forked from Sayrus/msi-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.cpp
220 lines (212 loc) · 6.11 KB
/
keyboard.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "hidapi.h"
#include "keyboard.h"
#define BUFFER_SIZE 8
e_mode ConvertToEMode(char* mode)
{
if(!mode)
return e_mode::MODE_NORMAL;
if(strcmp(mode, "normal") == 0)
return e_mode::MODE_NORMAL;
if(strcmp(mode, "gaming") == 0)
return e_mode::MODE_GAMING;
if(strcmp(mode, "breath") == 0)
return e_mode::MODE_BREATH;
if(strcmp(mode, "audio") == 0)
return e_mode::MODE_AUDIO;
if(strcmp(mode, "wave") == 0)
return e_mode::MODE_WAVE;
if(strcmp(mode, "dual") == 0)
return e_mode::MODE_DUAL;
return e_mode::MODE_NORMAL;
}
e_region ConvertToERegion(char* region)
{
if(!region)
return e_region::REGION_MIDDLE;
if(strcmp(region, "left") == 0)
return e_region::REGION_LEFT;
if(strcmp(region, "right") == 0)
return e_region::REGION_RIGHT;
return e_region::REGION_MIDDLE;
}
e_color ConvertToEColor(char* color)
{
if(!color)
return e_color::COLOR_NONE;
if(strcmp(color, "red") == 0)
return e_color::COLOR_RED;
if(strcmp(color, "orange") == 0)
return e_color::COLOR_ORANGE;
if(strcmp(color, "yellow") == 0)
return e_color::COLOR_YELLOW;
if(strcmp(color, "green") == 0)
return e_color::COLOR_GREEN;
if(strcmp(color, "sky") == 0)
return e_color::COLOR_SKY;
if(strcmp(color, "blue") == 0)
return e_color::COLOR_BLUE;
if(strcmp(color, "purple") == 0)
return e_color::COLOR_PURPLE;
if(strcmp(color, "white") == 0)
return e_color::COLOR_WHITE;
return e_color::COLOR_NONE;
}
e_intensity ConvertToEIntensity(char* intensity)
{
if(!intensity)
return e_intensity::INTENSITY_HIGH;
if(strcmp(intensity, "medium") == 0)
return e_intensity::INTENSITY_MEDIUM;
if(strcmp(intensity, "low") == 0)
return e_intensity::INTENSITY_LOW;
if(strcmp(intensity, "light") == 0)
return e_intensity::INTENSITY_LIGHT;
return e_intensity::INTENSITY_HIGH;
}
Keyboard::Keyboard() :
handle(NULL)
{
handle = hid_open(0x1770, 0xff00, 0);
if(!handle)
{
printf("Cannot open USB device");
return;
}
}
Keyboard::~Keyboard()
{
if(handle)
{
hid_close(handle);
}
}
void Keyboard::debug(unsigned char buffer[])
{
if(!handle)
return;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
}
void Keyboard::setColors(e_color primaryColor, e_intensity primaryIntensity, e_color secondaryColor, e_intensity secondaryIntensity)
{
if(!handle)
return;
unsigned char buffer[BUFFER_SIZE] = {0};
buffer[0] = 1;
buffer[1] = 2;
buffer[2] = 67;
buffer[7] = 236;
for(int i = 1; i < 9; i += 3)
{
buffer[3] = i;
buffer[4] = static_cast<unsigned int>(primaryColor);
buffer[5] = static_cast<unsigned int>(primaryIntensity);
buffer[6] = 0;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
buffer[3] = i + 1;
buffer[4] = static_cast<unsigned int>(secondaryColor);
buffer[5] = static_cast<unsigned int>(secondaryIntensity);
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
buffer[3] = i + 2;
// I will keep buffer[4-6] = 2 until I figure out what changing one of them does to the keyboard
buffer[4] = 2;
buffer[5] = 2;
buffer[6] = 2;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
}
}
void Keyboard::setMode(e_mode mode)
{
if(!handle)
return;
unsigned char buffer[BUFFER_SIZE] = {0};
buffer[0] = 1;
buffer[1] = 2;
buffer[2] = 65;
buffer[3] = static_cast<unsigned int>(mode);
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0;
buffer[7] = 236;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
}
void Keyboard::setMode(char* mode)
{
Keyboard::setMode(ConvertToEMode(mode));
}
void Keyboard::setColor(e_region region, e_color color, e_intensity intensity)
{
if(!handle)
return;
unsigned char buffer[BUFFER_SIZE] = {0};
buffer[0] = 1;
buffer[1] = 2;
buffer[2] = 66;
buffer[3] = static_cast<unsigned int>(region);
buffer[4] = static_cast<unsigned int>(color);
buffer[5] = static_cast<unsigned int>(intensity);
buffer[6] = 0;
buffer[7] = 236;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
}
void Keyboard::setColor(e_region region, s_color rgb)
{
if(!handle)
return;
unsigned char buffer[BUFFER_SIZE] = {0};
buffer[0] = 1;
buffer[1] = 2;
buffer[2] = 64;
buffer[3] = static_cast<unsigned int>(region);
buffer[4] = rgb.red;
buffer[5] = rgb.green;
buffer[6] = rgb.blue;
buffer[7] = 236;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
}
void Keyboard::setDualColor(s_pairColor primary, s_pairColor secondary, e_region region, double period)
{
int index = static_cast<int>(region) * 3 - 2;
int data1 = Keyboard::ComputeRampSpeed(
Keyboard::colorPalette[static_cast<int>(primary.color)][static_cast<int>(primary.intensity)][1],
Keyboard::colorPalette[static_cast<int>(secondary.color)][static_cast<int>(secondary.intensity)][1],
period
);
int data2 = Keyboard::ComputeRampSpeed(
Keyboard::colorPalette[static_cast<int>(primary.color)][static_cast<int>(primary.intensity)][2],
Keyboard::colorPalette[static_cast<int>(secondary.color)][static_cast<int>(secondary.intensity)][2],
period
);
int data3 = Keyboard::ComputeRampSpeed(
Keyboard::colorPalette[static_cast<int>(primary.color)][static_cast<int>(primary.intensity)][3],
Keyboard::colorPalette[static_cast<int>(secondary.color)][static_cast<int>(secondary.intensity)][3],
period
);
unsigned char buffer[BUFFER_SIZE] = {0};
buffer[0] = 1;
buffer[1] = 2;
buffer[2] = 67;
buffer[3] = index;
buffer[4] = static_cast<int>(primary.color);
buffer[5] = static_cast<int>(primary.intensity);
buffer[6] = 0;
buffer[7] = 236;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
buffer[3]++;
buffer[4] = static_cast<int>(secondary.color);
buffer[5] = static_cast<int>(secondary.intensity);
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
buffer[3]++;
buffer[4] = data1;
buffer[5] = data2;
buffer[6] = data3;
hid_send_feature_report(handle, buffer, BUFFER_SIZE);
}
int Keyboard::ComputeRampSpeed(double comp1, double comp2, double period)
{
if(comp1 == comp2)
return 0;
return static_cast<int>(ceil(250.0 / (abs(comp1 - comp2) / period)));
}