Dissertation project, looking at BLE communication in cars. This project is BLE peripheral acting as car indicator stalk

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_test1 by Alexander Lea

main.cpp

Committer:
alexanderlea
Date:
2015-03-12
Revision:
9:0ed64b14d46b
Parent:
6:1d7e5fcc8d00
Child:
10:2c5c202c69a5

File content as of revision 9:0ed64b14d46b:

/*
*   TODO:
*       - Make it work!
*/

#include "mbed.h"
#include "BLEDevice.h"
#include "BroadcasterService.h"
#include <string>

using namespace std;

BLEDevice ble;
DigitalOut led1(LED1);
Serial pc(USBTX, USBRX);

/*Variable Declarations*/
const static char     DEVICE_NAME[]        = "BLE_Broadcaster";
static volatile bool go = false;

void blink(void)
{
    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
    go = true;

}

void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason);
    ble.startAdvertising();
}

void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms)
{
    pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr);
    ble.stopAdvertising();
}

int main(void)
{
    //TODO: I want to be able to send something like:
    uint8_t cmdOn[8] = { 0x4d,0x32,0x81,0xc0,0x01,0x01,0x01,0x01 };
    uint8_t cmdOff[8] = { 0x4d,0x32,0x81,0xc0,0x00,0x00,0x00,0x00 };
    //with first 4 bits being type code, and last 4 being command, or something

    //uint8_t cmdOn = 0x01;
    //uint8_t cmdOff = 0x02;

    //blinky
    led1 = 1;
    Ticker t;
    t.attach(blink, 5.0f);

    //Create BLE stuff
    ble.init();
    ble.onDisconnection(disconnectionCallback);
    ble.onConnection(connectionCallback);

    //CarCommsService commsService(ble, cmd);
    BroadcasterService broadcasterService(ble);

    /*
    **BREDR_NOT_SUPPORTED = BLE only
    **LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out)
    **ADV_CONNECTABLE_UNDIRECTED = Any central device can connect
    */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
//    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);

    //Advertise
    ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600
    ble.startAdvertising();

    pc.printf("Advertising node %s\n\r", DEVICE_NAME);
    bool x = true;

    while(true) {

        ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
        if(go) {
            if(x) {
                pc.printf("Command = %u\r\n", cmdOn);
                broadcasterService.sendCommand(cmdOn);
                x = false;
            } else {
                pc.printf("Command = %u\r\n", cmdOff);
                broadcasterService.sendCommand(cmdOff);
                x = true;
            }
            go = false;
        }
    }
}