ll

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NewBluetooth.h Source File

NewBluetooth.h

00001 #include "mbed.h"
00002  
00003  
00004  
00005  
00006  Serial bluetooth(D10, D2); // tx,rx
00007 
00008 void Bluetooth_Init();
00009 
00010 void DataDecoder();
00011 bool BluetoothDataValid;
00012 unsigned char BluetoothDataReceived[24];
00013 unsigned char ValidData[22];
00014 int ReadingAscii;
00015 int DataStartPoint;
00016 bool ReadStart;
00017 int ReceivingAscii;
00018 int CountsOf_0xff;
00019 bool StartReceiving;
00020 bool DataValidorNot = false;
00021 
00022 
00023 ///////////////////////////
00024 float Car1X ;
00025 float Car1Y; 
00026 float Car2X ;
00027 float Car2Y ;
00028 float BlueBallX ;
00029 float BlueBallY ;
00030 float GreenBallX ;
00031 float GreenBallY ;
00032 float Car1DirectionInRad;
00033 float Car2DirectionInRad;
00034 unsigned char BluetoothCommand ;
00035 bool FirstReceive = true;
00036 ///////////////////////////////
00037 
00038 
00039 ///////////////////////////////car 1 means brown head ,red tail , target blue ball
00040 ///////////////////////////////car 2 means pink  head , yellow tail, target green ball
00041 void DataDecoder()
00042 {
00043     Car1X = (ValidData[1] * 256 + ValidData[0])*0.1f;
00044     Car1Y = (ValidData[3] * 256 + ValidData[2])*0.1f;
00045     Car2X = (ValidData[5] * 256 + ValidData[4])*0.1f;
00046     Car2Y = (ValidData[7] * 256 + ValidData[6])*0.1f;
00047     BlueBallX = (ValidData[9] * 256 + ValidData[8])*0.1f;
00048     BlueBallY = (ValidData[11] * 256 + ValidData[10])*0.1f;
00049     GreenBallX = (ValidData[13] * 256 + ValidData[12])*0.1f;
00050     GreenBallY = (ValidData[15] * 256 + ValidData[14])*0.1f;
00051     Car1DirectionInRad = (ValidData[17] * 256 + ValidData[16])*0.001 - PI;
00052     Car2DirectionInRad = (ValidData[19] * 256 + ValidData[18])*0.001 - PI;
00053     BluetoothCommand = ValidData[20];
00054     DataValidorNot = true;    
00055 }
00056  
00057  
00058  
00059  
00060  
00061  
00062  
00063  
00064  
00065  
00066  
00067  
00068  
00069  
00070  
00071  
00072  
00073  
00074  
00075  
00076  
00077  
00078  
00079  
00080  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00081 //------------------------------------
00082 // Hyperterminal configuration
00083 // 9600 bauds, 8-bit data, no parity
00084 //------------------------------------
00085  
00086 //Serial pc(SERIAL_TX, SERIAL_RX);
00087  
00088 //Serial bluetooth(D10, D2); // tx,rx
00089  
00090 // Circular buffers for serial TX and RX data - used by interrupt routines
00091 const int buffer_size = 100;
00092 // might need to increase buffer size for high baud rates
00093 uint8_t rx_buffer[buffer_size+1];
00094 // Circular buffer pointers
00095 // volatile makes read-modify-write atomic 
00096 volatile int rx_in=0;
00097 volatile int rx_out=0;
00098  
00099 void Rx_interrupt();
00100 bool availableRxFIFO();
00101 uint8_t readRxFIFO();
00102  
00103 
00104 void Bluetooth_Init()
00105 {
00106     
00107     bluetooth.baud(9600);
00108     BluetoothDataValid = false;
00109     ReceivingAscii = 0;
00110     ReadStart = false;
00111     CountsOf_0xff = 0;
00112     StartReceiving = false;
00113     bluetooth.attach(&Rx_interrupt, Serial::RxIrq);
00114 }
00115 
00116 
00117 void GetBluetoothData()
00118 {    
00119 
00120     if(availableRxFIFO())
00121     {
00122         if(StartReceiving == false)
00123         {
00124             unsigned char temp = readRxFIFO();
00125             if(temp == 0xFF) 
00126             {
00127               if(CountsOf_0xff == 1) 
00128               {
00129                   StartReceiving = true;
00130                   CountsOf_0xff = 0;
00131               }
00132               else CountsOf_0xff++;
00133             }
00134         }
00135         else
00136         {
00137             ValidData[ReceivingAscii] = readRxFIFO();
00138             ReceivingAscii++;
00139             if(ReceivingAscii > 21) 
00140             {
00141                 ReceivingAscii = 0;
00142                 StartReceiving = false;
00143                 DataDecoder();
00144             }
00145         }
00146     }
00147 }
00148  
00149  /*
00150 int main() {
00151   pc.printf("Hello World !\n");
00152   
00153   bluetooth.baud(9600);
00154   // Setup a serial interrupt function to receive data
00155   bluetooth.attach(&Rx_interrupt, Serial::RxIrq);
00156   
00157   while(1) { 
00158   if(availableRxFIFO())
00159   {
00160     uint8_t temp = readRxFIFO();   
00161     pc.printf("  count: %d\n",temp);
00162   }
00163   wait(0.1);
00164   
00165   }
00166 }
00167  
00168  */
00169 bool availableRxFIFO()
00170 {
00171     if(rx_out != rx_in) 
00172     {return 1;}
00173     else
00174     {return 0;}
00175 }
00176  
00177 uint8_t readRxFIFO()
00178 {
00179     if(rx_out != rx_in) {
00180         uint8_t Output = rx_buffer[rx_out];
00181         rx_out = (rx_out + 1) % buffer_size;
00182         return Output;
00183     }
00184     return 0;
00185 }
00186  
00187 // Interupt Routine to read in data from serial port
00188 void Rx_interrupt() {
00189 // Loop just in case more than one character is in UART's receive FIFO buffer
00190 // Stop if buffer full
00191     while ((bluetooth.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00192         rx_buffer[rx_in] = bluetooth.getc();
00193 // Uncomment to Echo to USB serial to watch data flow
00194 //        pc.putc(rx_buffer[rx_in]);
00195         rx_in = (rx_in + 1) % buffer_size;
00196     }
00197     return;
00198 }