Aegle / Mbed 2 deprecated Nucleo_BC127_DEBUG

Dependencies:   mbed

main.cpp

Committer:
Dot
Date:
2015-05-01
Revision:
4:51abc140d5d2
Parent:
3:ec621d656d40
Child:
5:ee564e17915b

File content as of revision 4:51abc140d5d2:

#include "mbed.h"
#include<string>
/*
Consider adding:

DEV_INFO =
ACCESSORY_NAME=Arc Chest Strap
MANUFACTURER_NAME=Aegle
MODEL_NAME=Arc CS1
SERIAL_NO=0000000000
HARDWARE_VER=0 5 2 
FIRMWARE_VER=0 5 2
*/

//LED
PwmOut stm_led(LED1);
//Bluetooth
Serial bluetooth(PB_6,PA_10);
Serial pc(SERIAL_TX, SERIAL_RX);

DigitalOut bt_pair(PA_7);
DigitalOut bt_reset(PC_7);

Timer timeout;
char responseBuff[4];
char responseOk[4] = {'\r','\n','K','O'};

bool bluetooth_command(string command){
    bluetooth.printf("%s\r",command);
    pc.printf("SENDING {%s}\r",command);
    timeout.reset();
    timeout.start();
    while(timeout.read_ms()<2000){
        if(memcmp(&responseOk[0],&responseBuff[0],4) == 0){
            pc.printf("%d ms\r",timeout.read_ms());
            timeout.stop();
            //Shift buffer by one to insure previous responses don't interfere
            for(int z=2;z>0;z--){
                responseBuff[z+1]=responseBuff[z];
            }
            responseBuff[1]=responseBuff[0];
            responseBuff[0]=0;
            return true;
        }    
    }
    timeout.stop();
    return false;
}

void ledBlink(int blinks,int delay){
    for(int i=2*blinks; i>0; i--){
        stm_led = !stm_led;
        wait_ms(delay);
    }
}

void bluetoothInitializer(){
    bt_reset = 0;
    wait(0.5);
    bt_reset = 1;
    wait(0.5);
    bt_pair =1;
    wait(0.5);
    bt_pair =0;
    wait(0.5);
}
void readResponse(){
    for(int z=2;z>0;z--){
        responseBuff[z+1]=responseBuff[z];
    }
    responseBuff[1]=responseBuff[0];
    responseBuff[0]=bluetooth.getc();
    pc.putc(responseBuff[0]);
}

void writeResponse(){
    bluetooth.putc(pc.getc());
}
int main() {
    bluetoothInitializer();
    bluetooth.attach(&readResponse);
    pc.attach(&writeResponse);
    wait(5);
    pc.printf("Starting\r");
    bool debug=true;
    stm_led=1;
    wait(1);
    pc.printf("Atempting to send command\r");
    
    bluetooth.printf("$$$$\r");
    wait(1);
    
    bluetooth_command("RESTORE");
    debug&=bluetooth_command("SET NAME=BC127 BLE");
    debug&=bluetooth_command("SET NAME_SHORT=BCBLE");
    debug&=bluetooth_command("SET ENABLE_SPP=ON");
    debug&=bluetooth_command("SET ENABLE_IAP=OFF");
    debug&=bluetooth_command("SET ENABLE_IAP_V2=OFF");
    debug&=bluetooth_command("SET ENABLE_MAP=OFF");
    debug&=bluetooth_command("SET BLE_ROLE=1");
    debug&=bluetooth_command("ADVERTISING ON");
    debug&=bluetooth_command("WRITE");
    bluetooth_command("RESET");
    wait(.1);
    pc.printf("Finished sending commands\r");
    bluetooth.printf("config\r");
    while(1) {
        if(debug==true){
        stm_led=!stm_led;
        wait(2);
        //bluetooth.printf("SEND BLE HelloWorld\r");
        //Results in "ERROR" from BC127
        }
    }
}