Sooner Competitive Robotics / Mbed 2 deprecated IEEE_14_Freescale

Dependencies:   mbed

Fork of IEEE_14_Freescale by IEEE 2014 Mbed

Revision:
16:6d900f687c18
Child:
17:e247d58d9f42
diff -r b10859606504 -r 6d900f687c18 droidBT.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/droidBT.cpp	Tue Mar 11 20:02:56 2014 +0000
@@ -0,0 +1,144 @@
+#include "droidBT.h"
+#include "dbgprint.h"
+
+droidBT::droidBT(PinName tx, PinName rx) : droid(tx,rx){
+    BTStart=0;
+    BTEnd=0;
+    DCStart=0;
+    DCEnd=0;
+    droid.baud(115200);
+    droid.attach(this,&droidBT::BTEnqueue);
+}
+
+void droidBT::BTEnqueue(){
+    //while(droid.readable()){
+        //led1=1;
+        BTBuffer[BTEnd]=droid.getc();
+        //int a = LPC_UART1->RBR;
+        //led2=1;
+        //int b = LPC_UART2->RBR;
+        //led3=1;
+        //int c = LPC_UART3->RBR;
+        //led4=1;
+        //DBGPRINT("Got %x\n", BTBuffer[BTEnd]);
+        BTEnd++;
+        BTEnd%=BTBUFSIZE;
+        //led1=0;
+    //}
+}
+
+int droidBT::clearData(){
+    DCStart=DCEnd=0;
+    return 1;
+}
+
+int droidBT::getData(int cmd, int* dest){
+    procBuf(1);
+    int i;
+    for(i=DCStart;i!=DCEnd;i=(i+1)%DCHUNKSIZE){
+        if((DataChunk[i][0]&0xF0)==0x60 && DataChunk[i][1]==cmd){
+            DataChunk[i][0]=0;
+            if(i==DCStart){
+                DCStart++;
+            }
+            for(int j=0;j<17;j++)
+                dest[j]=DataChunk[i][j];
+            return 1;
+        }
+    }
+    return 0;
+}
+
+int droidBT::procBuf(int cmd){
+    int i=BTStart;
+    int startChunk=i;
+    int seen=0;
+    while (i!=BTEnd) {
+        if(i==startChunk){ //first bit in the chunk
+            if((BTBuffer[i]&0xF0)==0x60){//data returned
+                i++;
+                DBGPRINT("gettin data\r\n",1);
+            } else if((BTBuffer[i]&0xF0)==0xC0){//ack
+                i++;
+                DBGPRINT("gettin ack\r\n",1);
+            } else {//dunno what this data is, toss
+                BTStart++;
+                i++;
+                startChunk=i;
+                DBGPRINT("throwing away\r\n",1);
+            }
+        } else {
+            DBGPRINT("not first\r\n",1);
+            if(bufSize()>=(BTBuffer[startChunk]&0xF)+1){
+                DBGPRINT("got enough space %x, %d\r\n",BTBuffer[startChunk]&0xF, DCEnd);
+                DataChunk[DCEnd][0]=BTBuffer[startChunk];
+                int j;
+                for(j=0;j<(BTBuffer[startChunk]&0xF);j++){
+                    DBGPRINT("copying %d, %d\r\n",BTBuffer[i+j], j);
+                    DataChunk[DCEnd][j+1]=BTBuffer[i+j];
+                }
+                if(DCEnd>=DCHUNKSIZE)
+                    DCEnd=0;
+                DBGPRINT("Got Data of size %d [%x %d %d]\n", BTBuffer[startChunk]&0xF, DataChunk[DCEnd][1], DataChunk[DCEnd][2], DataChunk[DCEnd][3]);
+                DCEnd++;
+                BTStart=i+j;
+                startChunk=i=BTStart;
+            } else {
+                break;
+            }
+        }
+                
+    }
+    return seen;
+}
+int droidBT::bufSize(){
+    return (BTEnd+BTBUFSIZE-BTStart)%BTBUFSIZE;
+}
+
+//returns both bits in an int form to look for
+int droidBT::setState(char State) {
+    int sendbuf[10];
+    sendbuf[0]=0xF3;
+    sendbuf[1]=State;
+    sendbuf[2]=0x11;
+    sendbuf[3]=0x44;
+    int i, cheapHash=0;
+    droid.putc(sendbuf[0]);
+    for(i=1;i<4;i++){
+        droid.putc(sendbuf[i]);
+        cheapHash^=sendbuf[i];
+    }
+    return 0xF300|cheapHash;
+}
+
+int droidBT::sendCmd(int cmd, int* info, int len) {
+    int sendbuf[10];
+    sendbuf[0]=(0xF0|(len+1));
+    sendbuf[1]=cmd;
+    sendbuf[2]=0x11;
+    sendbuf[3]=0x44;
+    int i, cheapHash=cmd;
+    droid.putc((0xF0|(len+1)));
+    droid.putc(cmd);
+    for(i=0;i<len;i++){
+        droid.putc(info[i]);
+        cheapHash^=info[i];
+    }
+    return 0xF000|((len+1)<<2)|cheapHash;
+}
+
+int droidBT::getAck(int hashetc){
+    int firstbit=(hashetc&0xFF00)>>8;
+    int secondbit=hashetc&0xFF;
+    int i;
+    if(BTStart==BTEnd) return 0;
+    for(i=BTStart;i!=BTEnd-1;i=(i+1)%BTBUFSIZE){
+        if(BTBuffer[i]==firstbit && BTBuffer[i+1]==secondbit){
+            BTBuffer[i]=BTBuffer[i+1]=0;
+            if(i==BTStart)
+                BTStart+=2;
+            return 1;
+        }
+    }
+    return 0;
+}
\ No newline at end of file