100% akira / kbtakira2

Fork of kbtakira by 100% akira

Files at this revision

API Documentation at this revision

Comitter:
kenken0721
Date:
Tue Jan 16 03:44:40 2018 +0000
Child:
1:c4fad24c3c2f
Commit message:
sbdbt?ps3??

Changed in this revision

kbt.cpp Show annotated file Show diff for this revision Revisions of this file
kbt.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kbt.cpp	Tue Jan 16 03:44:40 2018 +0000
@@ -0,0 +1,129 @@
+
+#include "kbt.h"
+#include "mbed.h"
+
+
+long KBT::map(long x, long in_min, long in_max, long out_min, long out_max){
+  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+KBT::KBT(PinName TX, PinName RX) : bt(TX,RX) {
+}
+
+void KBT::init(long baudrate){
+    numinit();
+    bt.baud(baudrate);
+    bt.attach(this, &KBT::intSerial, Serial::RxIrq);
+}
+
+void KBT::numinit(){
+    start = 0;
+    stopcheck = 0;
+    val = 0; 
+    count = 0;
+    bitbox[0] = data[0] = open_data[0] = 0x80;
+    bitbox[1] = data[1] = open_data[1] = 0x00;
+    bitbox[2] = data[2] = open_data[2] = 0x00;
+    bitbox[3] = data[3] = open_data[3] = 0x40;
+    bitbox[4] = data[4] = open_data[4] = 0x40;
+    bitbox[5] = data[5] = open_data[5] = 0x40;
+    bitbox[6] = data[6] = open_data[6] = 0x40;
+    bitbox[7] = data[7] = open_data[7] = 0x00;
+    check[0] = 0x01;
+    check[1] = 0x02; 
+    check[2] = 0x04;
+    check[3] = 0x08;
+    check[4] = 0x10;
+    check[5] = 0x20;
+    check[6] = 0x40;
+    check[7] = 0x03;
+    check[8] = 0x0C;
+}
+
+
+bool KBT::button(int num){
+    return Button[num];
+}
+
+int KBT::stick(int num){
+    return Stick[num];
+}
+
+void KBT::bitcheck(){
+    for(int i=0;i<5;i++){
+        if(bitbox[1] == check[i]){
+            Button[i] = true;
+        }else{
+            Button[i] = 0;
+        }
+    }
+    for(int i=0;i<9;i++){
+        if(bitbox[2] == check[i]){
+            Button[i+5] = true; 
+        }else{
+            Button[i+5] = 0;
+        }
+    }
+    if(bitbox[3] == 0x40){
+        Stick[0] = 0;
+    }else if(bitbox[3] < 0x40){
+        Stick[0] = map(bitbox[3], 65, 0, 0, 255);
+    }else if(bitbox[3] > 0x40){
+        Stick[0] = map(bitbox[3], 63, 127, 0, -255);
+    }
+    
+    if(bitbox[4] == 0x40){
+        Stick[1] = 0;
+    }else if(bitbox[4] < 0x40){
+        Stick[1] = map(bitbox[4], 63, 127, 0, -255);
+    }else if(bitbox[4] > 0x40){
+        Stick[1] = map(bitbox[4], 65, 0, 0, 255);
+    }
+
+    if(bitbox[5] == 0x40){
+        Stick[2] = 0;
+    }else if(bitbox[5] < 0x40){
+        Stick[2] = map(bitbox[5], 65, 0, 0, -255);
+    }else if(bitbox[5] > 0x40){
+        Stick[2] = map(bitbox[5], 63, 127, 0, 255);
+    }
+
+    if(bitbox[6] == 0x40){
+        Stick[3] = 0;
+    }else if(bitbox[6] < 0x40){
+        Stick[3] = map(bitbox[6], 65, 0, 0, 255);
+    }else if(bitbox[6] > 0x40){
+        Stick[3] = map(bitbox[6], 63, 127, 0, -255);
+    }
+}
+
+void KBT::intSerial(){
+    val = bt.getc();
+    //スタートビット処理
+    if(val == 0x80){
+        bitbox[0] = val;
+        start = 1;
+        count = 1;
+        stopcheck = 0;
+    }else{
+        if(start == 1){
+            bitbox[count] = val;
+            count++;
+        }
+        if(count >= BITNUM){
+            for(int i=1;i<DATANUM;i++){
+                stopcheck += bitbox[i];
+            }
+            //ストップビット処理
+            if((stopcheck & 0b01111111) == bitbox[DATANUM]){
+                for(int i=0;i<BITNUM;i++){
+                    data[i] = bitbox[i];
+                    open_data[i] = data[i];
+                }
+            }
+            bitcheck();
+            count=0;
+            start=0;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kbt.h	Tue Jan 16 03:44:40 2018 +0000
@@ -0,0 +1,58 @@
+
+#ifndef KBT_H
+#define KBT_H
+
+#include "mbed.h"
+
+#define BITNUM 8
+#define DATANUM 7
+
+#define rect 0
+#define L1 1
+#define L2 2
+#define R1 3
+#define R2 4
+#define up 5
+#define down 6
+#define right 7
+#define left 8
+#define triangle 9
+#define cross 10
+#define circle 11
+#define Start 12
+#define Select 13
+#define L_around 14
+#define L_updown 15
+#define R_around 16
+#define R_updown 17
+
+class KBT{
+public  :
+    bool Button[14];
+    int Stick[4];
+    int open_data[8];
+    
+    KBT(PinName TX, PinName RX);
+    void init(long baudrate);
+    bool button(int num);
+    int stick(int num);
+
+private :
+    Serial bt;
+    bool start;
+    int stopcheck;
+    int val;  
+    int count;
+    int bitbox[8];
+    int data[8];
+    int check[9];
+
+    long map(long x, long in_min, long in_max, long out_min, long out_max);
+    void bitcheck();
+    void intSerial();
+    void numinit();
+
+};
+
+#endif
+