#define LED2CMD "led2"
#define CMD_LENGTH 4
 
 
//char rxPayload[CMD_SIZE];
 
BLEDevice  ble;                               // Create Bluetooth object
DigitalOut led1(LED1);                        // Set the pin attached to LED1 as an output
PwmOut led2(LED2);                        // Set the pin attached to LED2 as an output
 
UARTService *uartServicePtr;
 
/* BLE disconnected callback */
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}
/* BLE UART data received callback */
void onDataWritten(const GattCharacteristicWriteCBParams *params)
{
    if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {     //If characters received over BLE
        uint16_t bytesRead = params->len;
        DEBUG("received %u bytes\n\r", bytesRead);
        DEBUG("Received string: '");
        DEBUG((const char *)params->data);             //Note the size of data expands to the largest string received. Need to use bytesRead to resize.
        DEBUG("'\n\r");
        if (!strncmp(LED2CMD,(const char *)params->data,CMD_LENGTH-1)){
            float value;
            char cmd[CMD_LENGTH];
            sscanf((const char *)params->data, "%s %f", cmd, &value );
            led2 = value;
            DEBUG("Cmd: %s LED Level = %f\n\r", cmd, value);
            }
        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data,bytesRead);   // Echo received characters back over BLE
    }
}
