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:
rgrover1
Date:
Mon Sep 22 10:42:54 2014 +0000
Revision:
6:e0fc9072e853
Parent:
5:4bc41267a03a
Child:
9:7b3841a91548
updating to 0.2.0 of the BLE_API

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
rgrover1 6:e0fc9072e853 20 #include "UARTService.h"
yihui 0:e910d9bb040f 21
rgrover1 6:e0fc9072e853 22 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Rohit Grover 2:e060367b9024 23 * it will have an impact on code-size and power consumption. */
yihui 0:e910d9bb040f 24
Rohit Grover 2:e060367b9024 25 #if NEED_CONSOLE_OUTPUT
rgrover1 6:e0fc9072e853 26 #define DEBUG(...) { printf(__VA_ARGS__); }
yihui 0:e910d9bb040f 27 #else
Rohit Grover 2:e060367b9024 28 #define DEBUG(...) /* nothing */
Rohit Grover 2:e060367b9024 29 #endif /* #if NEED_CONSOLE_OUTPUT */
yihui 0:e910d9bb040f 30
Rohit Grover 2:e060367b9024 31 BLEDevice ble;
Rohit Grover 2:e060367b9024 32 DigitalOut led1(LED1);
yihui 0:e910d9bb040f 33
rgrover1 6:e0fc9072e853 34 UARTService *uartServicePtr;
yihui 0:e910d9bb040f 35
rgrover1 5:4bc41267a03a 36 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 37 {
Rohit Grover 2:e060367b9024 38 DEBUG("Disconnected!\n\r");
Rohit Grover 2:e060367b9024 39 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 2:e060367b9024 40 ble.startAdvertising();
Rohit Grover 2:e060367b9024 41 }
yihui 0:e910d9bb040f 42
rgrover1 6:e0fc9072e853 43 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 44 {
rgrover1 6:e0fc9072e853 45 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
rgrover1 5:4bc41267a03a 46 uint16_t bytesRead = params->len;
rgrover1 5:4bc41267a03a 47 DEBUG("received %u bytes\n\r", bytesRead);
rgrover1 6:e0fc9072e853 48 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
yihui 0:e910d9bb040f 49 }
Rohit Grover 2:e060367b9024 50 }
yihui 0:e910d9bb040f 51
Rohit Grover 2:e060367b9024 52 void periodicCallback(void)
Rohit Grover 2:e060367b9024 53 {
rgrover1 6:e0fc9072e853 54 led1 = !led1;
Rohit Grover 2:e060367b9024 55 }
yihui 0:e910d9bb040f 56
yihui 0:e910d9bb040f 57 int main(void)
yihui 0:e910d9bb040f 58 {
Rohit Grover 2:e060367b9024 59 led1 = 1;
Rohit Grover 2:e060367b9024 60 Ticker ticker;
Rohit Grover 2:e060367b9024 61 ticker.attach(periodicCallback, 1);
yihui 0:e910d9bb040f 62
Rohit Grover 2:e060367b9024 63 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 2:e060367b9024 64 ble.init();
Rohit Grover 2:e060367b9024 65 ble.onDisconnection(disconnectionCallback);
Rohit Grover 2:e060367b9024 66 ble.onDataWritten(onDataWritten);
yihui 0:e910d9bb040f 67
Rohit Grover 2:e060367b9024 68 /* setup advertising */
Rohit Grover 2:e060367b9024 69 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 2:e060367b9024 70 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 71 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 72 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Rohit Grover 2:e060367b9024 73 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 74 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 75
Rohit Grover 2:e060367b9024 76 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
Rohit Grover 2:e060367b9024 77 ble.startAdvertising();
yihui 0:e910d9bb040f 78
rgrover1 6:e0fc9072e853 79 UARTService uartService(ble);
rgrover1 6:e0fc9072e853 80 uartServicePtr = &uartService;
yihui 0:e910d9bb040f 81
Rohit Grover 2:e060367b9024 82 while (true) {
Rohit Grover 2:e060367b9024 83 ble.waitForEvent();
yihui 0:e910d9bb040f 84 }
yihui 0:e910d9bb040f 85 }