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
- Committer:
- sswatek
- Date:
- 2014-03-12
- Revision:
- 22:ac1f69a841ce
- Parent:
- 19:3d77dc964d4b
- Parent:
- 20:5b8fde077301
- Child:
- 23:53cafcd67828
File content as of revision 22:ac1f69a841ce:
#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;
//commits
// fffffffffuuuuuuuu
//int b = LPC_UART2->RBR;
//led3=1;
//with
//int c = LPC_UART3->RBR;
//messing
// hello world
// hi
//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;
}
