Basic BLE comm Control Led using On/Off Command

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Revision:
10:d4cd8edc6216
Parent:
6:e0fc9072e853
Child:
11:4f9451eaca3d
--- a/main.cpp	Wed Nov 05 14:17:33 2014 +0000
+++ b/main.cpp	Sun Nov 09 21:05:20 2014 +0000
@@ -16,10 +16,12 @@
 
 #include "mbed.h"
 #include "BLEDevice.h"
+#include "Servo.h"
+#include <string.h>
 
 #include "UARTService.h"
 
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
 
 #if NEED_CONSOLE_OUTPUT
@@ -30,6 +32,9 @@
 
 BLEDevice  ble;
 DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut pwm_out(P0_30);
+
 
 UARTService *uartServicePtr;
 
@@ -42,9 +47,28 @@
 
 void onDataWritten(const GattCharacteristicWriteCBParams *params)
 {
+    char *str = 0;
+    int count = 0;
+
     if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
         DEBUG("received %u bytes\n\r", bytesRead);
+        
+        str = (char *)malloc((sizeof(char) * bytesRead) + 1);
+        while(count <= bytesRead)
+        {
+            *(str + count) = *(params->data + count);
+            count++;
+        }
+        count--;
+        *(str + count) = '\0';
+        DEBUG("payload = %s\n\r",str);
+        if (strncmp(str,"On",bytesRead) == 0)
+            led2 = 1;
+        else
+            led2 = 0;
+        free(str);    
+        strcpy((char *)params->data,"SPOPOPOSP");
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
     }
 }
@@ -69,7 +93,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+                                     (const uint8_t *)"AdarshBLE UART", sizeof("AdarshBLE UART") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));