-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiiAccessoryToUSB.ino
337 lines (278 loc) · 11.1 KB
/
WiiAccessoryToUSB.ino
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
/* WiiAccessoryToUSB
* Doug Barry 20140924
*
* WiiAccessoryToUSB.ino
* Main routine.
*
* This Arduino project allows you to connect a Wii accessory to an Arduino and
* through use of the UnoJoy project, make it appear as a game controller on PC
* and PS3. It supports the standard Wii Nunchuck including accelerometers,
* and the Hori FightStick released for Tatsunoko Vs Capcom.
*
* I have used this in conjunction with x360ce on a Windows PC to play games
* that support the XInput system to play Ultra Street Fighter 4 and other
* fighting games.
* See * https://code.google.com/p/unojoy/
* * https://code.google.com/p/x360ce/source/checkout
*
*
* What needs adding:
* - Calibration routine.
* - Input from other console controllers? Plenty of spare D pins.
*
* Note: I have only tried this on an Arduino Uno R3, compiled using
* ArduinoIDE 1.5.7+, so YMMV. I have made some attempts to make this compatible
* with older versions of the Ardunio IDE, but I do not know if they will work.
*
* Acknowledgements.
* This code draw inspiration and code from the following:
* - WiiClassic Test Code by Tim Hirzel: http://playground.arduino.cc/Main/WiiClassicController
* - Nunchuck functions by Tod E. Kurt: 2007-11 Tod E. Kurt: http://todbot.com/blog/
* - Wii Extension to USB HID project:
* https://code.google.com/p/wii-ext-to-usb/
* http://slotcover.wordpress.com/2009/03/12/wii-extension-controller-to-usb-project/
*/
/* Define DEBUG if you wish to see serial output describing the data coming in
* decoded from the I2C bus. This makes it easier to see which digital buttons
* map where.
* If this is not defined, then this will run at full speed, and is intended
* to be used when the Arduino's ATMEGA8U2 has been flashed with the UnoJoy
* firmware, to appear as a USB gamepad.
*/
//#define DEBUG
#include "UnoJoy.h"
#include <Wire.h>
#include "WiiAccessoryToUSB.h"
#include "WiiAccessories.h"
/* Define if using a WiiChuck
Sets pin A3 ~VCC
pin A2 VDD
*/
#define PWR_ANALOGPINS
// settings for Wii HORI Fightstick
#define FS_LS_X_WII_MIN 0
#define FS_LS_X_WII_MAX 63
#define FS_LS_X_WII_INVERT false
#define FS_LS_Y_WII_MIN 0
#define FS_LS_Y_WII_MAX 63
#define FS_LS_Y_WII_INVERT true
#define FS_RS_X_WII_MIN 0
#define FS_RS_X_WII_MAX 31
#define FS_RS_X_WII_INVERT false
#define FS_RS_Y_WII_MIN 0
#define FS_RS_Y_WII_MAX 31
#define FS_RS_Y_WII_INVERT true
WiiClassic wiiClassicController;
WiiArcade wiiArcadeController;
WiiNunchuck wiiNunchuckController;
dataForController_t controllerData;
unsigned char controllerType = CNTRL_NONE;
void setup()
{
setupUnoJoy();
#ifdef PWR_ANALOGPINS
setPowerPins();
#endif
controllerType = WiiAccessory::detect();
if (controllerType == CNTRL_CLASSIC)
{
wiiClassicController = WiiClassic();
wiiClassicController.begin();
wiiClassicController.update();
}
else if (controllerType == CNTRL_ARCADE)
{
wiiArcadeController.begin();
wiiArcadeController.update();
}
else
{
wiiNunchuckController.begin();
wiiNunchuckController.update();
}
#ifdef DEBUG
Serial.begin(9600);
delay(5);
Serial.write(27); // ESC
Serial.print("[H"); // Cursor home
Serial.write(27); // ESC
Serial.print("[2J"); // Clear screen
delay(10);
#endif
}
void loop()
{
switch (controllerType)
{
case CNTRL_CLASSIC:
controllerData = getControllerDataClassic();
break;
case CNTRL_ARCADE:
controllerData = getControllerDataArcade();
break;
case CNTRL_NUNCHUCK:
controllerData = getControllerDataNunchuck();
break;
}
#ifndef DEBUG
setControllerData(controllerData);
#endif
#ifdef DEBUG
Serial.print("TYPE: ");
Serial.print(controllerType, HEX);
Serial.println();
for (int i = 4; i < 6; i++)
{
Serial.println(wiiClassicController.data[i], BIN);
}
Serial.println();
Serial.println("XAYB UDLR L12 R12 S S H");
Serial.print(controllerData.triangleOn ? 1 : 0);
Serial.print(controllerData.circleOn ? 1 : 0);
Serial.print(controllerData.squareOn ? 1 : 0);
Serial.print(controllerData.crossOn ? 1 : 0);
Serial.print(" ");
Serial.print(controllerData.dpadUpOn ? 1 : 0);
Serial.print(controllerData.dpadDownOn ? 1 : 0);
Serial.print(controllerData.dpadLeftOn ? 1 : 0);
Serial.print(controllerData.dpadRightOn ? 1 : 0);
Serial.print(" ");
Serial.print(" ");
Serial.print(controllerData.l1On ? 1 : 0);
Serial.print(controllerData.l2On ? 1 : 0);
Serial.print(" ");
Serial.print(" ");
Serial.print(controllerData.r1On ? 1 : 0);
Serial.print(controllerData.r2On ? 1 : 0);
Serial.print(" ");
Serial.print(controllerData.selectOn ? 1 : 0);
Serial.print(" ");
Serial.print(controllerData.startOn ? 1 : 0);
Serial.print(" ");
Serial.print(controllerData.homeOn ? 1 : 0);
Serial.println();
Serial.print("LSX: ");
Serial.print(controllerData.leftStickX);
Serial.print("/");
if ((controllerType == CNTRL_CLASSIC) || (controllerType == CNTRL_ARCADE)) Serial.println(wiiClassicController.leftStickX());
if (controllerType == CNTRL_NUNCHUCK) Serial.println(wiiNunchuckController.joystickX());
Serial.print("LSY: ");
Serial.print(controllerData.leftStickY);
Serial.print("/");
if ((controllerType == CNTRL_CLASSIC) || (controllerType == CNTRL_ARCADE)) Serial.println(wiiClassicController.leftStickY());
if (controllerType == CNTRL_NUNCHUCK) Serial.println(wiiNunchuckController.joystickY());
Serial.print("RSX: ");
Serial.print(controllerData.rightStickX);
Serial.print("/");
if ((controllerType == CNTRL_CLASSIC) || (controllerType == CNTRL_ARCADE)) Serial.println(wiiClassicController.rightStickX());
if (controllerType == CNTRL_NUNCHUCK) Serial.println(wiiNunchuckController.accelX());
Serial.print("RSY: ");
Serial.print(controllerData.rightStickY);
Serial.print("/");
if ((controllerType == CNTRL_CLASSIC) || (controllerType == CNTRL_ARCADE)) Serial.println(wiiClassicController.rightStickY());
if (controllerType == CNTRL_NUNCHUCK) Serial.println(wiiNunchuckController.accelY());
Serial.write(27); // ESC
Serial.print("[H"); // Cursor home
#endif
LoopWait();
}
void inline LoopWait()
{
#ifdef DEBUG
delay(100);
#else
delay(10); // ~100Hz
#endif
}
dataForController_t getControllerDataArcade(void)
{
dataForController_t controllerData = getBlankDataForController();
wiiArcadeController.update();
// Arcade stick mappings
// PS3 XBOX360 Wii
controllerData.triangleOn = !wiiArcadeController.xIsPressed();// Y X
controllerData.circleOn = !wiiArcadeController.aIsPressed();// B A
controllerData.squareOn = !wiiArcadeController.yIsPressed();// X Y
controllerData.crossOn = !wiiArcadeController.bIsPressed();// A B
controllerData.dpadUpOn = !wiiArcadeController.upDIsPressed();
controllerData.dpadDownOn = !wiiArcadeController.downDIsPressed();
controllerData.dpadLeftOn = !wiiArcadeController.leftDIsPressed();
controllerData.dpadRightOn = !wiiArcadeController.rightDIsPressed();
controllerData.l1On = !wiiArcadeController.lzIsPressed();// LB zL
controllerData.l2On = !wiiArcadeController.rIsPressed();// RT R
controllerData.r1On = !wiiArcadeController.lIsPressed();// RB L
controllerData.r2On = !wiiArcadeController.rzIsPressed();// LT zR
controllerData.selectOn = !wiiArcadeController.selectIsPressed();
controllerData.startOn = !wiiArcadeController.startIsPressed();
controllerData.homeOn = !wiiArcadeController.homeIsPressed();
// this could do with some improvement, specifically, the MIN value is not taken into account currently.
#if defined FS_LS_X_WII_INVERT
#if FS_LS_X_WII_INVERT == true
controllerData.leftStickX = 255 - (wiiArcadeController.leftStickX() * (256 / (FS_LS_X_WII_MAX + 1)));
#else
controllerData.leftStickX = wiiArcadeController.leftStickX() * (256 / (FS_LS_X_WII_MAX + 1));
#endif
#endif
#if defined FS_LS_Y_WII_INVERT
#if FS_LS_Y_WII_INVERT == true
controllerData.leftStickY = 255 - (wiiArcadeController.leftStickY() * (256 / (FS_LS_Y_WII_MAX + 1)));
#else
controllerData.leftStickY = wiiArcadeController.leftStickY() * (256 / (FS_LS_Y_WII_MAX + 1));
#endif
#endif
#if defined FS_RS_X_WII_INVERT
#if FS_RS_X_WII_INVERT == true
controllerData.rightStickX = 255 - (wiiArcadeController.rightStickX() * (256 / (FS_RS_X_WII_MAX + 1)));
#else
controllerData.rightStickX = wiiArcadeController.rightStickX() * (256 / (FS_RS_X_WII_MAX + 1));
#endif
#endif
#if defined FS_RS_Y_WII_INVERT
#if FS_RS_Y_WII_INVERT == true
controllerData.rightStickY = 255 - (wiiArcadeController.rightStickY() * (256 / (FS_RS_Y_WII_MAX + 1)));
#else
controllerData.rightStickY = wiiArcadeController.rightStickY() * (256 / (FS_RS_Y_WII_MAX + 1));
#endif
#endif
return controllerData;
}
dataForController_t getControllerDataClassic(void)
{
// I do not have a classic controller to use in order to make these mappings correct!
dataForController_t controllerData = getBlankDataForController();
wiiClassicController.update();
// Arcade stick mappings
// PS3 XBOX360 Wii
controllerData.triangleOn = !wiiClassicController.xIsPressed();// Y X
controllerData.circleOn = !wiiClassicController.aIsPressed();// B A
controllerData.squareOn = !wiiClassicController.yIsPressed();// X Y
controllerData.crossOn = !wiiClassicController.bIsPressed();// A B
controllerData.dpadUpOn = !wiiClassicController.upDIsPressed();
controllerData.dpadDownOn = !wiiClassicController.downDIsPressed();
controllerData.dpadLeftOn = !wiiClassicController.leftDIsPressed();
controllerData.dpadRightOn = !wiiClassicController.rightDIsPressed();
controllerData.l1On = !wiiClassicController.lzIsPressed();// LB zL
controllerData.l2On = !wiiClassicController.rIsPressed();// RT R
controllerData.r1On = !wiiClassicController.lIsPressed();// RB L
controllerData.r2On = !wiiClassicController.rzIsPressed();// LT zR
controllerData.selectOn = !wiiClassicController.selectIsPressed();
controllerData.startOn = !wiiClassicController.startIsPressed();
controllerData.homeOn = !wiiClassicController.homeIsPressed();
controllerData.leftStickX = wiiClassicController.leftStickX();
controllerData.leftStickY = wiiClassicController.leftStickY();
controllerData.rightStickX = wiiClassicController.rightStickX();
controllerData.rightStickY = wiiClassicController.rightStickY();
return controllerData;
}
dataForController_t getControllerDataNunchuck(void)
{
dataForController_t controllerData = getBlankDataForController();
wiiNunchuckController.update();
controllerData.leftStickX = wiiNunchuckController.joystickX();
controllerData.leftStickY = wiiNunchuckController.joystickY();
controllerData.rightStickX = wiiNunchuckController.accelX();
controllerData.rightStickY = wiiNunchuckController.accelY();
controllerData.crossOn = wiiNunchuckController.zIsPressed();
controllerData.circleOn = wiiNunchuckController.cIsPressed();
return controllerData;
}