Aegle / Mbed 2 deprecated Nucleo_BC127_DEBUG

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include<string>
00003 //LED
00004 PwmOut stm_led(LED1);
00005 //Bluetooth
00006 Serial bluetooth(PB_6,PA_10);
00007 Serial pc(SERIAL_TX, SERIAL_RX);
00008 
00009 DigitalOut bt_pair(PA_7);
00010 DigitalOut bt_reset(PC_7);
00011 
00012 Timer timeout;
00013 char responseBuff[4];
00014 char responseOk[4] = {'\r','\n','K','O'};
00015 
00016 bool bluetooth_command(string command){
00017     pc.printf("SENDING {%s}\r",command);
00018     bluetooth.printf("%s\r",command);
00019     timeout.reset();
00020     timeout.start();
00021     while(timeout.read_ms()<2000){
00022         if(memcmp(&responseOk[0],&responseBuff[0],4) == 0){
00023             pc.printf("%d ms\r",timeout.read_ms());
00024             timeout.stop();
00025             //Shift buffer by one to insure previous responses don't interfere
00026             for(int z=2;z>0;z--){
00027                 responseBuff[z+1]=responseBuff[z];
00028             }
00029             responseBuff[1]=responseBuff[0];
00030             responseBuff[0]=0;
00031             return true;
00032         }    
00033     }
00034     timeout.stop();
00035     return false;
00036 }
00037 
00038 void ledBlink(int blinks,int delay){
00039     for(int i=2*blinks; i>0; i--){
00040         stm_led = !stm_led;
00041         wait_ms(delay);
00042     }
00043 }
00044 
00045 void bluetoothInitializer(){
00046     bt_reset = 0;
00047     wait(0.5);
00048     bt_reset = 1;
00049     wait(0.5);
00050     bt_pair =1;
00051     wait(0.5);
00052     bt_pair =0;
00053     wait(0.5);
00054     bluetooth.baud(9600);
00055     wait(.1);
00056 }
00057 void readResponse(){
00058     for(int z=2;z>0;z--){
00059         responseBuff[z+1]=responseBuff[z];
00060     }
00061     responseBuff[1]=responseBuff[0];
00062     responseBuff[0]=bluetooth.getc();
00063     pc.putc(responseBuff[0]);
00064 }
00065 
00066 void writeResponse(){
00067     bluetooth.putc(pc.getc());
00068 }
00069 int main() {
00070     bluetoothInitializer();
00071     bluetooth.attach(&readResponse);
00072     pc.attach(&writeResponse);
00073     wait(5);
00074     pc.printf("Starting\r");
00075     bool debug=true;
00076     stm_led=1;
00077     wait(1);
00078     pc.printf("Atempting to send command\r");
00079     wait(1);
00080     bluetooth_command("RESTORE");
00081     debug&=bluetooth_command("SET NAME=BC127 BLE");
00082     debug&=bluetooth_command("SET NAME_SHORT=BCBLE");
00083     debug&=bluetooth_command("SET ENABLE_SPP=ON");
00084     debug&=bluetooth_command("SET ENABLE_IAP=OFF");
00085     debug&=bluetooth_command("SET ENABLE_IAP_V2=OFF");
00086     debug&=bluetooth_command("SET ENABLE_MAP=OFF");
00087     debug&=bluetooth_command("SET BLE_ROLE=1");
00088     debug&=bluetooth_command("ADVERTISING ON");
00089     debug&=bluetooth_command("WRITE");
00090     bluetooth_command("RESET");
00091     wait(.1);
00092     pc.printf("Finished sending commands\r");
00093     bluetooth.printf("config\r");
00094     while(1) {
00095         if(debug==true){
00096         stm_led=!stm_led;
00097         wait(2);
00098         }
00099     }
00100 }