Aidas Liaudanskas / Mbed 2 deprecated SmartGloves

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

main.cpp

Committer:
AidasL
Date:
2017-05-31
Revision:
23:708cc5ef2604
Parent:
22:406127954d1f

File content as of revision 23:708cc5ef2604:

#include "mbed.h"
#include "ble/BLE.h"
#include "sensors.h"



DigitalOut led(LED1);
uint16_t customServiceUUID  = 0xABCD;
uint16_t readCharUUID       = 0xABCE;
uint16_t writeCharUUID      = 0xABCF;

static volatile bool triggerSensorPolling = false;


const static char DEVICE_NAME[]        = "WorkGloves";     // change this
static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development

/* Set Up custom Characteristics */
static uint16_t readValue[24] = {0x55, 0x33};
ReadOnlyArrayGattCharacteristic<uint16_t, sizeof(readValue)> readChar(readCharUUID, readValue);

static uint8_t writeValue[8] = {0x00};
WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);


/* Set up custom service */
GattCharacteristic *characteristics[] = {&readChar, &writeChar};
GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));

void dump(Datapacket todump)
{
        uint8_t j;
        for (j=0; j<12; j++)
        {
                readValue[j]=todump.flex[j];
        }
        for(j=12; j<18; j++)
        {
                readValue[j]=todump.acc[0][j%6];
        }

        for(j=18; j<24; j++)
        {
                readValue[j]=todump.acc[1][j%6];
        }



}


/*
 *  Restart advertising when phone app disconnects
 */
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
{
        BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
}

void periodicCallback(void)
{
        led = !led; /* Do blinky on LED1 while we're waiting for BLE events */

        /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
         * heavy-weight sensor polling from the main thread. */
        triggerSensorPolling = true;
}

/*
 *  Handle writes to writeCharacteristic for screen data from phone
 */
void writeCharCallback(const GattWriteCallbackParams *params)
{
        /* Check to see what characteristic was written, by handle */
        if(params->handle == writeChar.getValueHandle()) {
                /* Update the readChar with the value of writeChar */
//                BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
        }
}
/*
 * Initialization callback
 */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
        BLE &ble          = params->ble;
        ble_error_t error = params->error;

        if (error != BLE_ERROR_NONE) {
                return;
        }

        ble.gap().onDisconnection(disconnectionCallback);
        ble.gattServer().onDataWritten(writeCharCallback); // TODO: update to flush screen !!!

        /* Setup advertising */
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
        ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
        ble.gap().setAdvertisingInterval(500); // 500ms.

        /* Add our custom service */
        ble.addService(customService);

        /* Start advertising */
        ble.gap().startAdvertising();
}

/*
 *  Main loop
 */
int main(void)
{
        /* initialize stuff */
//        printf("\n\r********* Starting Main Loop *********\n\r");

        led = 1;
        Ticker ticker;
        ticker.attach(periodicCallback, 0.1); // blink LED every 0.5 second
        BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
        ble.init(bleInitComplete);

        /* SpinWait for initialization to complete. This is necessary because the
         * BLE object is used in the main loop below. */
        while (ble.hasInitialized()  == false) { /* spin loop */ }
        uint16_t data[24] = {0x11, 0x22};
        /* Infinite loop waiting for BLE interrupt events */
        while (1) {
                // check for trigger// from periodicCallback()
                if (triggerSensorPolling && ble.getGapState().connected) {
                        triggerSensorPolling = false;
                        data[1] = data[1]+1;
                        BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), (uint8_t*)data , 48 );
                        
                        }
                 else {
                        ble.waitForEvent(); // low power wait for event
                }
        }
}