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
main.cpp@17:4aa151182398, 2015-02-13 (annotated)
- Committer:
- tumaku
- Date:
- Fri Feb 13 16:05:26 2015 +0000
- Revision:
- 17:4aa151182398
- Parent:
- 16:3faefe15a4a4
- Child:
- 18:5f8cd32ff343
Include while loop in RX interrupt to collect all incoming data
Who changed what in which revision?
User | Revision | Line number | New 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 | 17:4aa151182398 | 64 | while (pcUart.readable()) { |
tumaku | 17:4aa151182398 | 65 | if (pcUart.readable()) { |
tumaku | 17:4aa151182398 | 66 | char s[2]; |
tumaku | 17:4aa151182398 | 67 | s[1]=0; |
tumaku | 17:4aa151182398 | 68 | s[0]=pcUart.getc(); |
tumaku | 17:4aa151182398 | 69 | // DEBUG("-%c_",s[0]); |
tumaku | 17:4aa151182398 | 70 | uartServicePtr->write(s,1); |
tumaku | 17:4aa151182398 | 71 | } |
tumaku | 16:3faefe15a4a4 | 72 | } |
Rohit Grover |
2:e060367b9024 | 73 | } |
yihui | 0:e910d9bb040f | 74 | |
yihui | 0:e910d9bb040f | 75 | int main(void) |
yihui | 0:e910d9bb040f | 76 | { |
tumaku | 16:3faefe15a4a4 | 77 | pcUart.attach(&rxInterrupt,Serial::RxIrq); |
yihui | 0:e910d9bb040f | 78 | |
Rohit Grover |
2:e060367b9024 | 79 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
2:e060367b9024 | 80 | ble.init(); |
bthomsen | 13:291a8f177400 | 81 | ble.onDisconnection(disconnectionCallback); // Define callback function for BLE disconnection event |
bthomsen | 13:291a8f177400 | 82 | ble.onDataWritten(onDataWritten); // Define callback function for BLE Data received event |
yihui | 0:e910d9bb040f | 83 | |
Rohit Grover |
2:e060367b9024 | 84 | /* setup advertising */ |
bthomsen | 13:291a8f177400 | 85 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); // Indicate that Legacy Bluetooth in not supported |
Rohit Grover |
2:e060367b9024 | 86 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 87 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
rgrover1 | 6:e0fc9072e853 | 88 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 89 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 6:e0fc9072e853 | 90 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
yihui | 0:e910d9bb040f | 91 | |
bthomsen | 13:291a8f177400 | 92 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); // Set advertising interval to 1 second |
bthomsen | 13:291a8f177400 | 93 | ble.startAdvertising(); // Start advertising |
yihui | 0:e910d9bb040f | 94 | |
bthomsen | 13:291a8f177400 | 95 | UARTService uartService(ble); // Create BLE UART service |
bthomsen | 13:291a8f177400 | 96 | uartServicePtr = &uartService; // Initalise pointer to point to UART Service |
yihui | 0:e910d9bb040f | 97 | |
Rohit Grover |
2:e060367b9024 | 98 | while (true) { |
bthomsen | 13:291a8f177400 | 99 | ble.waitForEvent(); // Wait for BLE events |
yihui | 0:e910d9bb040f | 100 | } |
yihui | 0:e910d9bb040f | 101 | } |