a

Fork of kbtakira by 100% akira

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers kbt.cpp Source File

kbt.cpp

00001 
00002 #include "kbt.h"
00003 #include "mbed.h"
00004 
00005 
00006 long KBT::map(long x, long in_min, long in_max, long out_min, long out_max){
00007   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00008 }
00009 
00010 KBT::KBT(PinName TX, PinName RX) : bt(TX,RX) {
00011 }
00012 
00013 void KBT::init(long baudrate){
00014     numinit();
00015     bt.baud(baudrate);
00016     bt.attach(this, &KBT::intSerial, Serial::RxIrq);
00017 }
00018 
00019 void KBT::numinit(){
00020     start = 0;
00021     stopcheck = 0;
00022     val = 0; 
00023     count = 0;
00024     bitbox[0] = data[0] = open_data[0] = 0x80;
00025     bitbox[1] = data[1] = open_data[1] = 0x00;
00026     bitbox[2] = data[2] = open_data[2] = 0x00;
00027     bitbox[3] = data[3] = open_data[3] = 0x40;
00028     bitbox[4] = data[4] = open_data[4] = 0x40;
00029     bitbox[5] = data[5] = open_data[5] = 0x40;
00030     bitbox[6] = data[6] = open_data[6] = 0x40;
00031     bitbox[7] = data[7] = open_data[7] = 0x00;
00032     check[0] = 0x01;
00033     check[1] = 0x02; 
00034     check[2] = 0x04;
00035     check[3] = 0x08;
00036     check[4] = 0x10;
00037     check[5] = 0x20;
00038     check[6] = 0x40;
00039     check[7] = 0x03;
00040     check[8] = 0x0C;
00041 }
00042 
00043 
00044 bool KBT::button(int num){
00045     return Button[num];
00046 }
00047 
00048 int KBT::stick(int num){
00049     return Stick[num - 14];
00050 }
00051 
00052 void KBT::bitcheck(){
00053     for(int i=0;i<5;i++){
00054         if(bitbox[1] == check[i]){
00055             Button[i] = true;
00056         }else{
00057             Button[i] = 0;
00058         }
00059     }
00060     for(int i=0;i<9;i++){
00061         if(bitbox[2] == check[i]){
00062             Button[i+5] = true; 
00063         }else{
00064             Button[i+5] = 0;
00065         }
00066     }
00067     if(bitbox[3] == 0x40){
00068         Stick[0] = 0;
00069     }else if(bitbox[3] < 0x40){
00070         Stick[0] = map(bitbox[3], 65, 0, 0, 255);
00071     }else if(bitbox[3] > 0x40){
00072         Stick[0] = map(bitbox[3], 63, 127, 0, -255);
00073     }
00074     
00075     if(bitbox[4] == 0x40){
00076         Stick[1] = 0;
00077     }else if(bitbox[4] < 0x40){
00078         Stick[1] = map(bitbox[4], 63, 127, 0, -255);
00079     }else if(bitbox[4] > 0x40){
00080         Stick[1] = map(bitbox[4], 65, 0, 0, 255);
00081     }
00082 
00083     if(bitbox[5] == 0x40){
00084         Stick[2] = 0;
00085     }else if(bitbox[5] < 0x40){
00086         Stick[2] = map(bitbox[5], 65, 0, 0, -255);
00087     }else if(bitbox[5] > 0x40){
00088         Stick[2] = map(bitbox[5], 63, 127, 0, 255);
00089     }
00090 
00091     if(bitbox[6] == 0x40){
00092         Stick[3] = 0;
00093     }else if(bitbox[6] < 0x40){
00094         Stick[3] = map(bitbox[6], 65, 0, 0, 255);
00095     }else if(bitbox[6] > 0x40){
00096         Stick[3] = map(bitbox[6], 63, 127, 0, -255);
00097     }
00098 }
00099 
00100 void KBT::intSerial(){
00101     val = bt.getc();
00102     if(val == 0x80){
00103         bitbox[0] = val;
00104         start = 1;
00105         count = 1;
00106         stopcheck = 0;
00107     }else{
00108         if(start == 1){
00109             bitbox[count] = val;
00110             count++;
00111         }
00112         if(count >= BITNUM){
00113             for(int i=1;i<DATANUM;i++){
00114                 stopcheck += bitbox[i];
00115             }
00116             if((stopcheck & 0b01111111) == bitbox[DATANUM]){
00117                 for(int i=0;i<BITNUM;i++){
00118                     data[i] = bitbox[i];
00119                     open_data[i] = data[i];
00120                 }
00121             }
00122             bitcheck();
00123             count=0;
00124             start=0;
00125         }
00126     }
00127 }