-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamecubeHub.ino
321 lines (262 loc) · 8.17 KB
/
GamecubeHub.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
#include "Joystick.h"
#define GC_PIN 2
#define GC_OUTPUT DDRD |= 0x02;
#define GC_INPUT DDRD &= ~0x02;
#define GC_HIGH PORTD |= 0x02;
#define GC_LOW PORTD &= ~0x02;
#define GC_QUERY (PIND & 0x02)
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
12, 0,
true, true, false,
true, true, false,
false, true,
false, true, false
);
static int jXMin = 1023;
static int jXMax = 0;
static int jYMin = 1023;
static int jYMax = 0;
static int cXMin = 1023;
static int cXMax = 0;
static int cYMin = 1023;
static int cYMax = 0;
static int jLMin = 1023;
static int jLMax = 0;
static int jRMin = 1023;
static int jRMax = 0;
static unsigned char gcRaw[64] = {};
static struct {
unsigned int bStart;
unsigned int bY;
unsigned int bX;
unsigned int bB;
unsigned int bA;
unsigned int bL;
unsigned int bR;
unsigned int bZ;
unsigned int dUp;
unsigned int dDown;
unsigned int dRight;
unsigned int dLeft;
int jX;
int jY;
int cX;
int cY;
int jL;
int jR;
} gcStatus;
static int btoi(unsigned char *data, int start, int end) {
int result = 0;
int counter = end - start;
for (int i = start; i < end + 1; i++) {
result |= (gcRaw[i] << counter);
counter--;
}
return result;
}
static void joystickSend() {
Joystick.setButton(0, gcStatus.bStart);
Joystick.setButton(1, gcStatus.bY);
Joystick.setButton(2, gcStatus.bX);
Joystick.setButton(3, gcStatus.bB);
Joystick.setButton(4, gcStatus.bA);
Joystick.setButton(5, gcStatus.bZ);
Joystick.setButton(6, gcStatus.bL);
Joystick.setButton(7, gcStatus.bR);
Joystick.setButton(8, gcStatus.dUp);
Joystick.setButton(9, gcStatus.dRight);
Joystick.setButton(10, gcStatus.dDown);
Joystick.setButton(11, gcStatus.dLeft);
Joystick.setXAxis(gcStatus.jX);
Joystick.setYAxis(jYMax - gcStatus.jY + jYMin);
Joystick.setRxAxis(gcStatus.cX);
Joystick.setRyAxis(gcStatus.cY);
Joystick.setBrake(gcStatus.jL);
Joystick.setThrottle(jRMax - gcStatus.jR + jRMin);
Joystick.sendState();
}
static void gcInit() {
unsigned char initialize = 0x00;
gcSend(&initialize, 1);
for (int x = 0; x < 64; x++) {
if (!GC_QUERY)
x = 0;
}
}
static void gcConvert() {
gcStatus.bStart = gcRaw[3];
gcStatus.bY = gcRaw[4];
gcStatus.bX = gcRaw[5];
gcStatus.bB = gcRaw[6];
gcStatus.bA = gcRaw[7];
gcStatus.bL = gcRaw[9];
gcStatus.bR = gcRaw[10];
gcStatus.bZ = gcRaw[11];
gcStatus.dUp = gcRaw[12];
gcStatus.dDown = gcRaw[13];
gcStatus.dRight = gcRaw[14];
gcStatus.dLeft = gcRaw[15];
gcStatus.jX = btoi(gcRaw, 16, 23);
gcStatus.jY = btoi(gcRaw, 24, 31);
gcStatus.cX = btoi(gcRaw, 32, 39);
gcStatus.cY = btoi(gcRaw, 40, 47);
gcStatus.jL = btoi(gcRaw, 48, 55);
gcStatus.jR = btoi(gcRaw, 56, 63);
}
static int gcGet() {
// need to read 8 bytes = 64 bits
int bitCounter = 0;
int timeout = 0;
// wait for line to go low
lowLoop : {
if (GC_QUERY) {
timeout++;
if (timeout >= 64)
return 0;
goto lowLoop;
} else {
// line has gone low wait 2 us and sample
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n");
goto analyze;
}
}
analyze : {
gcRaw[bitCounter] = (GC_QUERY >> 1);
bitCounter++;
if (bitCounter == 64)
return 1;
// wait for line to go high again before repeating
timeout = 0;
while (!GC_QUERY) {
timeout++;
if (timeout >= 64)
return 0;
}
timeout = 0;
goto lowLoop;
}
}
static void gcSend(unsigned char *buffer, char length) {
char bits;
outer_loop: {
bits = 8;
inner_loop: {
GC_LOW;
if (*buffer >> 7) {
// bit is a 1, remain low 1us and high 3us
asm volatile ("nop\nnop\nnop\nnop\nnop\n");
GC_HIGH;
// only 2us to allow time for statements below
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n");
} else {
// bit is a 0, remain low 3us and high 1us
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
GC_HIGH;
}
// line must return to low in 16 cycles
--bits;
if (bits != 0) {
// more delay to get exactly 16 cycles
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
// shift binary number to the left by 1 (to check next bit)
*buffer <<= 1;
goto inner_loop;
}
}
// END INNER LOOP
// Exactly 16 cycles are taken up here
--length;
if (length != 0) {
++buffer;
goto outer_loop;
}
}
// send stop bit (1)
asm volatile ("nop\nnop\nnop\nnop\n");
GC_LOW;
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
GC_HIGH;
}
static void gcCalibrate() {
for (int i = 0; i < 5000; i++) {
unsigned char command[] = {0x40, 0x03, 0x01};
noInterrupts();
GC_OUTPUT;
gcSend(command, 3);
GC_INPUT;
int status = gcGet();
interrupts();
if (!status) {
Serial.println("failed callibration");
continue;
}
gcConvert();
jXMin = (gcStatus.jX < jXMin) ? gcStatus.jX : jXMin;
jXMax = (gcStatus.jX > jXMax) ? gcStatus.jX : jXMax;
jYMin = (gcStatus.jY < jYMin) ? gcStatus.jY : jYMin;
jYMax = (gcStatus.jY > jYMax) ? gcStatus.jY : jYMax;
cXMin = (gcStatus.cX < cXMin) ? gcStatus.cX : cXMin;
cXMax = (gcStatus.cX > cXMax) ? gcStatus.cX : cXMax;
cYMin = (gcStatus.cY < cYMin) ? gcStatus.cY : cYMin;
cYMax = (gcStatus.cY > cYMax) ? gcStatus.cY : cYMax;
jLMin = (gcStatus.jL < jLMin) ? gcStatus.jL : jLMin;
jLMax = (gcStatus.jL > jLMax) ? gcStatus.jL : jLMax;
jRMin = (gcStatus.jR < jRMin) ? gcStatus.jR : jRMin;
jRMax = (gcStatus.jR > jRMax) ? gcStatus.jR : jRMax;
delay(1);
}
Joystick.setXAxisRange(jXMin, jXMax);
Joystick.setYAxisRange(jYMin, jYMax);
Joystick.setRxAxisRange(cXMin, cXMax);
Joystick.setRyAxisRange(cYMin, cYMax);
Joystick.setThrottleRange(jRMin, jRMax);
Joystick.setBrakeRange(jLMin, jLMax);
}
void setup() {
Serial.begin(9600);
Joystick.begin(false); // do not auto update state
GC_HIGH;
GC_OUTPUT;
noInterrupts();
gcInit();
interrupts();
}
void loop() {
unsigned char command[] = {0x40, 0x03, 0x00};
noInterrupts();
GC_OUTPUT;
gcSend(command, 3);
GC_INPUT;
int status = gcGet(); // return 1 if success or 0 if timeout
interrupts();
if (!status) {
Serial.println("failed");
return;
}
gcConvert();
if (gcStatus.jR && gcStatus.bZ && gcStatus.bStart && gcStatus.dDown)
gcCalibrate();
joystickSend();
delay(1);
}