-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgmmk.c
327 lines (263 loc) · 9.8 KB
/
gmmk.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright 2021 paulguy <[email protected]>
*
* This file is part of gmmkctl.
*
* gmmkctl is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gmmkctl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gmmkctl. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gmmk.h"
#define GMMK_VENDOR_ID (0x0C45)
#define GMMK_PRODUCT_ID (0x652F)
#define GMMK_LED_INTERFACE (1)
#define GMMK_OUT_ENDPOINT (0x03)
#define GMMK_IN_ENDPOINT (0x82)
#define GMMK_REPORT_ID (4)
#define GMMK_TIMEOUT_MS (1000)
#define GMMK_SUM_OFFSET (1)
#define GMMK_COMMAND_OFFSET (3)
#define GMMK_CMD_START (1)
#define GMMK_CMD_END (2)
#define GMMK_CMD_KEYCOLORS (0x11)
#define GMMK_KEYCOLORS_COUNT_OFFSET (4)
#define GMMK_KEYCOLORS_START_OFFSET (5)
#define GMMK_KEYCOLORS_DATA_OFFSET (8)
#define GMMK_KEYCOLORS_DATA_SIZE ((GMMK_PACKET_SIZE-GMMK_KEYCOLORS_DATA_OFFSET)/3*3)
#define GMMK_CMD_SUBCOMMAND (0x06)
#define GMMK_SUBCMD_CMD_OFFSET (4)
#define GMMK_SUBCMD_ARG_OFFSET (8)
const char GMMK_SUBCMD_MODE[] = {0x01, 0x00, 0x00, 0x00};
const char GMMK_SUBCMD_BRIGHTNESS[] = {0x01, 0x01, 0x00, 0x00};
const char GMMK_SUBCMD_DELAY[] = {0x01, 0x02, 0x00, 0x00};
const char GMMK_SUBCMD_DIRECTION[] = {0x01, 0x03, 0x00, 0x00};
const char GMMK_SUBCMD_COLORFUL[] = {0x01, 0x04, 0x00, 0x00};
const char GMMK_SUBCMD_ANIMCOLOR[] = {0x03, 0x05, 0x00, 0x00};
const char GMMK_SUBCMD_RATE[] = {0x01, 0x0f, 0x00, 0x00};
GMMKState *gmmk_open() {
GMMKState *s;
enum libusb_error e;
s = malloc(sizeof(GMMKState));
if(s == NULL) {
goto error0;
}
if((e = libusb_init(&(s->usb))) < 0) {
fprintf(stderr, "Couldn't init libusb: %s\n", libusb_strerror(e));
goto error1;
}
s->devices = open_libusb_devices(s->usb, s->usbDevs, GMMK_MAX_DEVS, GMMK_VENDOR_ID,
GMMK_PRODUCT_ID, GMMK_LED_INTERFACE);
if(s->devices == 0) {
goto error2;
}
return(s);
error2:
libusb_exit(s->usb);
error1:
free(s);
error0:
return(NULL);
}
void gmmk_close(GMMKState *s) {
unsigned int i;
enum libusb_error e;
for(i = 0; i < s->devices; i++) {
if((e = libusb_release_interface(s->usbDevs[i], GMMK_LED_INTERFACE)) < 0) {
fprintf(stderr, "WARNING: Couldn't release interface: %s\n", libusb_strerror(e));
}
libusb_close(s->usbDevs[i]);
}
libusb_exit(s->usb);
free(s);
}
void sum(unsigned short int *sum, unsigned char *mem, int size) {
int i;
*sum = 0;
for(i = 0; i < size; i++) {
*sum += (unsigned short int)mem[i];
}
}
#define TRANSFER_OR_FAIL(BUFFER, USBDEV) \
BUFFER[0] = 4; \
sum((unsigned short int *)&(BUFFER[GMMK_SUM_OFFSET]), \
&(BUFFER[GMMK_COMMAND_OFFSET]), \
GMMK_PACKET_SIZE - GMMK_COMMAND_OFFSET); \
if((e = libusb_interrupt_transfer(USBDEV, \
GMMK_OUT_ENDPOINT, \
BUFFER, \
GMMK_PACKET_SIZE, \
&transferred, \
GMMK_TIMEOUT_MS)) < 0) { \
fprintf(stderr, "Interrupt out failed: %s\n", libusb_strerror(e)); \
return(-1); \
} \
if(transferred < GMMK_PACKET_SIZE) { \
fprintf(stderr, "WARNING: Short packet sent.\n"); \
} /* read a packet back */ \
if((e = libusb_interrupt_transfer(USBDEV, \
GMMK_IN_ENDPOINT, \
BUFFER, \
GMMK_PACKET_SIZE, \
&transferred, \
GMMK_TIMEOUT_MS)) < 0) { \
fprintf(stderr, "Interrupt in failed: %s\n", libusb_strerror(e)); \
return(-1); \
} \
if(transferred < GMMK_PACKET_SIZE) { \
fprintf(stderr, "WARNING: Short packet read.\n"); \
}
#define CLEAR_BUFF(BUFFER) \
memset(&(BUFFER[GMMK_COMMAND_OFFSET]), 0,\
GMMK_PACKET_SIZE - GMMK_COMMAND_OFFSET);
#define DO_START(BUFFER, USBDEV) \
CLEAR_BUFF(BUFFER) \
BUFFER[GMMK_COMMAND_OFFSET] = GMMK_CMD_START; \
TRANSFER_OR_FAIL(BUFFER, USBDEV)
#define DO_END(BUFFER, USBDEV) \
CLEAR_BUFF(BUFFER) \
BUFFER[GMMK_COMMAND_OFFSET] = GMMK_CMD_END; \
TRANSFER_OR_FAIL(BUFFER, USBDEV)
#define APPLY_SUBCOMMAND(BUFFER, SCMD, SCARG) \
BUFFER[GMMK_COMMAND_OFFSET] = GMMK_CMD_SUBCOMMAND; \
memcpy(&(BUFFER[GMMK_SUBCMD_CMD_OFFSET]), \
SCMD, \
sizeof(SCMD)); \
BUFFER[GMMK_SUBCMD_ARG_OFFSET] = SCARG;
#define DO_SUBCOMMAND(BUFFER, USBDEV, SCMD, SCARG) \
CLEAR_BUFF(BUFFER) \
APPLY_SUBCOMMAND(BUFFER, SCMD, SCARG) \
TRANSFER_OR_FAIL(BUFFER, USBDEV)
#define APPLY_SUBCOMMAND_RGB(BUFFER, SCMD, SCR, SCG, SCB) \
BUFFER[GMMK_COMMAND_OFFSET] = GMMK_CMD_SUBCOMMAND; \
memcpy(&(BUFFER[GMMK_SUBCMD_CMD_OFFSET]), \
SCMD, \
sizeof(SCMD)); \
BUFFER[GMMK_SUBCMD_ARG_OFFSET] = SCR; \
BUFFER[GMMK_SUBCMD_ARG_OFFSET+1] = SCG; \
BUFFER[GMMK_SUBCMD_ARG_OFFSET+2] = SCB;
#define DO_SUBCOMMAND_RGB(BUFFER, USBDEV, SCMD, SCR, SCG, SCB) \
CLEAR_BUFF(BUFFER) \
APPLY_SUBCOMMAND_RGB(BUFFER, SCMD, SCR, SCG, SCB) \
TRANSFER_OR_FAIL(BUFFER, USBDEV)
int gmmk_setMode(GMMKState *s, int devNum, unsigned char mode) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_MODE, mode)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setBrightness(GMMKState *s, int devNum, unsigned char brightness) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_BRIGHTNESS, brightness)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setDelay(GMMKState *s, int devNum, unsigned char delay) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_DELAY, delay)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setDirLeft(GMMKState *s, int devNum) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_DIRECTION, 0xFF)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setDirRight(GMMKState *s, int devNum) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_DIRECTION, 0)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setColorful(GMMKState *s, int devNum) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_COLORFUL, 1)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_unsetColorful(GMMKState *s, int devNum) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_COLORFUL, 0)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setAnimationColor(GMMKState *s, int devNum,
unsigned char r, unsigned char g, unsigned char b) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND_RGB(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_ANIMCOLOR, r, g, b)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setRate(GMMKState *s, int devNum, unsigned char rate) {
enum libusb_error e;
int transferred;
DO_START(s->buffer, s->usbDevs[devNum])
DO_SUBCOMMAND(s->buffer, s->usbDevs[devNum], GMMK_SUBCMD_RATE, rate)
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}
int gmmk_setKeys(GMMKState *s, int devNum, unsigned int start,
unsigned int count, const GMMKColor *c) {
enum libusb_error e;
int transferred;
unsigned int i, j;
/* fprintf(stderr, "%u %u\n", start, count);
*/
if(start > GMMK_MAX_KEY) {
return(0);
}
if(start + count > GMMK_MAX_KEY) {
count = GMMK_MAX_KEY - start;
}
DO_START(s->buffer, s->usbDevs[devNum])
for(i = 0; i < count; i += GMMK_KEYCOLORS_DATA_SIZE / 3) {
CLEAR_BUFF(s->buffer)
s->buffer[GMMK_COMMAND_OFFSET] = GMMK_CMD_KEYCOLORS;
s->buffer[GMMK_KEYCOLORS_COUNT_OFFSET] = (count - i) * 3 > GMMK_KEYCOLORS_DATA_SIZE ?
GMMK_KEYCOLORS_DATA_SIZE :
(count - i) * 3;
*(unsigned short int *)(&(s->buffer[GMMK_KEYCOLORS_START_OFFSET])) =
(start * 3) + (i * 3);
/* fprintf(stderr, "%u %u\n",
*(unsigned short int *)(&(s->buffer[GMMK_KEYCOLORS_START_OFFSET])),
s->buffer[GMMK_KEYCOLORS_COUNT_OFFSET]);
*/
for(j = 0; j < s->buffer[GMMK_KEYCOLORS_COUNT_OFFSET]; j+=3) {
/* fprintf(stderr, "%u %u %u\n", c[i+(j/3)].r, c[i+(j/3)].g, c[i+(j/3)].b);
*/ s->buffer[GMMK_KEYCOLORS_DATA_OFFSET+j+0] = c[i+(j/3)].r;
s->buffer[GMMK_KEYCOLORS_DATA_OFFSET+j+1] = c[i+(j/3)].g;
s->buffer[GMMK_KEYCOLORS_DATA_OFFSET+j+2] = c[i+(j/3)].b;
}
TRANSFER_OR_FAIL(s->buffer, s->usbDevs[devNum])
}
DO_END(s->buffer, s->usbDevs[devNum])
return(0);
}