-
Notifications
You must be signed in to change notification settings - Fork 3
/
chronos-input.c
311 lines (278 loc) · 8.93 KB
/
chronos-input.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
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/uinput.h>
// Communication messages
char TRANS_INIT[3] = {0xFF, 0x07, 0x03};
char TRANS_REQUEST_ACC[7] = {0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00};
// Definitions
#define MOUSE_MODE 0
#define JOYSTICK_MODE 1
#define die(str, args...) do { \
perror(str); \
exit(EXIT_FAILURE); \
} while(0)
// Default variables
char* accessPointDev = "/dev/uinput";
char* uInputDev = "/dev/ttyACM0";
int delay = 30000;
int devmode = MOUSE_MODE;
double accelX = 1, accelY = 1, accelZ = 1;
int swapXZ = 0;
int deadzone = 3;
// Global variables
int fd_uinput, fd_transmitter;
void parseCmdOptions(int argc, char **argv) {
int option;
while ((option = getopt(argc, argv, "a:u:p:m:s:d:xh")) != -1) {
switch (option) {
case 'a':
accessPointDev = optarg;
break;
case 'u':
uInputDev = optarg;
break;
case 'p':
delay = atoi(optarg);
break;
case 'm':
if (optarg[0] == 'm') {
devmode = MOUSE_MODE;
} else if (optarg[0] == 'j') {
devmode = JOYSTICK_MODE;
} else {
fprintf(stderr, "Error: Unknown mode '%c'\n", optarg[0]);
exit(1);
}
break;
case 's':
if (optarg[0] == 'x') {
accelX = atof(optarg + 1);
printf("Setting acceleration of X axis to %f\n", accelX);
} else if (optarg[0] == 'y') {
accelY = atof(optarg + 1);
printf("Setting acceleration of Y axis to %f\n", accelY);
} else if (optarg[0] == 'z') {
accelZ = atof(optarg + 1);
printf("Setting acceleration of Z axis to %f\n", accelZ);
} else {
accelX = atof(optarg);
accelY = atof(optarg);
accelZ = atof(optarg);
printf("Setting acceleration of all axes to %f\n", accelX);
}
break;
case 'x':
swapXZ = 1;
break;
case 'd':
deadzone = atof(optarg);
break;
case 'h':
case '?':
printf("\neZ430-Chronos mouse and joystick driver\n");
printf("(c) Ignaz Forster; licensed under BSD license\n");
printf("\nUsage:\n");
printf("-m [m|j]\t\tUse watch as mouse or joystick device (default: mouse)\n");
printf("-a <device>\t\tPath to access point (default: %s)\n", accessPointDev);
printf("-u <device>\t\tPath to uInput device (default: %s)\n", uInputDev);
printf("-p <microseconds>\tPolling interval (default: %d)\n", delay);
printf("-s [x|y|z|]<multiplier>\tAccelerate axis / general movement speed (default: 1.0)\n");
printf("-d <number>\t\tDeadzone / ignore movement less than <number> from origin (default: %d)\n", deadzone);
printf("-x\t\t\tJoystick mode: Swap X / Z axis (for racing wheel emulation)\n");
exit(0);
}
}
}
void sighandler(int sig)
{
if (fd_transmitter)
close(fd_transmitter);
if (fd_uinput) {
ioctl(fd_uinput, UI_DEV_DESTROY);
close(fd_uinput);
}
printf("\nGoodbye!\n");
exit(0);
}
void setupSignalHandlers()
{
signal(SIGQUIT, sighandler);
signal(SIGINT, sighandler);
}
void openDevices(char* accessPointDev, char* uInputDev)
{
fd_uinput = open(uInputDev, O_WRONLY | O_NONBLOCK);
if (fd_uinput < 0) {
fd_uinput = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK);
if (fd_uinput < 0)
die("Error: Could not open uinput device. Is the uinput driver loaded (modprobe uinput) and do you have write permissions for the device?");
}
fd_transmitter = open(accessPointDev, O_RDWR);
if (fd_transmitter < 0)
die("Error: Could not open access point device. Is the transmitter connected and do you have read and write permissions for the device?");
}
void setIoctlPropertiesForUinput()
{
if (ioctl(fd_uinput, UI_SET_EVBIT, EV_KEY) < 0)
die("Error: ioctl EV_KEY failed");
if (devmode == MOUSE_MODE) {
// Button events
if (ioctl(fd_uinput, UI_SET_KEYBIT, BTN_LEFT) < 0)
die("Error: ioctl BTN_LEFT failed");
if (ioctl(fd_uinput, UI_SET_KEYBIT, BTN_RIGHT) < 0)
die("Error: ioctl BTN_RIGHT failed");
if (ioctl(fd_uinput, UI_SET_KEYBIT, BTN_MIDDLE) < 0)
die("Error: ioctl BTN_MIDDLE failed");
// Movement events
if (ioctl(fd_uinput, UI_SET_EVBIT, EV_REL) < 0)
die("Error: ioctl EV_REL failed");
if (ioctl(fd_uinput, UI_SET_RELBIT, REL_X) < 0)
die("Error: ioctl REL_X failed");
if (ioctl(fd_uinput, UI_SET_RELBIT, REL_Y) < 0)
die("Error: ioctl REL_Y failed");
} else if (devmode == JOYSTICK_MODE) {
if (ioctl(fd_uinput, UI_SET_KEYBIT, BTN_JOYSTICK) < 0)
die("Error: ioctl BTN_LEFT failed");
if (ioctl(fd_uinput, UI_SET_KEYBIT, BTN_JOYSTICK + 1) < 0)
die("Error: ioctl BTN_LEFT failed");
if (ioctl(fd_uinput, UI_SET_KEYBIT, BTN_JOYSTICK + 2) < 0)
die("Error: ioctl BTN_LEFT failed");
if (ioctl(fd_uinput, UI_SET_EVBIT, EV_ABS) < 0)
die("Error: ioctl EV_REL failed");
if (ioctl(fd_uinput, UI_SET_ABSBIT, ABS_X) < 0)
die("Error: ioctl REL_X failed");
if (ioctl(fd_uinput, UI_SET_ABSBIT, ABS_Y) < 0)
die("Error: ioctl REL_Y failed");
if (ioctl(fd_uinput, UI_SET_ABSBIT, ABS_Z) < 0)
die("Error: ioctl REL_Y failed");
}
}
struct uinput_user_dev createUinputDevice()
{
struct uinput_user_dev uidev;
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "Texas Instruments eZ430-Chronos");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x0451;
uidev.id.product = 0x16a6;
uidev.id.version = 1;
if (write(fd_uinput, &uidev, sizeof(uidev)) < 0)
die("Error: Writing control structure for uinput device failed");
if (ioctl(fd_uinput, UI_DEV_CREATE) < 0)
die("Error: Creation of uinput device failed");
return uidev;
}
void initAccessPoint(char* buffer)
{
if (write(fd_transmitter, TRANS_INIT, sizeof(TRANS_INIT)) < 0)
die("Error: Starting Access Point failed");
// Ignore initialization data
read(fd_transmitter, buffer, 3);
}
void getChronosEvents(char* buffer)
{
if (write(fd_transmitter, TRANS_REQUEST_ACC, sizeof(TRANS_REQUEST_ACC)) < 0)
die("Error: Requesting acceleration data failed");
if (read(fd_transmitter, buffer, 7) < 0) {
die("Error: Reading acceleration data failed");
}
}
void sendInputEvent(__u16 type, __u16 code, __s32 value)
{
struct input_event ev;
memset(&ev, 0, sizeof(struct input_event));
ev.type = type;
ev.code = code;
ev.value = value;
if (write(fd_uinput, &ev, sizeof(struct input_event)) < 0)
die("Error: Setting mouse event failed");
}
int main(int argc, char **argv)
{
struct uinput_user_dev uidev;
char buffer[7];
__u16 button_pressed = 0;
int debounce_counter = 0;
char tmp;
parseCmdOptions(argc, argv);
setupSignalHandlers();
openDevices(uInputDev, accessPointDev);
setIoctlPropertiesForUinput();
uidev = createUinputDevice(devmode);
// According to the manual we should wait up to 1 second after
// initializing the Chronos hardware
sleep(1);
initAccessPoint(buffer);
while (1) {
getChronosEvents(buffer);
//printf("RAW data: %d %d %d %d %d %d %d\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6]);
if (button_pressed > 0) {
sendInputEvent(EV_KEY, button_pressed, 0);
button_pressed = 0;
// Process button events and prevent bouncing
// Unfortunately the watch only sends events when a button is
// pressed, but not when it is released, so drag'n'drop cannot
// be supported. Additionally ignore all movement data as
// sending both a mouse click and a movement without a sync in
// between results in a drag'n'drop action.
// TODO: Find a better use for the keys in PPT mode
} else if (buffer[3] > 1 && debounce_counter == 0) {
if (devmode == MOUSE_MODE) {
if (buffer[3] == 17 || buffer[3] == 18) {
button_pressed = BTN_LEFT;
} else if (buffer[3] == 49 || buffer[3] == 50) {
button_pressed = BTN_RIGHT;
} else if (buffer[3] == 33 || buffer[3] == 34) {
button_pressed = BTN_MIDDLE;
}
} else if (devmode == JOYSTICK_MODE) {
if (buffer[3] == 17 || buffer[3] == 18) {
button_pressed = BTN_JOYSTICK;
} else if (buffer[3] == 49 || buffer[3] == 50) {
button_pressed = BTN_JOYSTICK + 1;
} else if (buffer[3] == 33 || buffer[3] == 34) {
button_pressed = BTN_JOYSTICK + 2;
}
}
sendInputEvent(EV_KEY, button_pressed, 1);
debounce_counter = 1;
// Process acceleration data
} else if (buffer[3] == 1) {
for (tmp = 4; tmp <= 6; tmp++) {
if (buffer[tmp] < deadzone && buffer[tmp] > -deadzone)
buffer[tmp] = 0;
else if (buffer[tmp] > 0)
buffer[tmp] = buffer[tmp] - deadzone;
else
buffer[tmp] = buffer[tmp] + deadzone;
}
if (devmode == MOUSE_MODE) {
sendInputEvent(EV_REL, REL_X, buffer[5] * accelX);
sendInputEvent(EV_REL, REL_Y, buffer[4] * accelY);
} else if (devmode == JOYSTICK_MODE) {
if (swapXZ) {
tmp = buffer[6];
buffer[6] = buffer[5];
buffer[5] = tmp;
}
sendInputEvent(EV_ABS, ABS_X, buffer[5] * 256 * accelX);
sendInputEvent(EV_ABS, ABS_Y, buffer[4] * 256 * accelY);
sendInputEvent(EV_ABS, ABS_Z, buffer[6] * 256 * accelZ);
}
}
sendInputEvent(EV_SYN, 0, 0);
if (debounce_counter > 0) {
if (debounce_counter == 20)
debounce_counter = 0;
else
debounce_counter++;
}
usleep(delay);
}
return 0;
}