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

Committer:
garimagupta002
Date:
Sun Dec 14 21:37:03 2014 +0000
Revision:
10:c3eef6f3686a
Parent:
9:7b3841a91548
First version of the BLE-UART-LCD project.; Does normal loopback in UART adding each byte received. Firmware misbehaving after library update on 14 Dec 2014.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:e910d9bb040f 1 /* mbed Microcontroller Library
yihui 0:e910d9bb040f 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:e910d9bb040f 3 *
yihui 0:e910d9bb040f 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:e910d9bb040f 5 * you may not use this file except in compliance with the License.
yihui 0:e910d9bb040f 6 * You may obtain a copy of the License at
yihui 0:e910d9bb040f 7 *
yihui 0:e910d9bb040f 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:e910d9bb040f 9 *
yihui 0:e910d9bb040f 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:e910d9bb040f 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:e910d9bb040f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:e910d9bb040f 13 * See the License for the specific language governing permissions and
yihui 0:e910d9bb040f 14 * limitations under the License.
yihui 0:e910d9bb040f 15 */
yihui 0:e910d9bb040f 16
yihui 0:e910d9bb040f 17 #include "mbed.h"
Rohit Grover 2:e060367b9024 18 #include "BLEDevice.h"
yihui 0:e910d9bb040f 19
garimagupta002 10:c3eef6f3686a 20 //To use LCD
garimagupta002 10:c3eef6f3686a 21 #include "uLCD_4DGL.h"
garimagupta002 10:c3eef6f3686a 22
garimagupta002 10:c3eef6f3686a 23 //UART Primary Service
rgrover1 6:e0fc9072e853 24 #include "UARTService.h"
yihui 0:e910d9bb040f 25
garimagupta002 10:c3eef6f3686a 26 //Battery and DeviceInformation Auxilary Services
garimagupta002 10:c3eef6f3686a 27 #include "BatteryService.h"
garimagupta002 10:c3eef6f3686a 28 #include "DeviceInformationService.h"
yihui 0:e910d9bb040f 29
Rohit Grover 2:e060367b9024 30 BLEDevice ble;
Rohit Grover 2:e060367b9024 31 DigitalOut led1(LED1);
garimagupta002 10:c3eef6f3686a 32 uLCD_4DGL uLCD(P0_9,P0_11,P0_1);
yihui 0:e910d9bb040f 33
garimagupta002 10:c3eef6f3686a 34 //Using the standard GATT Service IDs.
garimagupta002 10:c3eef6f3686a 35 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE,
garimagupta002 10:c3eef6f3686a 36 GattService::UUID_DEVICE_INFORMATION_SERVICE};
garimagupta002 10:c3eef6f3686a 37
garimagupta002 10:c3eef6f3686a 38 //Used to access the defined in main UARTService object globally.
rgrover1 6:e0fc9072e853 39 UARTService *uartServicePtr;
yihui 0:e910d9bb040f 40
garimagupta002 10:c3eef6f3686a 41 //Keeping a receive buffer to alter the data received.
garimagupta002 10:c3eef6f3686a 42 uint8_t DatatoSend[1000];
garimagupta002 10:c3eef6f3686a 43
rgrover1 5:4bc41267a03a 44 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 45 {
garimagupta002 10:c3eef6f3686a 46 //Print the LCD messages.
garimagupta002 10:c3eef6f3686a 47 uLCD.cls();
garimagupta002 10:c3eef6f3686a 48 uLCD.printf("\n Disconnected.!!\n");
garimagupta002 10:c3eef6f3686a 49 uLCD.printf("\n Restarting the advertising process.");
Rohit Grover 2:e060367b9024 50 ble.startAdvertising();
Rohit Grover 2:e060367b9024 51 }
yihui 0:e910d9bb040f 52
rgrover1 6:e0fc9072e853 53 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 54 {
rgrover1 6:e0fc9072e853 55 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
rgrover1 5:4bc41267a03a 56 uint16_t bytesRead = params->len;
garimagupta002 10:c3eef6f3686a 57
garimagupta002 10:c3eef6f3686a 58 uLCD.cls();
garimagupta002 10:c3eef6f3686a 59 uLCD.printf("Data Received.!!\n");
garimagupta002 10:c3eef6f3686a 60
garimagupta002 10:c3eef6f3686a 61 for(int j=0;j<bytesRead;j++)
garimagupta002 10:c3eef6f3686a 62 {
garimagupta002 10:c3eef6f3686a 63 uLCD.printf(" %x\n",*((params->data)+j));
garimagupta002 10:c3eef6f3686a 64 DatatoSend[j]=(*((params->data)+j))+1;
garimagupta002 10:c3eef6f3686a 65 }
garimagupta002 10:c3eef6f3686a 66 wait(1);
garimagupta002 10:c3eef6f3686a 67
garimagupta002 10:c3eef6f3686a 68 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), DatatoSend, bytesRead);
garimagupta002 10:c3eef6f3686a 69
garimagupta002 10:c3eef6f3686a 70 //Use the below statement for loopback.
garimagupta002 10:c3eef6f3686a 71 //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
garimagupta002 10:c3eef6f3686a 72
garimagupta002 10:c3eef6f3686a 73 uLCD.cls();
garimagupta002 10:c3eef6f3686a 74 uLCD.printf("Data Sent.!!\n");
garimagupta002 10:c3eef6f3686a 75 for(int j=0;j<bytesRead;j++)
garimagupta002 10:c3eef6f3686a 76 {
garimagupta002 10:c3eef6f3686a 77 uLCD.printf(" %x\n",DatatoSend[j]);
garimagupta002 10:c3eef6f3686a 78 }
garimagupta002 10:c3eef6f3686a 79 wait(1);
yihui 0:e910d9bb040f 80 }
Rohit Grover 2:e060367b9024 81 }
yihui 0:e910d9bb040f 82
Rohit Grover 2:e060367b9024 83 void periodicCallback(void)
Rohit Grover 2:e060367b9024 84 {
rgrover1 6:e0fc9072e853 85 led1 = !led1;
Rohit Grover 2:e060367b9024 86 }
yihui 0:e910d9bb040f 87
yihui 0:e910d9bb040f 88 int main(void)
yihui 0:e910d9bb040f 89 {
garimagupta002 10:c3eef6f3686a 90 uLCD.reset();
garimagupta002 10:c3eef6f3686a 91 uLCD.printf("\n Hello...\n"); //Default Green on black text
garimagupta002 10:c3eef6f3686a 92 uLCD.printf("\n Starting BLE-UART Demo...");
jimaobian 9:7b3841a91548 93
Rohit Grover 2:e060367b9024 94 led1 = 1;
Rohit Grover 2:e060367b9024 95 Ticker ticker;
Rohit Grover 2:e060367b9024 96 ticker.attach(periodicCallback, 1);
yihui 0:e910d9bb040f 97
Rohit Grover 2:e060367b9024 98 ble.init();
Rohit Grover 2:e060367b9024 99 ble.onDisconnection(disconnectionCallback);
Rohit Grover 2:e060367b9024 100 ble.onDataWritten(onDataWritten);
yihui 0:e910d9bb040f 101
garimagupta002 10:c3eef6f3686a 102 /* Set up Primary service*/
garimagupta002 10:c3eef6f3686a 103 UARTService uartService(ble);
garimagupta002 10:c3eef6f3686a 104 uartServicePtr = &uartService;
garimagupta002 10:c3eef6f3686a 105
garimagupta002 10:c3eef6f3686a 106 /* Setup auxiliary services. */
garimagupta002 10:c3eef6f3686a 107 BatteryService battery(ble);
garimagupta002 10:c3eef6f3686a 108 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
garimagupta002 10:c3eef6f3686a 109
garimagupta002 10:c3eef6f3686a 110 /* Setup advertising */
garimagupta002 10:c3eef6f3686a 111 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Rohit Grover 2:e060367b9024 112 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 113 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 114 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
garimagupta002 10:c3eef6f3686a 115 ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 116 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
garimagupta002 10:c3eef6f3686a 117 ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
Rohit Grover 2:e060367b9024 118 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
Rohit Grover 2:e060367b9024 119 ble.startAdvertising();
garimagupta002 10:c3eef6f3686a 120 uLCD.text_width(1);
garimagupta002 10:c3eef6f3686a 121 uLCD.text_height(1);
garimagupta002 10:c3eef6f3686a 122 uLCD.color(GREEN);
garimagupta002 10:c3eef6f3686a 123
garimagupta002 10:c3eef6f3686a 124 uLCD.cls();
garimagupta002 10:c3eef6f3686a 125 uLCD.locate(0,2);
garimagupta002 10:c3eef6f3686a 126 uLCD.printf("Started Advertising.!! \n");
garimagupta002 10:c3eef6f3686a 127 wait(1);
jimaobian 9:7b3841a91548 128
garimagupta002 10:c3eef6f3686a 129 while (true) {
jimaobian 9:7b3841a91548 130
garimagupta002 10:c3eef6f3686a 131 uLCD.cls();
garimagupta002 10:c3eef6f3686a 132 uLCD.locate(0,2);
garimagupta002 10:c3eef6f3686a 133 uLCD.printf("\nWaiting to receive Data.\n");
Rohit Grover 2:e060367b9024 134 ble.waitForEvent();
yihui 0:e910d9bb040f 135 }
yihui 0:e910d9bb040f 136 }