Edoardo Armeriv / Mbed 2 deprecated lab5

Dependencies:   mbed mbed-rtos tsi_sensor SLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HC06_bluetooth.h Source File

HC06_bluetooth.h

00001 #ifndef HC06_BLUETOOTH_H
00002 #define HC06_BLUETOOTH_H
00003 
00004 Serial bt(PTE16, PTE17);
00005 
00006 char bt_c;
00007 char bt_buffer[80];
00008 char bt_name[80];
00009 int bt_pin;
00010 int bt_count;
00011 int i;
00012 int action;
00013 int bt_baud[10]={0,1200,2400,4800,9600,19200,38400,57600,115200,230400};
00014 int bt_baud_select=4;
00015 
00016 int bt_baud_decode(int baud)
00017 {
00018     int target_baud;
00019     target_baud=0;
00020     if(baud >= 1 && baud <= 9)
00021     {
00022         target_baud=baud;
00023     }
00024     else
00025     {
00026         for(int i = 1; i <=9, i++)
00027         {
00028             if(baud ==  bt_baud(i))
00029             {
00030                 target_baud=baud;           
00031             }
00032         }
00033     }
00034     if(target_baud >0)
00035     {
00036         return(bt_baud(target_baud));
00037     }
00038     else
00039     {
00040         return(-1);   
00041     }    
00042 }
00043 
00044 int bt_baud(int baud);
00045 {
00046     int target_baud;
00047     target_baud=bt_baud_decode(baud);
00048     if(target_baud > 0)
00049     {
00050         bt.baud(bt_baud(target_baud));
00051         return(bt_baud(target_baud));
00052     }
00053     else
00054     {
00055         return(-1);   
00056     }     
00057 }
00058 
00059 int bt_find_baud()
00060 {
00061     
00062     for(bt_baud_select=1; bt_baud_select<=9; bt_baud_select++)
00063     {
00064 //        pc.printf("Trying case %1i: %i baud\n",bt_baud_select,bt_baud[bt_baud_select]);
00065         bt.baud(bt_baud[bt_baud_select]);
00066 //        pc.printf("Sending \"AT\"\n");
00067         bt_count=0;
00068         bt_buffer[0]=0;
00069         bt_buffer[1]=0;
00070         bt.printf("AT");  /*ask bluetooth for response, expect "OK" */
00071 //        bt.printf("AT\r\n");  /*ask bluetooth for response, expect "OK" */
00072 
00073         for( i=0; i<10000000; i++)
00074         {
00075             if(bt.readable())
00076             {
00077                 bt_buffer[bt_count]=bt.getc();
00078                 pc.putc(bt_buffer[bt_count]);
00079                 if(bt_count==0 && bt_buffer[0]=='O')
00080                     {bt_count++;}
00081                 if(bt_count==1 && bt_buffer[1]=='K')
00082                     {bt_count++; break;}
00083             }
00084         }
00085         if(bt_buffer[0]=='O' && bt_buffer[1]=='K')
00086         {
00087 //            pc.printf("\nReceived \"OK\" at case %1i: %i baud\n",bt_baud_select,bt_baud[bt_baud_select]);
00088             break;
00089         }
00090         else
00091         {
00092 //             pc.printf("\nFailed to receive \"OK\" at case %1i: %i baud\n",bt_baud_select,bt_baud[bt_baud_select]);
00093         }           
00094     }
00095     if(bt_baud_select > 9)
00096     {
00097 //        pc.printf("Unable to identify HC-06 Bluetooth baud rate.\nShutting down.\n");
00098         return(-1);
00099     }
00100     else
00101     {
00102         return(bt_baud[bt_baud_select]);
00103     }        
00104 }
00105 
00106 int bt_default_baud(int baud);
00107 {
00108     int target_baud;
00109     target_baud=lcd_baud_decode(baud);
00110     if(target_baud < 0)
00111     {
00112         return(-1);   
00113     }  
00114     bt_baud(bt_find_baud());
00115     bt.printf("AT+BAUD%1i",target_baud);
00116     bt_baud(target_baud);
00117     for( i=0; i<10000000; i++)
00118     {
00119         if(bt.readable())
00120         {
00121             bt.getc();
00122         }
00123     } 
00124     return(bt_baud[target_baud]);
00125 }
00126 
00127 int bt_device_name(char bt_name[])
00128 {
00129     bt.printf("AT+NAME%s",bt_name);
00130     for( i=0; i<10000000; i++)
00131     {
00132         if(bt.readable())
00133         {
00134             bt.getc();
00135         }
00136     }        
00137 }
00138 
00139 int bt_set_pin(int bt_pin)
00140 {
00141     if(bt_pin < 0 || bt_pin > 9999) {return(-1);}
00142     bt.printf("AT+PIN%04i",bt_pin);
00143     for( i=0; i<10000000; i++)
00144     {
00145         if(bt.readable())
00146         {
00147             bt.getc();
00148         }
00149     }  
00150 
00151 }
00152 
00153 
00154 #endif //HC06_BLUETOOTH_H