IM315を用いてDS2(PS2コントローラー)を扱うライブラリ
IM315.cpp@0:06c265b1fbd1, 2022-04-04 (annotated)
- Committer:
- m2130
- Date:
- Mon Apr 04 06:15:23 2022 +0000
- Revision:
- 0:06c265b1fbd1
- Child:
- 1:9ad38dad969c
IM315 de DS2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m2130 | 0:06c265b1fbd1 | 1 | #include "IM315.hpp" |
m2130 | 0:06c265b1fbd1 | 2 | |
m2130 | 0:06c265b1fbd1 | 3 | IM315::IM315(PinName TX, PinName RX){ |
m2130 | 0:06c265b1fbd1 | 4 | i = 0; |
m2130 | 0:06c265b1fbd1 | 5 | len = 17; |
m2130 | 0:06c265b1fbd1 | 6 | val = 0x3a; |
m2130 | 0:06c265b1fbd1 | 7 | data = new unsigned char[len]; |
m2130 | 0:06c265b1fbd1 | 8 | DATA = new unsigned char[8]; |
m2130 | 0:06c265b1fbd1 | 9 | _pc = new Serial(USBTX, USBRX); |
m2130 | 0:06c265b1fbd1 | 10 | dev = new Serial(TX, RX); |
m2130 | 0:06c265b1fbd1 | 11 | dev -> baud(19200); |
m2130 | 0:06c265b1fbd1 | 12 | _pc -> baud(9600); |
m2130 | 0:06c265b1fbd1 | 13 | dev -> printf("SRST\r\n"); |
m2130 | 0:06c265b1fbd1 | 14 | dev -> attach(callback(this,&IM315::get),Serial::RxIrq); |
m2130 | 0:06c265b1fbd1 | 15 | } |
m2130 | 0:06c265b1fbd1 | 16 | |
m2130 | 0:06c265b1fbd1 | 17 | IM315::IM315(PinName TX, PinName RX, int baud){ |
m2130 | 0:06c265b1fbd1 | 18 | i = 0; |
m2130 | 0:06c265b1fbd1 | 19 | len = 17; |
m2130 | 0:06c265b1fbd1 | 20 | val = 0x3a; |
m2130 | 0:06c265b1fbd1 | 21 | data = new unsigned char[len]; |
m2130 | 0:06c265b1fbd1 | 22 | DATA = new unsigned char[8]; |
m2130 | 0:06c265b1fbd1 | 23 | _pc = new Serial(USBTX, USBRX); |
m2130 | 0:06c265b1fbd1 | 24 | dev = new Serial(TX, RX); |
m2130 | 0:06c265b1fbd1 | 25 | dev -> baud(19200); |
m2130 | 0:06c265b1fbd1 | 26 | _pc -> baud(baud); |
m2130 | 0:06c265b1fbd1 | 27 | dev -> attach(callback(this,&IM315::get),Serial::RxIrq); |
m2130 | 0:06c265b1fbd1 | 28 | } |
m2130 | 0:06c265b1fbd1 | 29 | |
m2130 | 0:06c265b1fbd1 | 30 | void IM315::get(){ |
m2130 | 0:06c265b1fbd1 | 31 | if(dev -> readable()){ |
m2130 | 0:06c265b1fbd1 | 32 | data[i] = dev -> getc(); |
m2130 | 0:06c265b1fbd1 | 33 | if(data[i] == val){ |
m2130 | 0:06c265b1fbd1 | 34 | if(i == 0) i++; |
m2130 | 0:06c265b1fbd1 | 35 | else{ |
m2130 | 0:06c265b1fbd1 | 36 | data[0] = val; |
m2130 | 0:06c265b1fbd1 | 37 | i = 1; |
m2130 | 0:06c265b1fbd1 | 38 | } |
m2130 | 0:06c265b1fbd1 | 39 | } |
m2130 | 0:06c265b1fbd1 | 40 | else{ |
m2130 | 0:06c265b1fbd1 | 41 | if(i == 0); |
m2130 | 0:06c265b1fbd1 | 42 | else if(data[i] == ','); |
m2130 | 0:06c265b1fbd1 | 43 | else i++; |
m2130 | 0:06c265b1fbd1 | 44 | } |
m2130 | 0:06c265b1fbd1 | 45 | if(i >= len){ |
m2130 | 0:06c265b1fbd1 | 46 | if(data[0] == val){ |
m2130 | 0:06c265b1fbd1 | 47 | DATA[0] = ASCII(data[1], data[2]); |
m2130 | 0:06c265b1fbd1 | 48 | DATA[1] = ASCII(data[3], data[4]); |
m2130 | 0:06c265b1fbd1 | 49 | DATA[2] = ASCII(data[5], data[6]); |
m2130 | 0:06c265b1fbd1 | 50 | DATA[3] = ASCII(data[7], data[8]); |
m2130 | 0:06c265b1fbd1 | 51 | DATA[4] = ASCII(data[9], data[10]); |
m2130 | 0:06c265b1fbd1 | 52 | DATA[5] = ASCII(data[11], data[12]); |
m2130 | 0:06c265b1fbd1 | 53 | DATA[6] = ASCII(data[13], data[14]); |
m2130 | 0:06c265b1fbd1 | 54 | DATA[7] = ASCII(data[15], data[16]); |
m2130 | 0:06c265b1fbd1 | 55 | } |
m2130 | 0:06c265b1fbd1 | 56 | i = 0; |
m2130 | 0:06c265b1fbd1 | 57 | } |
m2130 | 0:06c265b1fbd1 | 58 | } |
m2130 | 0:06c265b1fbd1 | 59 | else{ |
m2130 | 0:06c265b1fbd1 | 60 | for(int i = 0 ; i < 8 ; i++)DATA[i] = 0; |
m2130 | 0:06c265b1fbd1 | 61 | } |
m2130 | 0:06c265b1fbd1 | 62 | } |
m2130 | 0:06c265b1fbd1 | 63 | |
m2130 | 0:06c265b1fbd1 | 64 | unsigned char IM315::ASCII(unsigned char data1, unsigned char data2){ |
m2130 | 0:06c265b1fbd1 | 65 | unsigned char dat1 = 0; |
m2130 | 0:06c265b1fbd1 | 66 | unsigned char dat2 = 0; |
m2130 | 0:06c265b1fbd1 | 67 | if(data1 >= 0x30 && data1 <= 0x39)dat1 = 16 * (data1 - 0x30); |
m2130 | 0:06c265b1fbd1 | 68 | else if(data1 >= 0x41 && data1 <= 0x46)dat1 = 16 * (data1 - 0x37); |
m2130 | 0:06c265b1fbd1 | 69 | if(data2 >= 0x30 && data2 <= 0x39)dat2 = data2 - 0x30; |
m2130 | 0:06c265b1fbd1 | 70 | else if(data2 >= 0x41 && data2 <= 0x46)dat2 = data2 - 0x37; |
m2130 | 0:06c265b1fbd1 | 71 | return dat1 + dat2; |
m2130 | 0:06c265b1fbd1 | 72 | } |
m2130 | 0:06c265b1fbd1 | 73 | |
m2130 | 0:06c265b1fbd1 | 74 | void IM315::button_state(){_pc -> printf("U: %d, D: %d, R: %d, L: %d, TRI: %d, X: %d, O: %d, SQU: %d, L1: %d, L2: %d, R1: %d, R2: %d, STA: %d, SEL: %d, L3: %3d, R3: %3d LSX: %3d, LSY: %3d, RSX: %3d, RY: %3d\n",up(), down(), right(), left(), tri(), cross(), circle(), square(), l1(), l2(), r1(), r2(), start(), select(), l3(), r3(), lsx(), lsy(), rsx(), rsy());} |
m2130 | 0:06c265b1fbd1 | 75 | |
m2130 | 0:06c265b1fbd1 | 76 | void IM315::data_print(){ |
m2130 | 0:06c265b1fbd1 | 77 | _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:06c265b1fbd1 | 78 | } |
m2130 | 0:06c265b1fbd1 | 79 | |
m2130 | 0:06c265b1fbd1 | 80 | bool IM315::up(){return DATA[2] & UP ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 81 | |
m2130 | 0:06c265b1fbd1 | 82 | bool IM315::right(){return DATA[2] & RIGHT ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 83 | |
m2130 | 0:06c265b1fbd1 | 84 | bool IM315::left(){return DATA[2] & LEFT ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 85 | |
m2130 | 0:06c265b1fbd1 | 86 | bool IM315::down(){return DATA[2] & DOWN ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 87 | |
m2130 | 0:06c265b1fbd1 | 88 | bool IM315::tri(){return DATA[1] & TRI ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 89 | |
m2130 | 0:06c265b1fbd1 | 90 | bool IM315::circle(){return DATA[1] & CIRCLE ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 91 | |
m2130 | 0:06c265b1fbd1 | 92 | bool IM315::square(){return DATA[1] & SQUARE ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 93 | |
m2130 | 0:06c265b1fbd1 | 94 | bool IM315::cross(){return DATA[1] & CROSS ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 95 | |
m2130 | 0:06c265b1fbd1 | 96 | bool IM315::l2(){return DATA[1] & L2 ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 97 | |
m2130 | 0:06c265b1fbd1 | 98 | bool IM315::r2(){return DATA[1] & R2 ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 99 | |
m2130 | 0:06c265b1fbd1 | 100 | bool IM315::l1(){return DATA[1] & L1 ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 101 | |
m2130 | 0:06c265b1fbd1 | 102 | bool IM315::r1(){return DATA[1] & R1 ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 103 | |
m2130 | 0:06c265b1fbd1 | 104 | bool IM315::start(){return DATA[2] & START ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 105 | |
m2130 | 0:06c265b1fbd1 | 106 | bool IM315::select(){return DATA[2] & SELECT ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 107 | |
m2130 | 0:06c265b1fbd1 | 108 | bool IM315::l3(){return DATA[2] & L3 ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 109 | |
m2130 | 0:06c265b1fbd1 | 110 | bool IM315::r3(){return DATA[2] & R3 ? 1 : 0;} |
m2130 | 0:06c265b1fbd1 | 111 | |
m2130 | 0:06c265b1fbd1 | 112 | char IM315::lsx(){return DATA[4];} |
m2130 | 0:06c265b1fbd1 | 113 | |
m2130 | 0:06c265b1fbd1 | 114 | char IM315::lsy(){return DATA[5];} |
m2130 | 0:06c265b1fbd1 | 115 | |
m2130 | 0:06c265b1fbd1 | 116 | char IM315::rsx(){return DATA[6];} |
m2130 | 0:06c265b1fbd1 | 117 | |
m2130 | 0:06c265b1fbd1 | 118 | char IM315::rsy(){return DATA[7];} |
m2130 | 0:06c265b1fbd1 | 119 | |
m2130 | 0:06c265b1fbd1 | 120 | IM315::~IM315(){ |
m2130 | 0:06c265b1fbd1 | 121 | delete data; |
m2130 | 0:06c265b1fbd1 | 122 | delete DATA; |
m2130 | 0:06c265b1fbd1 | 123 | delete dev; |
m2130 | 0:06c265b1fbd1 | 124 | delete _pc; |
m2130 | 0:06c265b1fbd1 | 125 | } |