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

Dependents:   PIDcontrol_4 go_robo2022kaisyu_no 2022_Ateam_MOTORprogram tape_LED ... more

Revision:
0:d10107920170
Child:
2:9fd7393a3023
diff -r 000000000000 -r d10107920170 sbdbt.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sbdbt.cpp	Wed Nov 10 13:53:15 2021 +0000
@@ -0,0 +1,114 @@
+#include "sbdbt.hpp"
+#include "mbed.h"
+
+sbdbt::sbdbt(PinName TX, PinName RX){
+    i = 0;
+    pos = 0;
+    val = 0x80;
+    len = 10;
+    data = new char[len];
+    PS3_Data = new char[len];
+    pc = new Serial(USBTX, USBRX);
+    dev = new Serial(TX, RX);
+    dev -> baud(9600);
+    dev -> attach(this,&sbdbt::get,Serial::RxIrq);
+}
+
+sbdbt::sbdbt(PinName TX, PinName RX, int baud){
+    i = 0;
+    pos = 0;
+    val = 0x80;
+    len = 10;
+    data = new char[len];
+    PS3_Data = new char[len];
+    pc = new Serial(USBTX, USBRX);
+    dev = new Serial(TX, RX);
+    dev -> baud(9600);
+    pc -> baud(baud);
+    dev -> attach(this,&sbdbt::get,Serial::RxIrq);
+}
+
+void sbdbt::get(){
+    data[i] = dev -> getc();
+    if(data[i] == val){
+        if(i != pos){
+            data[pos] = val;
+            i = pos + 1;
+        }
+        else{
+            i++;
+        }
+    }
+    else{
+        if(i == pos);
+        else{
+            i++;
+        }
+    }
+    if(i >= len){
+        PS3_Data[0] = data[0];
+        PS3_Data[1] = data[1];
+        PS3_Data[2] = data[2];
+        PS3_Data[3] = data[3];
+        PS3_Data[4] = data[4];
+        PS3_Data[5] = data[5];
+        PS3_Data[6] = data[6];
+        PS3_Data[7] = data[7];
+        PS3_Data[8] = data[8];
+        PS3_Data[9] = data[9];
+        i = 0;
+    }
+}
+
+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());}
+
+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]);}
+
+bool sbdbt::neutral(){return data[9] == 0 ? 1 : 0;}
+
+bool sbdbt::up(){return data[2] & UP ? 1 : 0;}
+
+bool sbdbt::down(){return data[2] & DOWN ? 1 : 0;}
+
+bool sbdbt::left(){return data[2] & LEFT ? 1 : 0;}
+
+bool sbdbt::right(){return data[2] & RIGHT ? 1 : 0;}
+
+bool sbdbt::tri(){return data[2] & TRI ? 1 : 0;}
+
+bool sbdbt::cross(){return data[2] & CROSS ? 1 : 0;}
+
+bool sbdbt::square(){return data[2] & SQUARE ? 1 : 0;}
+
+bool sbdbt::circle(){return data[1] & CIRCLE ? 1 : 0;}
+
+bool sbdbt::l1(){return data[1] & L1 ? 1 : 0;}
+
+bool sbdbt::l2(){return data[1] & L2 ? 1 : 0;}
+
+bool sbdbt::r1(){return data[1] & R1 ? 1 : 0;}
+
+bool sbdbt::r2(){return data[1] & R2 ? 1 : 0;}
+
+bool sbdbt::start(){return data[1] & START ? 1 : 0;}
+
+bool sbdbt::select(){return data[1] & SELECT ? 1 : 0;}
+
+char sbdbt::lsx(){return data[3];}
+
+char sbdbt::lsy(){return data[4];}
+
+char sbdbt::rsx(){return data[5];}
+
+char sbdbt::rsy(){return data[6];}
+
+char sbdbt::l2An(){return data[7];}
+
+char sbdbt::r2An(){return data[8];}
+
+sbdbt::~sbdbt(){
+    delete dev;
+    delete pc;
+    delete data;
+    delete PS3_Data;
+}
\ No newline at end of file