Use of nRF51822 to demonstrate usage of BLE-UART service provided by Nordic and the use of uLCD-144-G2. The demonstration uses the nRF Master Control Panel and nRF UART demo apps provided by Nordic and available on Google Play and Apple Store. The firmware receives data over the TX characteristic, increments it by 1 and sends it over RX characteristic. The notifications need to be enabled for the RX characteristic in the Master Control Panel to be able to receive the data. For more information check the notebook page: http://developer.mbed.org/users/garimagupta002/notebook/ble-uart-lcd-demo/

Dependencies:   4DGL-uLCD-SE BLE_API DFRobot_BLE_LoopbackUART mbed nRF51822

Fork of DFRobot_BLE_LoopbackUART by Angelo qiao

Revision:
10:c3eef6f3686a
Parent:
9:7b3841a91548
--- a/main.cpp	Fri Oct 31 07:55:37 2014 +0000
+++ b/main.cpp	Sun Dec 14 21:37:03 2014 +0000
@@ -17,28 +17,36 @@
 #include "mbed.h"
 #include "BLEDevice.h"
 
+//To use LCD
+#include "uLCD_4DGL.h"
+
+//UART Primary Service
 #include "UARTService.h"
 
-#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
-#define DEBUG(...) { printf(__VA_ARGS__); }
-#else
-#define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
+//Battery and DeviceInformation Auxilary Services
+#include "BatteryService.h"
+#include "DeviceInformationService.h"
 
 BLEDevice  ble;
 DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-Timer timer;
+uLCD_4DGL uLCD(P0_9,P0_11,P0_1);
 
+//Using the standard GATT Service IDs.
+static const uint16_t uuid16_list[]        = {GattService::UUID_BATTERY_SERVICE,
+                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
+                                              
+//Used to access the defined in main UARTService object globally.
 UARTService *uartServicePtr;
 
+//Keeping a receive buffer to alter the data received.
+uint8_t DatatoSend[1000];
+
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    DEBUG("Disconnected!\n\r");
-    DEBUG("Restarting the advertising process\n\r");
+    //Print the LCD messages.
+    uLCD.cls();
+    uLCD.printf("\n Disconnected.!!\n");
+    uLCD.printf("\n Restarting the advertising process.");
     ble.startAdvertising();
 }
 
@@ -46,8 +54,29 @@
 {
     if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
-        DEBUG("received %u bytes\n\r", bytesRead);
-        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+
+        uLCD.cls();
+        uLCD.printf("Data Received.!!\n");
+        
+        for(int j=0;j<bytesRead;j++)
+        {
+            uLCD.printf(" %x\n",*((params->data)+j));
+            DatatoSend[j]=(*((params->data)+j))+1;
+        }
+        wait(1);
+        
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), DatatoSend, bytesRead);
+
+        //Use the below statement for loopback.
+        //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        
+        uLCD.cls();
+        uLCD.printf("Data Sent.!!\n");
+        for(int j=0;j<bytesRead;j++)
+        {
+            uLCD.printf(" %x\n",DatatoSend[j]);
+        }
+        wait(1);
     }
 }
 
@@ -58,66 +87,50 @@
 
 int main(void)
 {
-    
-    
-    timer.start();
+    uLCD.reset();    
+    uLCD.printf("\n Hello...\n"); //Default Green on black text
+    uLCD.printf("\n Starting BLE-UART Demo...");
     
     led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, 1);
 
-    DEBUG("Initialising the nRF51822\n\r");
     ble.init();
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(onDataWritten);
 
-    /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    /* Set up Primary service*/
+    UARTService uartService(ble);
+    uartServicePtr = &uartService;
+    
+    /* Setup auxiliary services. */
+    BatteryService           battery(ble);
+    DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+
+    /* Setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                      (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
-
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));                               
     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.startAdvertising();
-
-    UARTService uartService(ble);
-    uartServicePtr = &uartService;
-
-
-    while (true) {
-        static unsigned long led2timer=timer.read_ms();
+    uLCD.text_width(1);
+    uLCD.text_height(1);
+    uLCD.color(GREEN);
+    
+    uLCD.cls();
+    uLCD.locate(0,2);
+    uLCD.printf("Started Advertising.!! \n");
+    wait(1);
         
-        if((unsigned long)timer.read_ms()-led2timer>1000UL)
-        {
-            led2timer=timer.read_ms();
-            led2=!led2;
-        }
+    while (true) {
         
-        
-        
+        uLCD.cls();
+        uLCD.locate(0,2);
+        uLCD.printf("\nWaiting to receive Data.\n");  
         ble.waitForEvent();
     }
 }
-
-//// Count the time to toggle a LED
-// 
-//#include "mbed.h"
-// 
-//Timer timer;
-//DigitalOut led(LED1);
-//int begin, end;
-// 
-//int main() {
-//    timer.start();
-//    
-//    while(true)
-//    {
-////    begin = timer.read_ms();
-//    led = !led;
-//    wait(1);
-////    end = timer.read_ms();
-////    printf("%d us\r\n", end - begin);
-//    }
-//}