-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi2c_mouse.c
576 lines (492 loc) · 14.3 KB
/
i2c_mouse.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* wusbmote: Wiimote accessory to USB Adapter
* Copyright (C) 2012-2014 Raphaël Assénat
*
* Based on:
* VBoy2USB: Virtual Boy to USB adapter
* Copyright (C) 2009 Raphaël Assénat
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* The author may be contacted at raph@raphnet.net
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "gamepad.h"
#include "i2c_gamepad.h"
#include "i2c.h"
#include "usbdrv.h"
#include "usbconfig.h"
#include "eeprom.h"
/* The wiibrew documentation talks about writing to 0x(4)a400xx, reading from 0x(4)a500xx.
*
* In binary:
* 0xa4.. 1010 0100
* 0xa5.. 1010 0101
*
* It is clear that A4 and A5 is the first 8 bits of the I2C transaction. The least
* significant bit is the I2C R/W bit.
*
* This translates to a 0x52 7bit address (0xa4>>1)
*
* Now the Wii motion plus documentation mentions 0x(4)A6000
* (http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Wii_Motion_Plus)
*
* So we have in binary:
* 0xa6.. 1010 0110
*
* The wii motion plus I2C 7 bit address is therfore 0x53.
*
* The document then explains the wii motion plus can be made to answer at 0xa4
* by writing 0x04 to register 0xFE.
*/
#define I2C_STANDARD_ADDRESS 0x52
#define I2C_W2I_MPLUS_ADDRESS 0x53
#define REPORT_SIZE 4
/*
* [0] Mouse buttons
* [1] Mouse X
* [2] Mouse Y
* [3] Mouse Wheel
*/
static const char mouse_report_descriptor[] PROGMEM = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x01, // USAGE (Pointer)
0xa1, 0x00, // COLLECTION (Physical)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x04, // USAGE_MAXIMUM (Button 4)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x04, // REPORT_COUNT (4)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x04, // REPORT_SIZE (4)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x02, // REPORT_COUNT (2)
0x81, 0x06, // INPUT (Data,Var,Rel)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
0x35, 0x00, // PHYSICAL MIN 0
0x45, 0x00, // PHYSICAL MAX 0
0x09, 0x38, // USAGE (Wheel)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x06, // INPUT (Data,Var,Rel)
0xc0, // END_COLLECTION (Physical)
#if 0
//// Vendor defined feature report
0xA1, 0x02, // COLLECTION (Logical)
0x06, 0x00, 0xFF, // USAGE_PAGE(Vendor defined page 1)
0x09, 0x01, // USAGE (Vendor defined usage)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x05, // REPORT_COUNT (5)
0xB1, 0x00, // FEATURE (Data,Ary,Abs)
0xC0, // END COLLECTION (Logical)
#endif
0xc0, // END_COLLECTION (Application)
};
// report matching the most recent bytes from the controller
static unsigned char last_read_controller_bytes[REPORT_SIZE];
// the most recently reported bytes
static unsigned char last_reported_controller_bytes[REPORT_SIZE];
static char g_active = 0;
#define DEBUGLOW() PORTC &= 0xFE
#define DEBUGHIGH() PORTC |= 0x01
#define W2I_REG_REPORT 0x00
#define W2I_REG_UNKNOWN_F0 0xF0
#define W2I_REG_UNKNOWN_FB 0xFB
#define W2I_REG_ID_L 0xFE
#define W2I_REG_ID_H 0xFF
#define STATE_INIT 0
#define STATE_READ_DATA 1
static char state = STATE_INIT;
// Based on reading 0xFE and 0xFF. This might be wrong...
#define ID_NUNCHUK 0x0000
#define ID_CLASSIC 0x0101
static unsigned short peripheral_id = ID_NUNCHUK;
static char w2i_reg_writeByte(unsigned char i2c_addr, unsigned char reg_addr, unsigned char value)
{
unsigned char tmpbuf[2];
char res;
tmpbuf[0] = reg_addr;
tmpbuf[1] = value;
res = i2c_transaction(i2c_addr, 2, tmpbuf, 0, NULL, 0);
_delay_us(400);
return res;
}
static char w2i_reg_readBlock(unsigned char reg_addr, unsigned char *dst, int len)
{
char res;
res = i2c_transaction(I2C_STANDARD_ADDRESS, 1, ®_addr, 0, NULL, 0);
if (res)
return res;
_delay_us(400);
res = i2c_transaction(I2C_STANDARD_ADDRESS, 0, NULL, len, dst, 0);
if (res)
return res;
_delay_us(400);
return 0;
}
#define MOUSE_DEADZONE g_eeprom_data.cfg.mouse_deadzone
#define SCR_NCK_THRES g_eeprom_data.cfg.scroll_nunchuck_threshold
#define SCR_NCK_C_THRES g_eeprom_data.cfg.scroll_nunchuck_c_threshold
#define SCR_RJOY_THRES 8
static void setLastValues(unsigned char x, unsigned char y, unsigned short rx, unsigned short ry, unsigned short rz, unsigned char btns, unsigned char orig_x, unsigned char orig_y)
{
int X,Y,XO,YO;;
int W = 0;
static int w_active;
static unsigned char wvalue = 0;
X = x - 0x80;
Y = y - 0x80;
XO = orig_x - 0x80;
YO = orig_y - 0x80;
X -= XO;
Y -= YO;
if (X > MOUSE_DEADZONE) {
X = X - MOUSE_DEADZONE;
} else if (X < -MOUSE_DEADZONE) {
X = X + MOUSE_DEADZONE;
} else {
X = 0;
}
if (Y > MOUSE_DEADZONE) {
Y = Y - MOUSE_DEADZONE;
} else if (Y < -MOUSE_DEADZONE) {
Y = Y + MOUSE_DEADZONE;
} else {
Y = 0;
}
if (peripheral_id == ID_NUNCHUK)
{
if (g_eeprom_data.cfg.scroll_nunchuck_c)
{ // scroll by pressing C while moving
static char last_c = 0, scrolling = 0;
char c = btns & 0x02;
// button down event
if (c && !last_c) {
if (Y != 0)
scrolling = 1;
}
// button release event
if (!c && last_c) {
scrolling = 0;
}
last_c = c;
if (scrolling) {
W = Y;
Y = 0;
X = 0;
if (!g_eeprom_data.cfg.scroll_nunchuck_invert) {
W = -W;
}
btns &= ~0x02;
if (W > SCR_NCK_C_THRES) {
W = 1;
} else if (W < -SCR_NCK_C_THRES) {
W = -1;
} else {
W = 0;
}
wvalue = W;
} else {
wvalue = 0;
}
}
else
{ // scroll by rolling
W = rx - 0x200;
if (g_eeprom_data.cfg.scroll_nunchuck_invert) {
W = -W;
}
if (W > SCR_NCK_THRES) {
if (!w_active) {
W = g_eeprom_data.cfg.scroll_nunchuck_step;
w_active = 1;
} else {
W = 0;
}
} else if (W < -SCR_NCK_THRES) {
if (!w_active) {
W = -g_eeprom_data.cfg.scroll_nunchuck_step;
w_active = 1;
} else {
W = 0;
}
} else {
W = 0;
w_active = 0;
}
wvalue = W;
}
}
if (peripheral_id == ID_CLASSIC)
{
W = ry - 0x10;
if (g_eeprom_data.cfg.scroll_joystick_invert) {
W = -W;
}
w_active = 0;
if (W > SCR_RJOY_THRES) {
if (!w_active) {
W = 1;
w_active = 1;
} else {
W = 0;
}
} else if (W < -SCR_RJOY_THRES) {
if (!w_active) {
W = -1;
w_active = 1;
} else {
W = 0;
}
} else {
W = 0;
w_active = 0;
}
wvalue = W;
}
if (X || Y || W || (btns & 0xf0)) {
g_active = 1;
} else {
g_active = 0;
}
X /= g_eeprom_data.cfg.mouse_divisor;
Y /= g_eeprom_data.cfg.mouse_divisor;
if (btns & 0x10) Y = -1;
if (btns & 0x20) Y = 1;
if (btns & 0x40) X = 1;
if (btns & 0x80) X = -1;
if (X < -127)
X = -127;
if (X > 127)
X = 127;
if (Y < -127)
Y = -127;
if (Y > 127)
Y = 127;
last_read_controller_bytes[0] = btns;
last_read_controller_bytes[1] = X & 0xff;
last_read_controller_bytes[2] = Y & 0xff;
last_read_controller_bytes[3] = wvalue;
}
static void i2cMouse_Update(void)
{
unsigned char buf[16];
char res;
unsigned char x=0x80,y=0x80;
static unsigned char orig_x=0x80, orig_y=0x80;
unsigned char btns=0;
unsigned short rx=0x200,ry=0x10,rz=0x200;
static char device_changed = 0;
switch (state)
{
case STATE_INIT:
// For now, we consider everything answering at this address to be the motion plus.
// This switches the mplus to the standard address.
res = w2i_reg_writeByte(I2C_W2I_MPLUS_ADDRESS, 0xFE, 0x04);
// ignore failure
//
// Init sequence from:
//
// http://wiibrew.org/wiki/Wiimote/Extension_Controllers
//
res = w2i_reg_writeByte(I2C_STANDARD_ADDRESS, W2I_REG_UNKNOWN_F0, 0x55);
if (res)
return;
res = w2i_reg_writeByte(I2C_STANDARD_ADDRESS, W2I_REG_UNKNOWN_FB, 0x00);
if (res)
return;
res = w2i_reg_readBlock(W2I_REG_ID_L, buf, 2);
if (res)
return;
peripheral_id = buf[1] | buf[0]<<8;
state = STATE_READ_DATA;
device_changed = 1;
_delay_ms(1000);
break;
// fallthrough
case STATE_READ_DATA:
res = w2i_reg_readBlock(W2I_REG_REPORT, buf, 6);
if (res) {
state = STATE_INIT;
return;
}
switch (peripheral_id)
{
default:
case ID_NUNCHUK:
// Source: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck
//
// 7 6 5 4 3 2 1 0
// 0 SX<7:0>
// 1 SY<7:0>
// 2 AX<9:2>
// 3 AY<9:2>
// 4 AZ<9:2>
// 5 AZ<1:0> AY<1:0> AX<1:0> BC BZ
//
x = buf[0];
y = buf[1] ^ 0xff;
rx = ((buf[5] & 0x0C) >> 2) | (buf[2] << 2);
ry = 0x10; // ((buf[5] & 0x30) >> 4) | (buf[3] << 2);
rz = ((buf[5] & 0xC0) >> 6) | (buf[4] << 2);
if (!(buf[5]&0x01)) btns |= 0x01;
if (!(buf[5]&0x02)) btns |= 0x02;
if (device_changed) {
device_changed = 0;
orig_x = x;
orig_y = y;
}
break;
case ID_CLASSIC:
// Source: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller
//
// 7 6 5 4 3 2 1 0
// 0 RX<4:3> LX<5:0>
// 1 RX<2:1> LY<5:0>
// 2 RX<0> LT<4:3> RY<4:0>
// 3 LT<2:0> RT<4:0>
// 4 BDR BDD BLT B- BH B+ BRT 1
// 5 BZL BB BY BA BX BZR BDL BDU
//
x = buf[0] << 2;
y = (buf[1] << 2) ^ 0xFF;
rx = ((buf[2]>>7) | ((buf[1]&0xC0)>>5) | ((buf[0]&0xC0)>>3)) << 5;
ry = buf[2]&0x1f;
rz = ((buf[3]>>5) | ((buf[2] & 0x60) >> 2) ) << 5;
rz ^= 0xffff;
if (!(buf[4] & 0x04)) btns |= 0x04; // +/Start
if (!(buf[5] & 0x20)) btns |= 0x02; // Y
if (!(buf[5] & 0x08)) btns |= 0x02; // X
if (!(buf[5] & 0x40)) btns |= 0x01; // B
if (!(buf[5] & 0x10)) btns |= 0x01; // A
/*
if (!(buf[4] & 0x20)) btns_l |= 0x20; // L trig
if (!(buf[4] & 0x02)) btns_l |= 0x40; // R trig
if (!(buf[5] & 0x04)) btns_l |= 0x80; // Zr
*/
if (!(buf[5] & 0x01)) btns |= 0x10; // UP
if (!(buf[4] & 0x40)) btns |= 0x20; // DOWN
if (!(buf[4] & 0x80)) btns |= 0x40; // RIGHT
if (!(buf[5] & 0x02)) btns |= 0x80; // LEFT
/*
if (!(buf[5] & 0x80)) btns_h |= 0x10; // Zl
if (!(buf[4] & 0x10)) btns_h |= 0x20; // SELECT
if (!(buf[4] & 0x08)) btns_h |= 0x40; // HOME
*/
if (device_changed) {
device_changed = 0;
orig_x = x;
orig_y = y;
}
break;
} // switch peripheral_id
break; // STATE
}
setLastValues(x,y,rx,ry,rz,btns,orig_x,orig_y);
}
static void i2cMouse_Init(void)
{
//
// TWPS = 0
// CPU FREQ = 12000000
// TARGET SCL FREQ = 400000
//
// CPU FREQ
// --------------------
// SCL freq = 16 + 2*TWBR * 4^TWPS
//
// TWBR = (((12000000 / 400000) - 16) / 1) / 2 = 7
//
//i2c_init(I2C_FLAG_EXTERNAL_PULLUP, 15);
// In fact, 100khz is stable.
i2c_init(I2C_FLAG_EXTERNAL_PULLUP, 52);
i2cMouse_Update();
DDRC |= 0x01;
DEBUGLOW();
}
static char i2cMouse_Changed(void)
{
static char was_moving = 1;
if (g_active)
return 1;
if (last_read_controller_bytes[0] != last_reported_controller_bytes[0])
return 1;
if (last_read_controller_bytes[3] != last_reported_controller_bytes[3])
return 1;
if (was_moving) {
was_moving = 0;
return 1;
}
return 0;
}
static void i2cMouse_BuildReport(unsigned char *reportBuffer)
{
if (reportBuffer != NULL)
{
memcpy(reportBuffer, last_read_controller_bytes, REPORT_SIZE);
}
memcpy(last_reported_controller_bytes,
last_read_controller_bytes,
REPORT_SIZE);
}
#define USBDESCR_DEVICE 1
static const char mouse_device_descriptor[] PROGMEM = { /* USB device descriptor */
18, /* sizeof(usbDescrDevice): length of descriptor in bytes */
USBDESCR_DEVICE, /* descriptor type */
0x01, 0x01, /* USB version supported */
USB_CFG_DEVICE_CLASS,
USB_CFG_DEVICE_SUBCLASS,
0, /* protocol */
8, /* max packet size */
USB_CFG_VENDOR_ID, /* 2 bytes */
USB_CFG_DEVICE_ID_MOUSE, /* 2 bytes */
USB_CFG_DEVICE_VERSION, /* 2 bytes */
1, /* Manufacturer string */
2, /* Product string */
3, /* Serial number string */
1, /* number of configurations */
};
Gamepad i2cMouse_Gamepad = {
report_size: REPORT_SIZE,
reportDescriptor: mouse_report_descriptor,
reportDescriptorSize: sizeof(mouse_report_descriptor),
deviceDescriptor: mouse_device_descriptor,
deviceDescriptorSize: sizeof(mouse_device_descriptor),
init: i2cMouse_Init,
update: i2cMouse_Update,
changed: i2cMouse_Changed,
buildReport: i2cMouse_BuildReport
};
Gamepad *i2cMouse_GetGamepad(void)
{
return &i2cMouse_Gamepad;
}