-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPs2ControllerEmulator.ino
More file actions
526 lines (327 loc) · 11.2 KB
/
Ps2ControllerEmulator.ino
File metadata and controls
526 lines (327 loc) · 11.2 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <SPI.h>
#define ackPin 9
#define triPin 7
#define sqPin 6
#define oPin 5
#define exPin 4
/*
General Process
1. Set up MCU as slave - check
2. Set up MCU for interrupts, letting the ps2 handle SS and clock rate - check
3. Collect data regarding the controller state
4. When an interrupt occurs, transfer this data to the Ps2, byte by byte
Transfer Data Process:
1. Have 1 array (data.response) that can hold all the needed data to transfer.
2. For each command we recieve from the ps2, we set up the array for the needed information and the needed response length
3. Transfer it one byte at time. (NOTE: One byte per interrupt)
4. Except for a few special cases, we don't have to worry about another command from the ps2 until the transfer is complete.
5. Reset our index for the next transfer.
*/
struct DATA { //Main data structure
byte response[20];
volatile size_t count;
volatile size_t sendAck;
size_t responseLength;
bool configState;
bool aboutToConfig;
bool aboutToSwitchBetweenAnalogDigital;
bool secondPass;
bool modeLocked;
bool isDigital;
bool isAnalog;
bool ssState;
bool oldSsState;
bool FourcSecondPass;
bool aboutToSetMotorStates;
bool smallMotorOn;
long lastChange;
} data = {{0x41, 0x5A}, 0, 0, 5, false, false, false, false, false, true, false, false, false, false, false, false, 0};
void SlaveInit(void) {
/*
This function sets up the arduino in the correct configureation to communicate with the PS2.
*/
pinMode(MOSI, INPUT); //pin 11
pinMode(MISO, OUTPUT); //pin 12
pinMode(SCK, INPUT); //pin 13
pinMode(SS, INPUT); //pin 10
SPCR |= bit (SPE); //Init as slave
SPCR |= 0x2C; //Configure for LSB & correct SPI_Mode
pinMode(ackPin, OUTPUT);
digitalWrite(ackPin, LOW); //Because of external circuitry (open drain) need to flip this so it works appropriately
pinMode(triPin, INPUT_PULLUP);
pinMode(sqPin, INPUT_PULLUP);
pinMode(oPin, INPUT_PULLUP);
pinMode(exPin, INPUT_PULLUP);
SPCR |= _BV(SPIE); //Attach Interrupts
}
void setup() {
SlaveInit();
}
/*
This function reads the states of the pins and returns the correct byte describing the state of the controller
*/
byte getControllerStateFirstByte() {
byte data = 0xFF; //Buttons are ative low
byte pinState = PIND; //digitalRead() is too slow, we need to read the pins directly from the register
//The following are examples of reading the pins and correctly setting up the data to be transferred.
/*
if (!(pinState & 0b00001000)) {
data &= ~0x10; // up
}
if (!(pinState & 00000010)) {
data &= ~0x40; //down
}
*/
return data;
}
byte getControllerStateSecondByte() {
/*
This function reads the states of the pins and
sets up the byte describing the state of the controller appropriately
*/
byte pinState = PIND;
byte data = 0xFF;
//The following are examples of reading the pins and correctly setting up the data to be transferred.
/*
if (!(pinState & 0b10000000)) {
data &= ~0x10; //triangle
}
if (!(pinState & 0b01000000)) {
data &= ~0x40; // X
}
if (!(pinState & 0b00100000)) {
data &= ~0x20; // O
}
if (!(pinState & 0b00010000)) {
data &= ~0x80; //Square
}
*/
return data; //Buttons are active low in the bus, so we invert all the bits
}
// SPI interrupt routine
ISR (SPI_STC_vect) {
//By the time we reach this routine, we have already transferred the first byte i.e CMD: 0x01, Data: 0xFF, aka the first byte of the header has already been sent
byte cmd = SPDR; //Read incomming command from ps2
switch (cmd) { //Main logic handling of the cmds of the ps2
//See http://store.curiousinventor.com/guides/PS2/ for more information about these commands.
case 0x41:
data.responseLength = 9;
if (data.configState && data.isAnalog) {
data.response[0] = 0x79;
data.response[1] = 0x5a;
data.response[2] = 0x00;
data.response[3] = 0x00;
data.response[4] = 0x03;
data.response[5] = 0x00;
data.response[6] = 0x00;
data.response[7] = 0x5A; //last byte is always 0x5A
} else if (data.configState && data.isDigital) {
data.response[0] = 0x41;
data.response[1] = 0x5A;
data.response[2] = 0xFF;
data.response[3] = 0xFF;
data.response[4] = 0x00;
data.response[5] = 0x00;
data.response[6] = 0x00;
data.response[7] = 0x5A; //last byte is always 0x5A
}
break;
case 0x42:
if (data.isDigital) {
data.responseLength = 5;
data.response[0] = 0x41;
}
else if (data.isAnalog) {
data.responseLength = 9;
data.response[0] = 0x79;
data.response[4] = 0x7F;
data.response[5] = 0x7F;
data.response[6] = 0x7F;
data.response[7] = 0x7F;
data.response[8] = 0x7F;
}
/*
TODO: handle rumble here
*/
data.response[1] = 0x5A;
data.response[2] = getControllerStateFirstByte();
data.response[3] = getControllerStateSecondByte(); //This is the 5th byte
break;
case 0x43: // enter/exit config mode
data.aboutToConfig = true;
data.responseLength = 9;
if (data.isDigital) {
data.responseLength = 5;
data.response[0] = 0x41;
}
else if (data.isAnalog) {
data.response[0] = 0x79; //analog mode
if (!(data.configState)) {
data.response[4] = 0x7F; //analog sticks
data.response[5] = 0x7F;
data.response[6] = 0x7F;
data.response[7] = 0x7F;
}
}
if (!(data.configState)) {
data.response[2] = getControllerStateFirstByte();
data.response[3] = getControllerStateSecondByte();
//No vibration motor control necessary
}
break;
case 0x44:
data.responseLength = 9;
if (data.configState) { //Only works if we are in config mode
data.aboutToSwitchBetweenAnalogDigital = true;
data.response[0] = 0xF3; //Stating that we are indeed in config mode
data.response[1] = 0x5A;
}
break;
case 0x45:
data.responseLength = 9;
// if (data.configState) { //For some reason this causes glitches though all documentation says we should be in config mode
data.response[0] = 0xF3;
data.response[1] = 0x5A;
data.response[2] = 0x03;
data.response[3] = 0x02;
data.response[4] = 0x00; //0x01 if LED is on, possibly a way to switch between analog & digtial?
data.response[5] = 0x02;
data.response[6] = 0x01;
data.response[7] = 0x00;
// }
break;
case 0x46:
data.responseLength = 9;
if (data.configState && data.secondPass == false) { //Only works if we are in config mode
data.response[0] = 0xF3;
data.response[1] = 0x5A;
data.response[3] = 0x00;
data.response[4] = 0x00;
data.response[5] = 0x02;
data.response[6] = 0x00;
data.response[7] = 0x0A;
data.secondPass = true;
}
break;
case 0x47:
data.responseLength = 9;
if (data.configState) {
data.response[0] = 0xF3;
data.response[1] = 0x5A;
data.response[3] = 0x00;
data.response[4] = 0x02;
data.response[5] = 0x00;
data.response[6] = 0x00;
data.response[7] = 0x0A;
}
break;
case 0x4C:
data.responseLength = 9;
if (data.configState && data.FourcSecondPass == false) {
data.response[0] = 0xF3;
data.response[1] = 0x5A;
data.response[2] = 0x00;
data.response[3] = 0x00;
data.response[4] = 0x00;
data.response[5] = 0x04;
data.response[6] = 0x00;
data.response[7] = 0x00;
data.FourcSecondPass = true;
}
break;
case 0x4D:
//Not implemented
break;
case 0x4F:
//Not implemented
break;
case 0x00: //Special commands after recieving a specific command
if (data.count == 2) { //This jumps over the end of the header where the cmd would be 0x00
if (data.aboutToConfig) {
data.configState = false;
data.response[0] = 0x41;
data.aboutToConfig = false;
}
else if (data.aboutToSwitchBetweenAnalogDigital && data.modeLocked == false) {
data.isDigital = true;
data.isAnalog = false;
data.response[0] = 0x41;
data.aboutToSwitchBetweenAnalogDigital = false;
}
else if (data.aboutToSetMotorStates) {
data.smallMotorOn = true;
}
}
break;
case 0x01: //Special commands after recieving a specific command
if (data.count == 2) { //this jumps over the header where cmd would be 0x01, may not need this
if (data.aboutToConfig) {
data.configState = true;
data.response[0] = 0xF3;
data.aboutToConfig = false;
}
else if (data.aboutToSwitchBetweenAnalogDigital && data.modeLocked == false) {
data.isDigital = false;
data.isAnalog = true;
data.response[0] = 0xF3;
data.aboutToSwitchBetweenAnalogDigital = false;
}
else if (data.configState && data.secondPass) { //Only works if we are in config mode
data.response[0] = 0xF3;
data.response[1] = 0x5A;
data.response[3] = 0x00;
data.response[4] = 0x00;
data.response[5] = 0x00;
data.response[6] = 0x00;
data.response[7] = 0x14;
}
else if (data.configState && data.FourcSecondPass) { //Only works if we are in config mode
data.response[0] = 0xF3;
data.response[1] = 0x5A;
data.response[3] = 0x00;
data.response[4] = 0x00;
data.response[5] = 0x06;
data.response[6] = 0x00;
data.response[7] = 0x00;
}
}
break;
case 0x03: //Lock Analog/Digital
data.modeLocked = true;
data.response[4] = 0x00;
data.response[5] = 0x00;
data.response[6] = 0x00;
data.response[7] = 0x00;
data.aboutToSwitchBetweenAnalogDigital = false;
break;
}// end switch
SPDR = data.response[data.count]; //Put the data into the registers to be transferred during this interuupt
if (data.count < (data.responseLength - 1)) { //only increase our counter until we are at the end of the needed response
data.count++;
}
data.sendAck = 1;
}
void loop() {
//Send ack after each bit transfer
if (data.sendAck) {
digitalWrite(ackPin, HIGH); //Again in opposite configuration due to external open drain circuitry
delayMicroseconds(4);
digitalWrite(ackPin, LOW);
data.sendAck = 0;
}
//Debouce the SS line
/*
We debounce this line so that our count is reset appropriately when we aren't communicating
*/
data.ssState = digitalRead(SS);
if (data.ssState == HIGH) { //Reset where we are when we aren't communicating
if (millis() - data.lastChange > 10) { //State is valid
data.count = 0;
data.responseLength = 0;
}
}
if (data.ssState != data.oldSsState) {
data.lastChange = millis();
}
data.oldSsState = data.ssState;
} //end loop()