Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of IEEE_14_Freescale by
droidBT.cpp@23:53cafcd67828, 2014-03-13 (annotated)
- Committer:
- sswatek
- Date:
- Thu Mar 13 04:30:24 2014 +0000
- Revision:
- 23:53cafcd67828
- Parent:
- 22:ac1f69a841ce
- Child:
- 24:46eeab7cebc6
Reliably send messages to the phone
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| soonerbot | 16:6d900f687c18 | 1 | #include "droidBT.h" |
| soonerbot | 16:6d900f687c18 | 2 | #include "dbgprint.h" |
| soonerbot | 16:6d900f687c18 | 3 | |
| soonerbot | 16:6d900f687c18 | 4 | droidBT::droidBT(PinName tx, PinName rx) : droid(tx,rx){ |
| soonerbot | 16:6d900f687c18 | 5 | BTStart=0; |
| soonerbot | 16:6d900f687c18 | 6 | BTEnd=0; |
| soonerbot | 16:6d900f687c18 | 7 | DCStart=0; |
| soonerbot | 16:6d900f687c18 | 8 | DCEnd=0; |
| soonerbot | 16:6d900f687c18 | 9 | droid.baud(115200); |
| soonerbot | 16:6d900f687c18 | 10 | droid.attach(this,&droidBT::BTEnqueue); |
| soonerbot | 16:6d900f687c18 | 11 | } |
| soonerbot | 16:6d900f687c18 | 12 | |
| soonerbot | 16:6d900f687c18 | 13 | void droidBT::BTEnqueue(){ |
| soonerbot | 16:6d900f687c18 | 14 | //while(droid.readable()){ |
| soonerbot | 16:6d900f687c18 | 15 | //led1=1; |
| soonerbot | 16:6d900f687c18 | 16 | BTBuffer[BTEnd]=droid.getc(); |
| soonerbot | 16:6d900f687c18 | 17 | //int a = LPC_UART1->RBR; |
| soonerbot | 16:6d900f687c18 | 18 | //led2=1; |
| sswatek | 19:3d77dc964d4b | 19 | //commits |
| rmutalik | 20:5b8fde077301 | 20 | // fffffffffuuuuuuuu |
| soonerbot | 16:6d900f687c18 | 21 | //int b = LPC_UART2->RBR; |
| soonerbot | 16:6d900f687c18 | 22 | //led3=1; |
| sswatek | 19:3d77dc964d4b | 23 | //with |
| soonerbot | 16:6d900f687c18 | 24 | //int c = LPC_UART3->RBR; |
| sswatek | 19:3d77dc964d4b | 25 | //messing |
| rmutalik | 20:5b8fde077301 | 26 | // hello world |
| rmutalik | 20:5b8fde077301 | 27 | // hi |
| soonerbot | 16:6d900f687c18 | 28 | //led4=1; |
| soonerbot | 16:6d900f687c18 | 29 | //DBGPRINT("Got %x\n", BTBuffer[BTEnd]); |
| soonerbot | 16:6d900f687c18 | 30 | BTEnd++; |
| soonerbot | 16:6d900f687c18 | 31 | BTEnd%=BTBUFSIZE; |
| soonerbot | 16:6d900f687c18 | 32 | //led1=0; |
| soonerbot | 16:6d900f687c18 | 33 | //} |
| soonerbot | 16:6d900f687c18 | 34 | } |
| soonerbot | 16:6d900f687c18 | 35 | |
| soonerbot | 16:6d900f687c18 | 36 | int droidBT::clearData(){ |
| soonerbot | 16:6d900f687c18 | 37 | DCStart=DCEnd=0; |
| soonerbot | 16:6d900f687c18 | 38 | return 1; |
| soonerbot | 16:6d900f687c18 | 39 | } |
| soonerbot | 16:6d900f687c18 | 40 | |
| soonerbot | 16:6d900f687c18 | 41 | int droidBT::getData(int cmd, int* dest){ |
| soonerbot | 16:6d900f687c18 | 42 | procBuf(1); |
| soonerbot | 16:6d900f687c18 | 43 | int i; |
| soonerbot | 16:6d900f687c18 | 44 | for(i=DCStart;i!=DCEnd;i=(i+1)%DCHUNKSIZE){ |
| soonerbot | 16:6d900f687c18 | 45 | if((DataChunk[i][0]&0xF0)==0x60 && DataChunk[i][1]==cmd){ |
| soonerbot | 16:6d900f687c18 | 46 | DataChunk[i][0]=0; |
| soonerbot | 16:6d900f687c18 | 47 | if(i==DCStart){ |
| soonerbot | 16:6d900f687c18 | 48 | DCStart++; |
| soonerbot | 16:6d900f687c18 | 49 | } |
| soonerbot | 16:6d900f687c18 | 50 | for(int j=0;j<17;j++) |
| soonerbot | 16:6d900f687c18 | 51 | dest[j]=DataChunk[i][j]; |
| soonerbot | 16:6d900f687c18 | 52 | return 1; |
| soonerbot | 16:6d900f687c18 | 53 | } |
| soonerbot | 16:6d900f687c18 | 54 | } |
| soonerbot | 16:6d900f687c18 | 55 | return 0; |
| soonerbot | 16:6d900f687c18 | 56 | } |
| soonerbot | 16:6d900f687c18 | 57 | |
| soonerbot | 16:6d900f687c18 | 58 | int droidBT::procBuf(int cmd){ |
| soonerbot | 16:6d900f687c18 | 59 | int i=BTStart; |
| soonerbot | 16:6d900f687c18 | 60 | int startChunk=i; |
| soonerbot | 16:6d900f687c18 | 61 | int seen=0; |
| sswatek | 23:53cafcd67828 | 62 | int cheapHash=0; |
| soonerbot | 16:6d900f687c18 | 63 | while (i!=BTEnd) { |
| soonerbot | 16:6d900f687c18 | 64 | if(i==startChunk){ //first bit in the chunk |
| soonerbot | 16:6d900f687c18 | 65 | if((BTBuffer[i]&0xF0)==0x60){//data returned |
| sswatek | 23:53cafcd67828 | 66 | cheapHash=BTBuffer[i]; |
| soonerbot | 16:6d900f687c18 | 67 | DBGPRINT("gettin data\r\n",1); |
| soonerbot | 16:6d900f687c18 | 68 | i++; |
| sswatek | 23:53cafcd67828 | 69 | } else if((BTBuffer[i]&0xF0)==0xF0){//ack |
| sswatek | 23:53cafcd67828 | 70 | cheapHash=BTBuffer[i]; |
| sswatek | 23:53cafcd67828 | 71 | DBGPRINT("gettin ack %x\r\n",BTBuffer[i]); |
| sswatek | 23:53cafcd67828 | 72 | BTBuffer[i]=0xF1; |
| sswatek | 23:53cafcd67828 | 73 | i++; |
| soonerbot | 16:6d900f687c18 | 74 | } else {//dunno what this data is, toss |
| soonerbot | 16:6d900f687c18 | 75 | BTStart++; |
| sswatek | 23:53cafcd67828 | 76 | startChunk=i; |
| sswatek | 23:53cafcd67828 | 77 | DBGPRINT("throwing away %x\r\n",BTBuffer[i]); |
| soonerbot | 16:6d900f687c18 | 78 | i++; |
| soonerbot | 16:6d900f687c18 | 79 | } |
| soonerbot | 16:6d900f687c18 | 80 | } else { |
| soonerbot | 16:6d900f687c18 | 81 | DBGPRINT("not first\r\n",1); |
| soonerbot | 16:6d900f687c18 | 82 | if(bufSize()>=(BTBuffer[startChunk]&0xF)+1){ |
| soonerbot | 16:6d900f687c18 | 83 | DBGPRINT("got enough space %x, %d\r\n",BTBuffer[startChunk]&0xF, DCEnd); |
| soonerbot | 16:6d900f687c18 | 84 | DataChunk[DCEnd][0]=BTBuffer[startChunk]; |
| soonerbot | 16:6d900f687c18 | 85 | int j; |
| soonerbot | 16:6d900f687c18 | 86 | for(j=0;j<(BTBuffer[startChunk]&0xF);j++){ |
| soonerbot | 16:6d900f687c18 | 87 | DBGPRINT("copying %d, %d\r\n",BTBuffer[i+j], j); |
| soonerbot | 16:6d900f687c18 | 88 | DataChunk[DCEnd][j+1]=BTBuffer[i+j]; |
| sswatek | 23:53cafcd67828 | 89 | if(j<((BTBuffer[startChunk]&0xF)-1)){ |
| sswatek | 23:53cafcd67828 | 90 | cheapHash^=BTBuffer[i+j]; |
| sswatek | 23:53cafcd67828 | 91 | } |
| soonerbot | 16:6d900f687c18 | 92 | } |
| sswatek | 23:53cafcd67828 | 93 | //check and store if the hash matched |
| sswatek | 23:53cafcd67828 | 94 | if(cheapHash == BTBuffer[i+(BTBuffer[startChunk]&0xF)-1]){ |
| sswatek | 23:53cafcd67828 | 95 | DBGPRINT("Hash Matches\r\n",1); |
| sswatek | 23:53cafcd67828 | 96 | DataChunk[DCEnd][(BTBuffer[startChunk]&0xF)]=1; |
| sswatek | 23:53cafcd67828 | 97 | if(DCEnd>=DCHUNKSIZE) |
| sswatek | 23:53cafcd67828 | 98 | DCEnd=0; |
| sswatek | 23:53cafcd67828 | 99 | DCEnd++; |
| sswatek | 23:53cafcd67828 | 100 | }else{ |
| sswatek | 23:53cafcd67828 | 101 | DBGPRINT("Hash Does Not Match\r\n",1); |
| sswatek | 23:53cafcd67828 | 102 | DataChunk[DCEnd][(BTBuffer[startChunk]&0xF)]=0; |
| sswatek | 23:53cafcd67828 | 103 | } |
| soonerbot | 16:6d900f687c18 | 104 | DBGPRINT("Got Data of size %d [%x %d %d]\n", BTBuffer[startChunk]&0xF, DataChunk[DCEnd][1], DataChunk[DCEnd][2], DataChunk[DCEnd][3]); |
| sswatek | 23:53cafcd67828 | 105 | |
| soonerbot | 16:6d900f687c18 | 106 | BTStart=i+j; |
| soonerbot | 16:6d900f687c18 | 107 | startChunk=i=BTStart; |
| soonerbot | 16:6d900f687c18 | 108 | } else { |
| soonerbot | 16:6d900f687c18 | 109 | break; |
| soonerbot | 16:6d900f687c18 | 110 | } |
| soonerbot | 16:6d900f687c18 | 111 | } |
| soonerbot | 16:6d900f687c18 | 112 | |
| soonerbot | 16:6d900f687c18 | 113 | } |
| soonerbot | 16:6d900f687c18 | 114 | return seen; |
| soonerbot | 16:6d900f687c18 | 115 | } |
| soonerbot | 16:6d900f687c18 | 116 | int droidBT::bufSize(){ |
| soonerbot | 16:6d900f687c18 | 117 | return (BTEnd+BTBUFSIZE-BTStart)%BTBUFSIZE; |
| soonerbot | 16:6d900f687c18 | 118 | } |
| soonerbot | 16:6d900f687c18 | 119 | |
| soonerbot | 16:6d900f687c18 | 120 | //returns both bits in an int form to look for |
| soonerbot | 16:6d900f687c18 | 121 | int droidBT::setState(char State) { |
| soonerbot | 16:6d900f687c18 | 122 | int sendbuf[10]; |
| soonerbot | 16:6d900f687c18 | 123 | sendbuf[0]=0xF3; |
| soonerbot | 16:6d900f687c18 | 124 | sendbuf[1]=State; |
| soonerbot | 16:6d900f687c18 | 125 | sendbuf[2]=0x11; |
| soonerbot | 16:6d900f687c18 | 126 | sendbuf[3]=0x44; |
| soonerbot | 16:6d900f687c18 | 127 | int i, cheapHash=0; |
| soonerbot | 16:6d900f687c18 | 128 | droid.putc(sendbuf[0]); |
| soonerbot | 16:6d900f687c18 | 129 | for(i=1;i<4;i++){ |
| soonerbot | 16:6d900f687c18 | 130 | droid.putc(sendbuf[i]); |
| soonerbot | 16:6d900f687c18 | 131 | cheapHash^=sendbuf[i]; |
| soonerbot | 16:6d900f687c18 | 132 | } |
| soonerbot | 16:6d900f687c18 | 133 | return 0xF300|cheapHash; |
| soonerbot | 16:6d900f687c18 | 134 | } |
| soonerbot | 16:6d900f687c18 | 135 | |
| soonerbot | 16:6d900f687c18 | 136 | int droidBT::sendCmd(int cmd, int* info, int len) { |
| sswatek | 17:e247d58d9f42 | 137 | //int sendbuf[10]; |
| sswatek | 17:e247d58d9f42 | 138 | //sendbuf[0]=(0xF0|(len+1)); |
| sswatek | 17:e247d58d9f42 | 139 | //sendbuf[1]=cmd; |
| sswatek | 17:e247d58d9f42 | 140 | //sendbuf[2]=0x11; |
| sswatek | 17:e247d58d9f42 | 141 | //sendbuf[3]=0x44; |
| soonerbot | 16:6d900f687c18 | 142 | int i, cheapHash=cmd; |
| soonerbot | 16:6d900f687c18 | 143 | droid.putc((0xF0|(len+1))); |
| soonerbot | 16:6d900f687c18 | 144 | droid.putc(cmd); |
| soonerbot | 16:6d900f687c18 | 145 | for(i=0;i<len;i++){ |
| soonerbot | 16:6d900f687c18 | 146 | droid.putc(info[i]); |
| soonerbot | 16:6d900f687c18 | 147 | cheapHash^=info[i]; |
| soonerbot | 16:6d900f687c18 | 148 | } |
| sswatek | 23:53cafcd67828 | 149 | return 0xF000|((len+1)<<8)|(cheapHash&0xFF); |
| soonerbot | 16:6d900f687c18 | 150 | } |
| soonerbot | 16:6d900f687c18 | 151 | |
| soonerbot | 16:6d900f687c18 | 152 | int droidBT::getAck(int hashetc){ |
| soonerbot | 16:6d900f687c18 | 153 | int firstbit=(hashetc&0xFF00)>>8; |
| soonerbot | 16:6d900f687c18 | 154 | int secondbit=hashetc&0xFF; |
| soonerbot | 16:6d900f687c18 | 155 | int i; |
| soonerbot | 16:6d900f687c18 | 156 | if(BTStart==BTEnd) return 0; |
| soonerbot | 16:6d900f687c18 | 157 | for(i=BTStart;i!=BTEnd-1;i=(i+1)%BTBUFSIZE){ |
| soonerbot | 16:6d900f687c18 | 158 | if(BTBuffer[i]==firstbit && BTBuffer[i+1]==secondbit){ |
| soonerbot | 16:6d900f687c18 | 159 | BTBuffer[i]=BTBuffer[i+1]=0; |
| soonerbot | 16:6d900f687c18 | 160 | if(i==BTStart) |
| soonerbot | 16:6d900f687c18 | 161 | BTStart+=2; |
| soonerbot | 16:6d900f687c18 | 162 | return 1; |
| soonerbot | 16:6d900f687c18 | 163 | } |
| soonerbot | 16:6d900f687c18 | 164 | } |
| soonerbot | 16:6d900f687c18 | 165 | return 0; |
| soonerbot | 16:6d900f687c18 | 166 | } |
