ll

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bluetooth.h Source File

bluetooth.h

00001 Serial bluetooth(D10, D2); // tx,rx
00002 
00003 
00004 void DataDecoder();
00005 bool BluetoothDataValid;
00006 unsigned char BluetoothDataReceived[23];
00007 unsigned char ValidData[21];
00008 int ReadingAscii;
00009 int DataStartPoint;
00010 bool ReadStart;
00011 int ReceivingAscii;
00012 int CountsOf_0xff;
00013 bool StartReceiving;
00014 bool DataValidorNot = false;
00015 
00016 
00017 ///////////////////////////
00018 float Car1X ;
00019 float Car1Y; 
00020 float Car2X ;
00021 float Car2Y ;
00022 float BlueBallX ;
00023 float BlueBallY ;
00024 float GreenBallX ;
00025 float GreenBallY ;
00026 float Car1DirectionInRad;
00027 float Car2DirectionInRad;
00028 unsigned char BluetoothCommand ;
00029 ///////////////////////////////
00030 
00031 void Bluetooth_Init()
00032 {
00033     
00034     bluetooth.baud(9600);
00035     BluetoothDataValid = false;
00036     ReceivingAscii = 0;
00037     ReadStart = false;
00038     CountsOf_0xff = 0;
00039     StartReceiving = false;
00040 }
00041 
00042 void GetBluetoothData()
00043 {
00044     
00045     if(bluetooth.readable())
00046     {
00047         if(StartReceiving == false)
00048         {
00049             unsigned char temp = bluetooth.getc();
00050             if(temp == 0xFF) 
00051             {
00052               if(CountsOf_0xff == 1) 
00053               {
00054                   StartReceiving = true;
00055                   CountsOf_0xff = 0;
00056               }
00057               else CountsOf_0xff++;
00058             }
00059         }
00060         else
00061         {
00062             ValidData[ReceivingAscii] = bluetooth.getc();
00063             ReceivingAscii++;
00064             if(ReceivingAscii > 20) 
00065             {
00066                 ReceivingAscii = 0;
00067                 StartReceiving = false;
00068                 DataDecoder();
00069             }
00070         }
00071     }
00072     
00073     /*
00074     if(bluetooth.readable())
00075     {
00076         //unsigned char temp = bluetooth.getc();
00077         
00078         BluetoothDataReceived[ReceivingAscii] = bluetooth.getc();
00079         ReceivingAscii++;
00080         if(ReceivingAscii == 23) ReceivingAscii = 0;    
00081         //
00082         if(BluetoothDataReceived[ReceivingAscii] == 0xFF && BluetoothDataReceived[ReceivingAscii - 1] == 0xFF )  
00083         {
00084             DataStartPoint = ReceivingAscii + 1;    
00085             ReadStart = true ;  
00086         }
00087         if( ReadStart == true)  
00088         {
00089             ValidData[] = BluetoothDataReceived[DataStartPoint];
00090         }
00091     }
00092     */
00093     /*
00094     if( ReadStart == true)  
00095     {
00096         for(int i = ReadStart; i<21;i++)
00097         {
00098             ValidData[i] = BluetoothDataReceived[DataStartPoint];
00099         }
00100         DataDecoder();
00101     }
00102     */
00103 }
00104 ///////////////////////////////car 1 means brown head ,red tail , target blue ball
00105 ///////////////////////////////car 2 means pink  head , yellow tail, target green ball
00106 void DataDecoder()
00107 {
00108     Car1X = (ValidData[1] * 256 + ValidData[0])*0.1f;
00109     Car1Y = (ValidData[3] * 256 + ValidData[2])*0.1f;
00110     Car2X = (ValidData[5] * 256 + ValidData[4])*0.1f;
00111     Car2Y = (ValidData[7] * 256 + ValidData[6])*0.1f;
00112     BlueBallX = (ValidData[9] * 256 + ValidData[8])*0.1f;
00113     BlueBallY = (ValidData[11] * 256 + ValidData[10])*0.1f;
00114     GreenBallX = (ValidData[13] * 256 + ValidData[12])*0.1f;
00115     GreenBallY = (ValidData[15] * 256 + ValidData[14])*0.1f;
00116     Car1DirectionInRad = (ValidData[17] * 256 + ValidData[16])*0.001 - PI;
00117     Car2DirectionInRad = (ValidData[19] * 256 + ValidData[18])*0.001 - PI;
00118     BluetoothCommand = ValidData[20];
00119     DataValidorNot = true;    
00120 }