SBDBTを用いてDS3(PS3コントローラ)を扱うライブラリ

Dependents:   PIDcontrol_4 go_robo2022kaisyu_no 2022_Ateam_MOTORprogram tape_LED ... more

Committer:
m2130
Date:
Wed Nov 10 13:53:15 2021 +0000
Revision:
0:d10107920170
Child:
2:9fd7393a3023
SBDBT;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m2130 0:d10107920170 1 #include "sbdbt.hpp"
m2130 0:d10107920170 2 #include "mbed.h"
m2130 0:d10107920170 3
m2130 0:d10107920170 4 sbdbt::sbdbt(PinName TX, PinName RX){
m2130 0:d10107920170 5 i = 0;
m2130 0:d10107920170 6 pos = 0;
m2130 0:d10107920170 7 val = 0x80;
m2130 0:d10107920170 8 len = 10;
m2130 0:d10107920170 9 data = new char[len];
m2130 0:d10107920170 10 PS3_Data = new char[len];
m2130 0:d10107920170 11 pc = new Serial(USBTX, USBRX);
m2130 0:d10107920170 12 dev = new Serial(TX, RX);
m2130 0:d10107920170 13 dev -> baud(9600);
m2130 0:d10107920170 14 dev -> attach(this,&sbdbt::get,Serial::RxIrq);
m2130 0:d10107920170 15 }
m2130 0:d10107920170 16
m2130 0:d10107920170 17 sbdbt::sbdbt(PinName TX, PinName RX, int baud){
m2130 0:d10107920170 18 i = 0;
m2130 0:d10107920170 19 pos = 0;
m2130 0:d10107920170 20 val = 0x80;
m2130 0:d10107920170 21 len = 10;
m2130 0:d10107920170 22 data = new char[len];
m2130 0:d10107920170 23 PS3_Data = new char[len];
m2130 0:d10107920170 24 pc = new Serial(USBTX, USBRX);
m2130 0:d10107920170 25 dev = new Serial(TX, RX);
m2130 0:d10107920170 26 dev -> baud(9600);
m2130 0:d10107920170 27 pc -> baud(baud);
m2130 0:d10107920170 28 dev -> attach(this,&sbdbt::get,Serial::RxIrq);
m2130 0:d10107920170 29 }
m2130 0:d10107920170 30
m2130 0:d10107920170 31 void sbdbt::get(){
m2130 0:d10107920170 32 data[i] = dev -> getc();
m2130 0:d10107920170 33 if(data[i] == val){
m2130 0:d10107920170 34 if(i != pos){
m2130 0:d10107920170 35 data[pos] = val;
m2130 0:d10107920170 36 i = pos + 1;
m2130 0:d10107920170 37 }
m2130 0:d10107920170 38 else{
m2130 0:d10107920170 39 i++;
m2130 0:d10107920170 40 }
m2130 0:d10107920170 41 }
m2130 0:d10107920170 42 else{
m2130 0:d10107920170 43 if(i == pos);
m2130 0:d10107920170 44 else{
m2130 0:d10107920170 45 i++;
m2130 0:d10107920170 46 }
m2130 0:d10107920170 47 }
m2130 0:d10107920170 48 if(i >= len){
m2130 0:d10107920170 49 PS3_Data[0] = data[0];
m2130 0:d10107920170 50 PS3_Data[1] = data[1];
m2130 0:d10107920170 51 PS3_Data[2] = data[2];
m2130 0:d10107920170 52 PS3_Data[3] = data[3];
m2130 0:d10107920170 53 PS3_Data[4] = data[4];
m2130 0:d10107920170 54 PS3_Data[5] = data[5];
m2130 0:d10107920170 55 PS3_Data[6] = data[6];
m2130 0:d10107920170 56 PS3_Data[7] = data[7];
m2130 0:d10107920170 57 PS3_Data[8] = data[8];
m2130 0:d10107920170 58 PS3_Data[9] = data[9];
m2130 0:d10107920170 59 i = 0;
m2130 0:d10107920170 60 }
m2130 0:d10107920170 61 }
m2130 0:d10107920170 62
m2130 0:d10107920170 63 void sbdbt::button_state(){pc -> printf("UP: %d, DOW: %d, RIG: %d, LEF: %d, TRI: %d, CRO: %d, CIR: %d, SQU: %d, L1: %d, L2: %d, R1: %d, R2: %d, STA: %d, SEL: %d LSX: %3d, LSY: %3d, RSX: %3d, RY: %3d, L2An: %3d, R2An: %3d\n",up(), down(), right(), left(), tri(), cross(), circle(), square(), l1(), l2(), r1(), r2(), start(), select(), lsx(), lsy(), rsx(), rsy(), l2An(), r2An());}
m2130 0:d10107920170 64
m2130 0:d10107920170 65 void sbdbt::data_print(){pc -> printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",PS3_Data[0], PS3_Data[1], PS3_Data[2], PS3_Data[3], PS3_Data[4], PS3_Data[5], PS3_Data[6], PS3_Data[7], PS3_Data[8], PS3_Data[9]);}
m2130 0:d10107920170 66
m2130 0:d10107920170 67 bool sbdbt::neutral(){return data[9] == 0 ? 1 : 0;}
m2130 0:d10107920170 68
m2130 0:d10107920170 69 bool sbdbt::up(){return data[2] & UP ? 1 : 0;}
m2130 0:d10107920170 70
m2130 0:d10107920170 71 bool sbdbt::down(){return data[2] & DOWN ? 1 : 0;}
m2130 0:d10107920170 72
m2130 0:d10107920170 73 bool sbdbt::left(){return data[2] & LEFT ? 1 : 0;}
m2130 0:d10107920170 74
m2130 0:d10107920170 75 bool sbdbt::right(){return data[2] & RIGHT ? 1 : 0;}
m2130 0:d10107920170 76
m2130 0:d10107920170 77 bool sbdbt::tri(){return data[2] & TRI ? 1 : 0;}
m2130 0:d10107920170 78
m2130 0:d10107920170 79 bool sbdbt::cross(){return data[2] & CROSS ? 1 : 0;}
m2130 0:d10107920170 80
m2130 0:d10107920170 81 bool sbdbt::square(){return data[2] & SQUARE ? 1 : 0;}
m2130 0:d10107920170 82
m2130 0:d10107920170 83 bool sbdbt::circle(){return data[1] & CIRCLE ? 1 : 0;}
m2130 0:d10107920170 84
m2130 0:d10107920170 85 bool sbdbt::l1(){return data[1] & L1 ? 1 : 0;}
m2130 0:d10107920170 86
m2130 0:d10107920170 87 bool sbdbt::l2(){return data[1] & L2 ? 1 : 0;}
m2130 0:d10107920170 88
m2130 0:d10107920170 89 bool sbdbt::r1(){return data[1] & R1 ? 1 : 0;}
m2130 0:d10107920170 90
m2130 0:d10107920170 91 bool sbdbt::r2(){return data[1] & R2 ? 1 : 0;}
m2130 0:d10107920170 92
m2130 0:d10107920170 93 bool sbdbt::start(){return data[1] & START ? 1 : 0;}
m2130 0:d10107920170 94
m2130 0:d10107920170 95 bool sbdbt::select(){return data[1] & SELECT ? 1 : 0;}
m2130 0:d10107920170 96
m2130 0:d10107920170 97 char sbdbt::lsx(){return data[3];}
m2130 0:d10107920170 98
m2130 0:d10107920170 99 char sbdbt::lsy(){return data[4];}
m2130 0:d10107920170 100
m2130 0:d10107920170 101 char sbdbt::rsx(){return data[5];}
m2130 0:d10107920170 102
m2130 0:d10107920170 103 char sbdbt::rsy(){return data[6];}
m2130 0:d10107920170 104
m2130 0:d10107920170 105 char sbdbt::l2An(){return data[7];}
m2130 0:d10107920170 106
m2130 0:d10107920170 107 char sbdbt::r2An(){return data[8];}
m2130 0:d10107920170 108
m2130 0:d10107920170 109 sbdbt::~sbdbt(){
m2130 0:d10107920170 110 delete dev;
m2130 0:d10107920170 111 delete pc;
m2130 0:d10107920170 112 delete data;
m2130 0:d10107920170 113 delete PS3_Data;
m2130 0:d10107920170 114 }