-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-wiitar-adapter.ino
144 lines (121 loc) · 4.12 KB
/
arduino-wiitar-adapter.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
// Try to identify broken state packets
#define VERIFY 0
// Android-compatible build, but more unused buttons
#define ANDROID 0
#include <Wire.h>
#include "Joystick.h"
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID, // hidReportId
JOYSTICK_TYPE_GAMEPAD, // joystickType
#if ANDROID // buttonCount
14,
#else
9,
#endif
0, // hatSwitchCount
true, // includeXAxis
true, // includeYAxis
true, // includeZAxis
false, // includeRxAxis
false, // includeRyAxis
false, // includeRzAxis
false, // includeRudder
false, // includeThrottle
false, // includeAccelerator
false, // includeBrake
false // includeSteering
);
#define ADDRESS 0x52
void send_byte(byte data, byte location) {
Wire.beginTransmission(ADDRESS);
Wire.write(location);
Wire.write(data);
Wire.endTransmission();
delay(1);
}
void read_state(byte data[6]) {
send_byte(0, 0);
// Read state from the guitar
byte count = 0;
Wire.requestFrom(ADDRESS, 6);
while (Wire.available())
data[count++] = Wire.read();
}
#if VERIFY
byte GH3;
byte ZERO;
#endif
void setup() {
// Initizlize I2C
//
// Wiitars should work at speeds up to 400000 bits/sec, but mine does not
// But we only read 6 bytes at a time, so I don't think that it really maters
Wire.begin();
Wire.setClock(350000);
// Initialize joystick
Joystick.begin(false);
Joystick.setXAxisRange(0, 63);
Joystick.setYAxisRange(0, 63);
Joystick.setZAxisRange(0, 31);
// Initialize unencrypted connection
send_byte(0x55, 0xF0);
send_byte(0x00, 0xFB);
#if VERIFY
// Identify what bits should be at GH3 and "0" positions
// http://www.wiibrew.org/wiki/Wiimote/Extension_Controllers/Guitar_Hero_(Wii)_Guitars
byte temp1 = 0;
byte temp2 = 0;
byte data[6];
for (byte _ = 0; _ < 20; _++) {
read_state(data);
temp1 += ((data[0] >> 7) & 1) + ((data[0] >> 6) & 1);
temp1 += ((data[1] >> 7) & 1) + ((data[1] >> 6) & 1);
temp2 += ((data[2] >> 7) & 1) + ((data[2] >> 6) & 1) + ((data[2] >> 5) & 1);
temp2 += ((data[3] >> 7) & 1) + ((data[3] >> 6) & 1) + ((data[3] >> 5) & 1);
}
GH3 = temp1 > 40 ? 0b11000000 : 0b0000000;
ZERO = temp2 > 60 ? 0b11100000 : 0b0000000;
#endif
}
byte data[6];
void loop() {
// Request state from the guitar
read_state(data);
#if VERIFY
if (
(data[0] & 0b11000000) != GH3 ||
(data[1] & 0b11000000) != GH3 ||
(data[2] & 0b11100000) != ZERO ||
(data[3] & 0b11100000) != ZERO ||
(data[4] & 0b10101011) != 0b10101011 ||
(data[5] & 0b00000110) != 0b00000110
) {
return;
}
#endif
#if ANDROID
Joystick.setButton(0, !(data[5] & 0b00010000)); // Fret green
Joystick.setButton(1, !(data[5] & 0b01000000)); // Fret red
Joystick.setButton(3, !(data[5] & 0b00001000)); // Fret yellow
Joystick.setButton(4, !(data[5] & 0b00100000)); // Fret blue
Joystick.setButton(6, !(data[5] & 0b10000000)); // Fret orange
Joystick.setButton(7, !(data[4] & 0b01000000)); // Strum down
Joystick.setButton(11, !(data[5] & 0b00000001)); // Strum up
Joystick.setButton(12, !(data[4] & 0b00010000)); // Button minus
Joystick.setButton(13, !(data[4] & 0b00000100)); // Button plus
#else
Joystick.setButton(0, !(data[5] & 0b00010000)); // Fret green
Joystick.setButton(1, !(data[5] & 0b01000000)); // Fret red
Joystick.setButton(2, !(data[5] & 0b00001000)); // Fret yellow
Joystick.setButton(3, !(data[5] & 0b00100000)); // Fret blue
Joystick.setButton(4, !(data[5] & 0b10000000)); // Fret orange
Joystick.setButton(5, !(data[4] & 0b01000000)); // Strum down
Joystick.setButton(6, !(data[5] & 0b00000001)); // Strum up
Joystick.setButton(7, !(data[4] & 0b00010000)); // Button minus
Joystick.setButton(8, !(data[4] & 0b00000100)); // Button plus
#endif
Joystick.setXAxis(data[0] & 0b00111111); // Stick X
Joystick.setYAxis(data[1] & 0b00111111); // Stick Y
Joystick.setZAxis(data[3] & 0b00011111); // Whammy
Joystick.sendState();
}