-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiiAccessories.h
373 lines (327 loc) · 8.05 KB
/
WiiAccessories.h
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
/* WiiAccessoryToUSB
* Doug Barry 20140924
*
* WiiAccessory.h
* Accessories library.
*
* The classes in this library ease the receipt and decoding of
* Wii accessory data over the I2C bus. The base class
* WiiAccessory is designed to be extended, with methods to
* return button, axes and accelerometer information.
*/
#ifndef WIIACCESSORY_H
#define WIIACCESSORY_H
#include <Wire.h>
#define CNTRL_NONE 0
#define CNTRL_NUNCHUCK 0xFE
#define CNTRL_CLASSIC 0xFD
#define CNTRL_ARCADE 0x5F
/* Base class, contains code for communication with accessory
* and decoding of information.
*/
class WiiAccessory
{
public:
byte cnt;
uint8_t data[6];
// Initialise communication with accessory
void begin()
{
Wire.begin();
cnt = 0;
Wire.beginTransmission (0x52);
#if (ARDUINO >= 100)
Wire.write (0x40);
Wire.write (0x00);
#else
Wire.send (0x40);
Wire.send (0x00);
#endif
Wire.endTransmission ();
}
// Get current accessory status registers and buffer
bool update() {
cnt = 0;
Wire.requestFrom (0x52, 6);// request data from nunchuck
while (Wire.available ()) {
#if (ARDUINO >= 100)
data[cnt] = decodeByte(Wire.read());
#else
data[cnt] = decodeByte(Wire.receive());
#endif
cnt++;
}
requestNext();
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// Static method which returns the type of accessory connected.
static int detect()
{
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
Wire.write((uint8_t)0x40);// sends memory address
Wire.write((uint8_t)0x00);// sends sent a zero.
#else
Wire.send((uint8_t)0x40);// sends memory address
Wire.send((uint8_t)0x00);// sends sent a zero.
#endif
Wire.endTransmission();// stop transmitting
delay(5);
Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
Wire.write((uint8_t)0xFE);
#else
Wire.send((uint8_t)0xFE);
#endif
Wire.endTransmission();// stop transmitting
delay(10);
unsigned char typeBuffer[7];
Wire.requestFrom(0x52, 2);
int i = 0;
while (Wire.available())
{
#if (ARDUINO >= 100)
typeBuffer[i] = Wire.read();
#else
typeBuffer[i] = Wire.receive();
#endif
i++;
if (i > 7)
{
// bad things
break;
}
}
if ((typeBuffer[0] == 0xFE) && (typeBuffer[1] == 0xFE))
{
return CNTRL_NUNCHUCK;
}
else if ((typeBuffer[0] == 0xFD) && (typeBuffer[1] == 0xFD))
{
return CNTRL_CLASSIC;
}
else// if ((typeBuffer[0] == 0x5F) && (typeBuffer[1] == 0x5F))
{
return CNTRL_ARCADE;
}
}
private:
// Method to decode bytes received.
byte decodeByte(byte x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
// Request the next chunk of the registers.
void requestNext()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
#if (ARDUINO >= 100)
Wire.write(0x00); // sends one byte
#else
Wire.send(0x00); // sends one byte
#endif
Wire.endTransmission (); // stop transmitting
}
};
/* Subclass which analyses the registers buffer as if it were a Wii Classic
* controller.
* This function is likely quite wrong, as I do not have a classic
* controller against which to correct it!
*/
class WiiClassic : public WiiAccessory {
public:
bool lIsPressed() {
return data[5] & 0b10000000;
}
bool rIsPressed() {
return data[4] & 0b00000010;
}
bool leftShoulderIsPressed() {
return isPressedRowBit(0, 5);
}
bool rightShoulderIsPressed() {
return isPressedRowBit(0, 1);
}
bool lzIsPressed() {
return data[4] & 0b00100000;
}
bool rzIsPressed() {
return data[5] & 0b00000100;
}
bool leftDIsPressed() {
return data[5] & 0b00000010;
}
bool rightDIsPressed() {
return data[4] & 0b10000000;
}
bool upDIsPressed() {
return data[5] & 0b00000001;
}
bool downDIsPressed() {
return data[4] & 0b01000000;
}
bool selectIsPressed() {
return data[4] & 0b00010000;
}
bool homeIsPressed() {
return data[4] & 0b00001000;
}
bool startIsPressed() {
return data[4] & 0b00000100;
}
bool xIsPressed() {
return data[5] & 0b00100000;
}
bool yIsPressed() {
return data[5] & 0b00001000;
}
bool aIsPressed() {
return data[5] & 0b01000000;
}
bool bIsPressed() {
return data[5] & 0b00010000;
}
int rightShouldPressure() {
return data[3] & 0x1f;
}
int leftShouldPressure() {
return ((data[2] & 0x60) >> 2) + ((data[3] & 0xe0) >> 5);
}
int leftStickX() {
return ( (data[0] & 0x3f));
}
int leftStickY() {
return ((data[1] & 0x3f));
}
int rightStickX() {
return ((data[0] & 0xc0) >> 3) + ((data[1] & 0xc0) >> 5) + ((data[2] & 0x80) >> 7);
}
int rightStickY() {
return data[2] & 0x1f;
}
protected:
bool isPressedRowBit(byte row, byte bit)
{
byte mask = (1 << bit);
return (!(data[row + 5] & mask));
}
};
class WiiArcade : public WiiAccessory {
public:
bool lIsPressed() {
return data[5] & 0b10000000;
}
bool rIsPressed() {
return data[4] & 0b00000010;
}
bool leftShoulderIsPressed() {
return isPressedRowBit(0, 5);
}
bool rightShoulderIsPressed() {
return isPressedRowBit(0, 1);
}
bool lzIsPressed() {
return data[4] & 0b00100000;
}
bool rzIsPressed() {
return data[5] & 0b00000100;
}
bool leftDIsPressed() {
return data[5] & 0b00000010;
}
bool rightDIsPressed() {
return data[4] & 0b10000000;
}
bool upDIsPressed() {
return data[5] & 0b00000001;
}
bool downDIsPressed() {
return data[4] & 0b01000000;
}
bool selectIsPressed() {
return data[4] & 0b00010000;
}
bool homeIsPressed() {
return data[4] & 0b00001000;
}
bool startIsPressed() {
return data[4] & 0b00000100;
}
bool xIsPressed() {
return data[5] & 0b00100000;
}
bool yIsPressed() {
return data[5] & 0b00001000;
}
bool aIsPressed() {
return data[5] & 0b01000000;
}
bool bIsPressed() {
return data[5] & 0b00010000;
}
int rightShouldPressure() {
return data[3] & 0x1f;
}
int leftShouldPressure() {
return ((data[2] & 0x60) >> 2) + ((data[3] & 0xe0) >> 5);
}
int leftStickX() {
return ( (data[0] & 0x3f));
}
int leftStickY() {
return ((data[1] & 0x3f));
}
int rightStickX() {
return ((data[0] & 0xc0) >> 3) + ((data[1] & 0xc0) >> 5) + ((data[2] & 0x80) >> 7);
}
int rightStickY() {
return data[2] & 0x1f;
}
protected:
bool isPressedRowBit(byte row, byte bit)
{
byte mask = (1 << bit);
return (!(data[row + 5] & mask));
}
};
/* Subclass which analyses the registers buffer as if it were a Wii Nunchuck
* controller.
*/
class WiiNunchuck : public WiiAccessory
{
public:
// returns zbutton state: 1=pressed, 0=notpressed
bool zIsPressed() {
return ((data[5] >> 0) & 1) ? 0 : 1;
}
// returns zbutton state: 1=pressed, 0=notpressed
bool cIsPressed() {
return ((data[5] >> 1) & 1) ? 0 : 1;
}
// returns value of x-axis joystick
int joystickX() {
return data[0];
}
// returns value of y-axis joystick
int joystickY() {
return data[1];
}
// returns value of x-axis accelerometer
int accelX() {
return ((data[2] << 2) + ((data[5] & (3 << 2 ) >> 2)) >> 2);
}
// returns value of y-axis accelerometer
int accelY() {
return ((data[3] << 2) + ((data[5] & (3 << 4 ) >> 4)) >> 2);
}
// returns value of z-axis accelerometer
int accelZ() {
return ((data[4] << 2) + ((data[5] & (3 << 6 ) >> 6)) >> 2);
}
};
#endif