Vlad Cazan / Mbed 2 deprecated PlayStationController

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut PSclock(p21);
00004 DigitalIn PSdata(p22);
00005 DigitalIn PSack(p23);
00006 DigitalOut PScommand(p24);
00007 DigitalOut PSattention(p25);
00008 
00009 Serial pc(USBTX, USBRX); // tx, rx
00010 int temp, data0, data1, data2, data3, data4, data5, i ,debounceSelect;
00011 
00012 // enable interupts
00013 
00014 int _BV(int bit) {
00015     return 1 << bit;
00016 }
00017 
00018 // PSx controller communication function.
00019 // send a byte on the command line and receive one on the data line.
00020 // needs Attention pin to have gone low before called to activate controller.
00021 char gameByte (char command) {
00022     wait_us(1);
00023     char data = 0x00;                             // clear data variable to save setting low bits later.
00024     for (int i=0; i<8; i++) {
00025         if (command & _BV(i)) {
00026             PScommand=1;    // bit bang "command" out on PScommand wire.
00027         } else {
00028             PScommand = 0;
00029         }
00030         PSclock = 0;                            // CLOCK LOW
00031         wait_us(20);                                              // wait for output to stabilise
00032         //if ((PIND & _BV(PSdata)))_SFR_BYTE(data) |= _BV(i); // read PSdata pin and store
00033         //else cbi(data, i);
00034         PSclock = 1;                             // CLOCK HIGH
00035     }
00036     PScommand = 1;
00037 
00038     wait_us(20);                             // wait for ACK to pass.
00039 
00040     return data;
00041 }
00042 
00043 
00044 //sei();
00045 
00046 // this loop continues to put PSx controller into analouge mode untill the
00047 // controller responds with 0x73 in the 2nd byte.
00048 // (PS2 controller responds with 0x73 when in analouge mode.)
00049 // the status LEDs will continue to count upwards untill a controller is found.
00050 // if everything is working correctly this should happen on the first pass of
00051 // this loop but occasionally errors occur and a 2nd or 3rd itteration happen.
00052 int chk_ana = 0, cnt = 0;
00053 
00054 void startup() {
00055 
00056     while (chk_ana != 0x73) {
00057         // put controller in config mode
00058         PScommand = 1;
00059         PSclock = 1;
00060         PSattention =0;
00061 
00062         gameByte(0x01);
00063         gameByte(0x43);
00064         gameByte(0x00);
00065         gameByte(0x01);
00066         gameByte(0x00);
00067 
00068         PScommand = 1;
00069         wait_ms(1);
00070         PSattention=1;
00071 
00072         wait_ms(10);
00073 
00074         // put controller in analog mode
00075         PScommand = 1;
00076         PSclock =1;
00077         PSattention =1;;
00078 
00079         gameByte(0x01);
00080         gameByte(0x44);
00081         gameByte(0x00);
00082         gameByte(0x01);
00083         gameByte(0x03);
00084         gameByte(0x00);
00085         gameByte(0x00);
00086         gameByte(0x00);
00087         gameByte(0x00);
00088 
00089         PScommand = 1;
00090         wait_ms(1);
00091         PSattention =1;
00092 
00093         wait_ms(10);
00094 
00095         // exit config mode
00096         PScommand = 1;
00097         PSclock = 1;
00098         PSattention = 1;
00099 
00100         gameByte(0x01);
00101         gameByte(0x43);
00102         gameByte(0x00);
00103         gameByte(0x00);
00104         gameByte(0x5A);
00105         gameByte(0x5A);
00106         gameByte(0x5A);
00107         gameByte(0x5A);
00108         gameByte(0x5A);
00109 
00110         PScommand = 1;
00111         wait_ms(1);
00112         PSattention = 1;
00113 
00114         wait_ms(10);
00115         // poll controller and check in analouge mode.
00116         PScommand = 1;
00117         PSclock=1;
00118         PSattention=0;
00119 
00120         gameByte(0x01);
00121         chk_ana = gameByte(0x42);            // the 2nd byte to be returned from the controller should = 0x73 for "red" analouge controller.
00122         gameByte(0x00);
00123         gameByte(0x00);
00124         gameByte(0x00);
00125         gameByte(0x00);
00126         gameByte(0x00);
00127         gameByte(0x00);
00128         gameByte(0x00);
00129 
00130         PScommand = 1;
00131         wait_ms(1);
00132         PSattention =1;
00133 
00134         wait_ms(10);
00135 
00136 
00137         // keep increasing counter to be dispalyed untill PSx controller confirms it's in analouge mode.
00138         pc.putc(cnt++);
00139         if (cnt > 254) {
00140             cnt=0;
00141         }
00142     }
00143 }
00144 
00145 
00146 // main program loop:
00147 void loop () {
00148 
00149     PScommand=1;
00150     PSclock=1;
00151     PSattention = 0;
00152 
00153     gameByte(0x01);                                       // bite 0. header.
00154     temp = gameByte(0x42);                          // bite 1. header. (should possibly put test on this byte to detect unplugging of controller.)
00155     gameByte(0x00);                                       // bite 2. header.
00156 
00157     data0 = gameByte(0x00);                         // bite 3. first data bite.
00158     data1 = gameByte(0x00);                         // bite 4.
00159     data2 = gameByte(0x00);                         // bite 5.
00160     data3 = gameByte(0x00);                         // bite 6.
00161     data4 = gameByte(0x00);                         // bite 7.
00162     data5 = gameByte(0x00);                         // bite 8.
00163 
00164     wait_ms(1);
00165     PScommand = 1;                      // close communication with PSx controller
00166     wait_ms(1);
00167     PSattention = 1;                        // all done.
00168 
00169 
00170 
00171     if (!(data0 & _BV(0)) && !debounceSelect) {     // capture one unique press of the "select" button
00172         debounceSelect = 1;
00173     } else if ((data0 & _BV(0)) && debounceSelect) {
00174         if (i++ >= 5) i=0;
00175         debounceSelect = 0;
00176     }
00177 
00178 
00179     // this switch decides which data register to show on status LEDs depending on how many times
00180     // the "select" button on the PS2 controller has been pressed.
00181     switch (i) {
00182         case 0:
00183             pc.printf("case 0: %d\n", data0);
00184             break;
00185         case 1:
00186             pc.printf("case 1: %d\n", data1);
00187             break;
00188         case 2:
00189             pc.printf("case 2: %d\n", data2);
00190             break;
00191         case 3:
00192             pc.printf("case 3: %d\n", data3);
00193             break;
00194         case 4:
00195             pc.printf("case 4: %d\n", data4);
00196             break;
00197         case 5:
00198             pc.printf("case 5: %d\n", data5);
00199     }
00200 
00201     pc.printf("$d\n",data1);
00202 } //void loop
00203 
00204 
00205 int main() {
00206     startup();
00207     while (1) {
00208         loop();
00209     }
00210 }