Connect the BLE_UART Console to a PC USB/Serial Console. Data written to PC console is sent over BLE UART Data received from BLE UART is displayed in PC console

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LED_Controller by UCL IoT

Committer:
tumaku
Date:
Fri Feb 13 15:14:27 2015 +0000
Revision:
16:3faefe15a4a4
Parent:
15:016434184568
Child:
17:4aa151182398
First version

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
tumaku 16:3faefe15a4a4 22 #define NEED_CONSOLE_OUTPUT 1 /* 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
tumaku 16:3faefe15a4a4 26 #define DEBUG(...) { printf(__VA_ARGS__); } //Defaults to stdio without having to wirte pcUart explicitly
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
tumaku 16:3faefe15a4a4 31 Serial pcUart(USBTX, USBRX); // tx, rx
bthomsen 13:291a8f177400 32
bthomsen 13:291a8f177400 33 BLEDevice ble; // Create Bluetooth object
yihui 0:e910d9bb040f 34
rgrover1 6:e0fc9072e853 35 UARTService *uartServicePtr;
yihui 0:e910d9bb040f 36
bthomsen 13:291a8f177400 37 /* BLE disconnected callback */
rgrover1 5:4bc41267a03a 38 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 39 {
Rohit Grover 2:e060367b9024 40 DEBUG("Disconnected!\n\r");
Rohit Grover 2:e060367b9024 41 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 2:e060367b9024 42 ble.startAdvertising();
Rohit Grover 2:e060367b9024 43 }
bthomsen 13:291a8f177400 44 /* BLE UART data received callback */
rgrover1 6:e0fc9072e853 45 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 46 {
bthomsen 13:291a8f177400 47 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { //If characters received over BLE
rgrover1 5:4bc41267a03a 48 uint16_t bytesRead = params->len;
rgrover1 5:4bc41267a03a 49 DEBUG("received %u bytes\n\r", bytesRead);
bthomsen 13:291a8f177400 50 DEBUG("Received string: '");
tumaku 16:3faefe15a4a4 51 for (int i=0;i<bytesRead; i++) {
tumaku 16:3faefe15a4a4 52 DEBUG("%c",params->data[i]);
tumaku 16:3faefe15a4a4 53 }
tumaku 16:3faefe15a4a4 54 //DEBUG((const char *)params->data); //Note the size of data expands to the largest string received. Need to use bytesRead to resize.
bthomsen 13:291a8f177400 55 DEBUG("'\n\r");
tumaku 16:3faefe15a4a4 56
tumaku 16:3faefe15a4a4 57 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data,bytesRead); // Echo received characters back over BLE
yihui 0:e910d9bb040f 58 }
Rohit Grover 2:e060367b9024 59 }
yihui 0:e910d9bb040f 60
bthomsen 12:36a9f01cfa0a 61 /* Periodic Ticker callback */
tumaku 16:3faefe15a4a4 62 void rxInterrupt(void)
Rohit Grover 2:e060367b9024 63 {
tumaku 16:3faefe15a4a4 64 if (pcUart.readable()) {
tumaku 16:3faefe15a4a4 65 char s[2];
tumaku 16:3faefe15a4a4 66 s[1]=0;
tumaku 16:3faefe15a4a4 67 s[0]=pcUart.getc();
tumaku 16:3faefe15a4a4 68 // DEBUG("-%c_",s[0]);
tumaku 16:3faefe15a4a4 69 uartServicePtr->write(s,1);
tumaku 16:3faefe15a4a4 70 }
Rohit Grover 2:e060367b9024 71 }
yihui 0:e910d9bb040f 72
yihui 0:e910d9bb040f 73 int main(void)
yihui 0:e910d9bb040f 74 {
tumaku 16:3faefe15a4a4 75 pcUart.attach(&rxInterrupt,Serial::RxIrq);
yihui 0:e910d9bb040f 76
Rohit Grover 2:e060367b9024 77 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 2:e060367b9024 78 ble.init();
bthomsen 13:291a8f177400 79 ble.onDisconnection(disconnectionCallback); // Define callback function for BLE disconnection event
bthomsen 13:291a8f177400 80 ble.onDataWritten(onDataWritten); // Define callback function for BLE Data received event
yihui 0:e910d9bb040f 81
Rohit Grover 2:e060367b9024 82 /* setup advertising */
bthomsen 13:291a8f177400 83 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); // Indicate that Legacy Bluetooth in not supported
Rohit Grover 2:e060367b9024 84 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 85 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 86 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Rohit Grover 2:e060367b9024 87 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 88 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 89
bthomsen 13:291a8f177400 90 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); // Set advertising interval to 1 second
bthomsen 13:291a8f177400 91 ble.startAdvertising(); // Start advertising
yihui 0:e910d9bb040f 92
bthomsen 13:291a8f177400 93 UARTService uartService(ble); // Create BLE UART service
bthomsen 13:291a8f177400 94 uartServicePtr = &uartService; // Initalise pointer to point to UART Service
yihui 0:e910d9bb040f 95
Rohit Grover 2:e060367b9024 96 while (true) {
bthomsen 13:291a8f177400 97 ble.waitForEvent(); // Wait for BLE events
yihui 0:e910d9bb040f 98 }
yihui 0:e910d9bb040f 99 }