-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPertuLat.ino
269 lines (237 loc) · 6.67 KB
/
PertuLat.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
#include "Nintendo.h" // For GC Mode
// light sensor
#define LIGHT_SENSOR_PIN 11
#define LIGHT 0
#define NO_LIGHT 1
//button
#define BUTTON_PIN 10
#define BUTTON_PRESSED 0
#define BUTTON_RELEASED 1
//GC Stuff
#define GC_PIN 5
CGamecubeConsole GamecubeConsole(GC_PIN);
Gamecube_Data_t data = defaultGamecubeData;
CGamecubeController GamecubeController1(7);
unsigned int actionCounter = 0;
bool testReady = 0;
//modes
#define PC_MODE 0
#define GC_MODE 1
//Mode var
unsigned int mode = PC_MODE;
// used in tests to get time when test starts
unsigned long startTime;
// used in tests to get time when sensor responded to pixel changes
unsigned long endTime;
// variable for reading the light sensor state
bool lightSensorState = 0;
//Control vars
float click = -1.0;
byte * bClick = (byte *) &click;
float gcWriteError = -2.0;
byte * bGcWriteError = (byte *) &gcWriteError;
void setup() {
// initialize serial communication at 19200 bits per second:
Serial.begin(19200);
// initialize light sensor pin as an input
pinMode(LIGHT_SENSOR_PIN, INPUT);
// initialize the LED pin as an output:
pinMode(LED_BUILTIN, OUTPUT);
//Initialize button as input
pinMode(BUTTON_PIN, INPUT_PULLUP);
if(digitalRead(BUTTON_PIN) == BUTTON_PRESSED){
GC_Mode_Setup();
}
else{
for(int i = 0; i < 2; i++){
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(750);
}
}
}
void GC_Mode_Setup()
{
TCCR1A = 0; // Init Timer1A
TCCR1B = 0; // Init Timer1B
TCCR1B |= B00000101; // Prescaler = 256
OCR1A = 62500; // Timer Compare1A Register
TIMSK1 |= B00000010; // Enable Timer COMPA Interrupt
GamecubeController1.read();
for(int i = 0; i < 2; i++){
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
data.report.a = 0;
data.report.b = 0;
data.report.x = 0;
data.report.y = 0;
data.report.ddown = 0;
data.report.dleft = 0;
data.report.dright = 0;
data.report.dup = 0;
mode = GC_MODE;
}
ISR(TIMER1_COMPA_vect)
{
// Handle The Timer Interrupt
if(actionCounter == 0){ //Fake a press because it gets skipped
actionCounter++;
data.report.a = 1;
OCR1A += 7813; // 0.5sec
}else if(actionCounter== 1){ //Fake a press release
actionCounter++;
data.report.a = 0;
OCR1A += 15624; //1sec
}else if(actionCounter == 2){ //Dpad Right
actionCounter++;
data.report.dright = 1;
OCR1A += 15624; //1sec
}else if(actionCounter == 3){ //Dpad Up
actionCounter++;
data.report.dright = 0;
data.report.dup = 1;
OCR1A += 15624; //1sec
}else if(actionCounter == 4){ //A - Enter Games & More
actionCounter++;
data.report.dup = 0;
data.report.a = 1;
OCR1A += 15624; //1sec
}else if(actionCounter == 5){ //Dpad Down
actionCounter++;
data.report.a = 0;
data.report.ddown = 1;
OCR1A += 1563; // 0.1sec
}else if(actionCounter == 6){ //Dpad Left
actionCounter++;
data.report.ddown = 0;
data.report.dleft = 1;
OCR1A += 15624; // 1sec
}else if(actionCounter == 7){ //A - Enter training mode
actionCounter++;
data.report.dleft = 0;
data.report.a = 1;
OCR1A += 46875; // 3sec
}else if(actionCounter == 8){ //Dpad right
actionCounter++;
data.report.a = 0;
data.report.dright = 1;
OCR1A += 14062; // 0.9sec
}else if(actionCounter == 9){ //Dpad down
actionCounter++;
data.report.dright = 0;
data.report.ddown = 1;
OCR1A += 3906; // 0.25sec
}else if(actionCounter == 10){ //X - change to battlefield form
actionCounter++;
data.report.ddown = 0;
data.report.x = 1;
OCR1A += 3906; // 0.25sec
}else if(actionCounter == 11){ //release X
actionCounter++;
data.report.x = 0;
OCR1A += 3906; // 0.25sec
}else if(actionCounter == 12){ //X - change to omega form
actionCounter++;
data.report.x = 1;
OCR1A += 3906; // 0.25sec
}else if(actionCounter == 13){ //A - choose 75m omega
actionCounter++;
data.report.x = 0;
data.report.a = 1;
OCR1A += 62500; // 4sec
}else if(actionCounter == 14){ //Wait extented
actionCounter++;
OCR1A += 62500; // 4sec
}else if(actionCounter == 15){ //Dpad up
actionCounter++;
data.report.a = 0;
data.report.dup = 1;
OCR1A += 7032; // 0.45sec
}else if(actionCounter == 16){ //Dpad right
actionCounter++;
data.report.dup = 0;
data.report.dright = 1;
OCR1A += 17968; // 1.15sec
}else if(actionCounter == 17){ //A - Pick Little Mac
actionCounter++;
data.report.dright = 0;
data.report.a = 1;
OCR1A += 3906; // 0.25sec
}else if(actionCounter == 18){ //Start - Start Match
actionCounter++;
data.report.a = 0;
data.report.start = 1;
OCR1A += 62500; // 4sec
}else if(actionCounter == 19){ //Wait for match to load
actionCounter++;
data.report.start = 0;
digitalWrite(LED_BUILTIN, HIGH);
while(digitalRead(BUTTON_PIN) == BUTTON_RELEASED){
GamecubeConsole.write(data);
}
digitalWrite(LED_BUILTIN, LOW);
testReady = 1;
TIMSK1 = B00000000; //Disable timer interrupts
}
}
void GC_loop(){
if(testReady){
data.report.r = 1;
data.report.right = 200; //Analog R
lightSensorState = digitalRead(LIGHT_SENSOR_PIN); // get state of light sensor
startTime = micros();
// wait until sensor's state is changed
while(lightSensorState == NO_LIGHT){
GamecubeConsole.write(data);
lightSensorState = digitalRead(LIGHT_SENSOR_PIN); // get state of light sensor
}
endTime = micros();
data.report.r = 0;
data.report.right = 35; // Analog R
float responseTime = (endTime - startTime)/1000.0;
byte * b = (byte *) &responseTime;
Serial.write(b,4);
// wait 1000 polls (around 1 second. Depends on the adapter polling rate)
for(int i = 0; i < 1000; i++){
GamecubeConsole.write(data);
}
}
else{
// Write controller data to the console
if (!GamecubeConsole.write(data)){
Serial.write(bGcWriteError,4);
delay(1000);
}
}
}
void PC_loop(){
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH);
lightSensorState = digitalRead(LIGHT_SENSOR_PIN); // get state of light sensor
Serial.write(bClick,4);
startTime = micros();
// wait until sensor's state is changed
while(lightSensorState == NO_LIGHT)
{
lightSensorState = digitalRead(LIGHT_SENSOR_PIN); // get state of light sensor
}
endTime = micros();
Serial.write(bClick,4);
float responseTime = (endTime - startTime)/1000.0;
byte * b = (byte *) &responseTime;
Serial.write(b,4);
digitalWrite(LED_BUILTIN, LOW);
delay(random(400, 600));
}
void loop() {
if(mode == GC_MODE){
GC_loop();
}
else{
PC_loop();
}
}