PS4 controller with IM315

Dependents:  

Committer:
m2130
Date:
Sat Aug 06 07:19:29 2022 +0000
Revision:
0:9577e971f55b
Child:
2:83f5186d950c
PS4 controller with IM315

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m2130 0:9577e971f55b 1 #include "IM315PS4.hpp"
m2130 0:9577e971f55b 2
m2130 0:9577e971f55b 3 IM315PS4::IM315PS4(PinName TX, PinName RX){
m2130 0:9577e971f55b 4 i = 0;
m2130 0:9577e971f55b 5 len = 17;
m2130 0:9577e971f55b 6 val = ':';
m2130 0:9577e971f55b 7 data = new uint8_t[len];
m2130 0:9577e971f55b 8 DATA = new uint8_t[8];
m2130 0:9577e971f55b 9 _pc = new Serial(USBTX, USBRX);
m2130 0:9577e971f55b 10 dev = new Serial(TX, RX);
m2130 0:9577e971f55b 11 dev -> baud(19200);
m2130 0:9577e971f55b 12 _pc -> baud(9600);
m2130 0:9577e971f55b 13 dev -> printf("SRST\r\n");
m2130 0:9577e971f55b 14 dev -> attach(callback(this,&IM315PS4::get),Serial::RxIrq);
m2130 0:9577e971f55b 15 }
m2130 0:9577e971f55b 16
m2130 0:9577e971f55b 17 IM315PS4::IM315PS4(PinName TX, PinName RX, int baud){
m2130 0:9577e971f55b 18 i = 0;
m2130 0:9577e971f55b 19 len = 17;
m2130 0:9577e971f55b 20 val = ':';
m2130 0:9577e971f55b 21 data = new uint8_t[len];
m2130 0:9577e971f55b 22 DATA = new uint8_t[8];
m2130 0:9577e971f55b 23 _pc = new Serial(USBTX, USBRX);
m2130 0:9577e971f55b 24 dev = new Serial(TX, RX);
m2130 0:9577e971f55b 25 dev -> baud(19200);
m2130 0:9577e971f55b 26 _pc -> baud(baud);
m2130 0:9577e971f55b 27 dev -> printf("SRST\r\n");
m2130 0:9577e971f55b 28 dev -> attach(callback(this,&IM315PS4::get),Serial::RxIrq);
m2130 0:9577e971f55b 29 }
m2130 0:9577e971f55b 30
m2130 0:9577e971f55b 31 void IM315PS4::get(){
m2130 0:9577e971f55b 32 if(dev -> readable()){
m2130 0:9577e971f55b 33 data[i] = dev -> getc();
m2130 0:9577e971f55b 34 if(data[i] == val){
m2130 0:9577e971f55b 35 if(i == 0) i++;
m2130 0:9577e971f55b 36 else{
m2130 0:9577e971f55b 37 data[0] = val;
m2130 0:9577e971f55b 38 i = 1;
m2130 0:9577e971f55b 39 }
m2130 0:9577e971f55b 40 }
m2130 0:9577e971f55b 41 else{
m2130 0:9577e971f55b 42 if(i == 0);
m2130 0:9577e971f55b 43 else if(data[i] == ',');
m2130 0:9577e971f55b 44 else i++;
m2130 0:9577e971f55b 45 }
m2130 0:9577e971f55b 46 if(i >= len){
m2130 0:9577e971f55b 47 if(data[0] == val){
m2130 0:9577e971f55b 48 DATA[0] = ASCII(data[1], data[2]);
m2130 0:9577e971f55b 49 DATA[1] = ASCII(data[3], data[4]);
m2130 0:9577e971f55b 50 DATA[2] = ASCII(data[5], data[6]);
m2130 0:9577e971f55b 51 DATA[3] = ASCII(data[7], data[8]);
m2130 0:9577e971f55b 52 DATA[4] = ASCII(data[9], data[10]);
m2130 0:9577e971f55b 53 DATA[5] = ASCII(data[11], data[12]);
m2130 0:9577e971f55b 54 DATA[6] = ASCII(data[13], data[14]);
m2130 0:9577e971f55b 55 DATA[7] = ASCII(data[15], data[16]);
m2130 0:9577e971f55b 56 }
m2130 0:9577e971f55b 57 i = 0;
m2130 0:9577e971f55b 58 }
m2130 0:9577e971f55b 59 }
m2130 0:9577e971f55b 60 else{
m2130 0:9577e971f55b 61 for(int i = 0 ; i < 8 ; i++)DATA[i] = 0;
m2130 0:9577e971f55b 62 }
m2130 0:9577e971f55b 63 }
m2130 0:9577e971f55b 64
m2130 0:9577e971f55b 65 uint8_t IM315PS4::ASCII(uint8_t data1, uint8_t data2){
m2130 0:9577e971f55b 66 uint8_t dat1 = 0;
m2130 0:9577e971f55b 67 uint8_t dat2 = 0;
m2130 0:9577e971f55b 68 if(data1 >= 0x30 && data1 <= 0x39)dat1 = 16 * (data1 - 0x30);
m2130 0:9577e971f55b 69 else if(data1 >= 0x41 && data1 <= 0x46)dat1 = 16 * (data1 - 0x37);
m2130 0:9577e971f55b 70 if(data2 >= 0x30 && data2 <= 0x39)dat2 = data2 - 0x30;
m2130 0:9577e971f55b 71 else if(data2 >= 0x41 && data2 <= 0x46)dat2 = data2 - 0x37;
m2130 0:9577e971f55b 72 return dat1 + dat2;
m2130 0:9577e971f55b 73 }
m2130 0:9577e971f55b 74
m2130 0:9577e971f55b 75 void IM315PS4::button_state(){
m2130 0:9577e971f55b 76 if(up()) _pc -> printf("Up ");
m2130 0:9577e971f55b 77 if(right()) _pc -> printf("Right ");
m2130 0:9577e971f55b 78 if(left()) _pc -> printf("Left ");
m2130 0:9577e971f55b 79 if(down()) _pc -> printf("Down ");
m2130 0:9577e971f55b 80 if(tri()) _pc -> printf("Tri ");
m2130 0:9577e971f55b 81 if(circle()) _pc -> printf("Circle ");
m2130 0:9577e971f55b 82 if(square()) _pc -> printf("Square ");
m2130 0:9577e971f55b 83 if(cross()) _pc -> printf("Cross ");
m2130 0:9577e971f55b 84 if(l1()) _pc -> printf("L1 ");
m2130 0:9577e971f55b 85 if(l2An() != 0)_pc -> printf("L2:%2x ", l2An());
m2130 0:9577e971f55b 86 if(l3()) _pc -> printf("L3 ");
m2130 0:9577e971f55b 87 if(r1()) _pc -> printf("R1 ");
m2130 0:9577e971f55b 88 if(r2An() != 0)_pc -> printf("R2:%2x ", r2An());
m2130 0:9577e971f55b 89 if(r3()) _pc -> printf("R3 ");
m2130 0:9577e971f55b 90 if(option()) _pc -> printf("OPTIONS ");
m2130 0:9577e971f55b 91 if(share()) _pc -> printf("SHARE ");
m2130 0:9577e971f55b 92 if(ps()) _pc -> printf("PS ");
m2130 0:9577e971f55b 93 if(touchpad()) _pc -> printf("TOUCHPAD ");
m2130 0:9577e971f55b 94 if(lsx() != 127) _pc -> printf("LSX:%3d ", lsx());
m2130 0:9577e971f55b 95 if(lsy() != 127) _pc -> printf("LSY:%3d ", lsy());
m2130 0:9577e971f55b 96 if(rsx() != 127) _pc -> printf("RSX:%3d ", rsx());
m2130 0:9577e971f55b 97 if(rsy() != 127)_pc -> printf("RSY:%3d ", rsy());
m2130 0:9577e971f55b 98 _pc -> printf("\n");
m2130 0:9577e971f55b 99 }
m2130 0:9577e971f55b 100 void IM315PS4::data_print(){_pc -> printf("%2x %2x %2x %2x %2x %2x %2x %2x\n", DATA[0], DATA[1], DATA[2], DATA[3], DATA[4], DATA[5], DATA[6], DATA[7]);}
m2130 0:9577e971f55b 101
m2130 0:9577e971f55b 102 bool IM315PS4::up(){return DATA[1] & UP ? 1 : 0;}
m2130 0:9577e971f55b 103
m2130 0:9577e971f55b 104 bool IM315PS4::right(){return DATA[1] & RIGHT ? 1 : 0;}
m2130 0:9577e971f55b 105
m2130 0:9577e971f55b 106 bool IM315PS4::left(){return DATA[1] & LEFT ? 1 : 0;}
m2130 0:9577e971f55b 107
m2130 0:9577e971f55b 108 bool IM315PS4::down(){return DATA[1] & DOWN ? 1 : 0;}
m2130 0:9577e971f55b 109
m2130 0:9577e971f55b 110 bool IM315PS4::tri(){return DATA[0] & TRI ? 1 : 0;}
m2130 0:9577e971f55b 111
m2130 0:9577e971f55b 112 bool IM315PS4::circle(){return DATA[0] & CIRCLE ? 1 : 0;}
m2130 0:9577e971f55b 113
m2130 0:9577e971f55b 114 bool IM315PS4::square(){return DATA[0] & SQUARE ? 1 : 0;}
m2130 0:9577e971f55b 115
m2130 0:9577e971f55b 116 bool IM315PS4::cross(){return DATA[0] & CROSS ? 1 : 0;}
m2130 0:9577e971f55b 117
m2130 0:9577e971f55b 118 bool IM315PS4::touchpad(){return DATA[0] & TOUCHPAD ? 1 : 0;}
m2130 0:9577e971f55b 119
m2130 0:9577e971f55b 120 bool IM315PS4::ps(){return DATA[0] & PS ? 1 : 0;}
m2130 0:9577e971f55b 121
m2130 0:9577e971f55b 122 bool IM315PS4::l1(){return DATA[0] & L1 ? 1 : 0;}
m2130 0:9577e971f55b 123
m2130 0:9577e971f55b 124 bool IM315PS4::r1(){return DATA[0] & R1 ? 1 : 0;}
m2130 0:9577e971f55b 125
m2130 0:9577e971f55b 126 bool IM315PS4::option(){return DATA[1] & OPTION ? 1 : 0;}
m2130 0:9577e971f55b 127
m2130 0:9577e971f55b 128 bool IM315PS4::share(){return DATA[1] & SHARE ? 1 : 0;}
m2130 0:9577e971f55b 129
m2130 0:9577e971f55b 130 bool IM315PS4::l3(){return DATA[1] & L3 ? 1 : 0;}
m2130 0:9577e971f55b 131
m2130 0:9577e971f55b 132 bool IM315PS4::r3(){return DATA[1] & R3 ? 1 : 0;}
m2130 0:9577e971f55b 133
m2130 0:9577e971f55b 134 uint8_t IM315PS4::l2An(){return DATA[2];}
m2130 0:9577e971f55b 135
m2130 0:9577e971f55b 136 uint8_t IM315PS4::r2An(){return DATA[3];}
m2130 0:9577e971f55b 137
m2130 0:9577e971f55b 138 uint8_t IM315PS4::lsx(){return DATA[4];}
m2130 0:9577e971f55b 139
m2130 0:9577e971f55b 140 uint8_t IM315PS4::lsy(){return DATA[5];}
m2130 0:9577e971f55b 141
m2130 0:9577e971f55b 142 uint8_t IM315PS4::rsx(){return DATA[6];}
m2130 0:9577e971f55b 143
m2130 0:9577e971f55b 144 uint8_t IM315PS4::rsy(){return DATA[7];}
m2130 0:9577e971f55b 145
m2130 0:9577e971f55b 146 IM315PS4::~IM315PS4(){
m2130 0:9577e971f55b 147 delete data;
m2130 0:9577e971f55b 148 delete DATA;
m2130 0:9577e971f55b 149 delete dev;
m2130 0:9577e971f55b 150 delete _pc;
m2130 0:9577e971f55b 151 }