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@2:e060367b9024, 2014-06-13 (annotated)
- Committer:
- Rohit Grover
- Date:
- Fri Jun 13 11:19:22 2014 +0100
- Revision:
- 2:e060367b9024
- Parent:
- 0:e910d9bb040f
- Child:
- 5:4bc41267a03a
update demo to use the new APIs; we have it working now.
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 | |
Rohit Grover |
2:e060367b9024 | 20 | #define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */ |
Rohit Grover |
2:e060367b9024 | 21 | #define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
Rohit Grover |
2:e060367b9024 | 22 | #define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
yihui | 0:e910d9bb040f | 23 | |
Rohit Grover |
2:e060367b9024 | 24 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Rohit Grover |
2:e060367b9024 | 25 | * it will have an impact on code-size and power consumption. */ |
yihui | 0:e910d9bb040f | 26 | |
Rohit Grover |
2:e060367b9024 | 27 | #if NEED_CONSOLE_OUTPUT |
Rohit Grover |
2:e060367b9024 | 28 | Serial pc(USBTX, USBRX); |
Rohit Grover |
2:e060367b9024 | 29 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
yihui | 0:e910d9bb040f | 30 | #else |
Rohit Grover |
2:e060367b9024 | 31 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
2:e060367b9024 | 32 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
yihui | 0:e910d9bb040f | 33 | |
Rohit Grover |
2:e060367b9024 | 34 | BLEDevice ble; |
Rohit Grover |
2:e060367b9024 | 35 | DigitalOut led1(LED1); |
yihui | 0:e910d9bb040f | 36 | |
yihui | 0:e910d9bb040f | 37 | // The Nordic UART Service |
Rohit Grover |
2:e060367b9024 | 38 | static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 39 | static const uint8_t uart_tx_uuid[] = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 40 | static const uint8_t uart_rx_uuid[] = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 41 | static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e}; |
yihui | 0:e910d9bb040f | 42 | uint8_t txPayload[8] = {0,}; |
yihui | 0:e910d9bb040f | 43 | uint8_t rxPayload[8] = {0,}; |
Rohit Grover |
2:e060367b9024 | 44 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, 8, |
Rohit Grover |
2:e060367b9024 | 45 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
Rohit Grover |
2:e060367b9024 | 46 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, 8, |
Rohit Grover |
2:e060367b9024 | 47 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
Rohit Grover |
2:e060367b9024 | 48 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
Rohit Grover |
2:e060367b9024 | 49 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
yihui | 0:e910d9bb040f | 50 | |
Rohit Grover |
2:e060367b9024 | 51 | void disconnectionCallback(void) |
yihui | 0:e910d9bb040f | 52 | { |
Rohit Grover |
2:e060367b9024 | 53 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
2:e060367b9024 | 54 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
2:e060367b9024 | 55 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 56 | } |
yihui | 0:e910d9bb040f | 57 | |
Rohit Grover |
2:e060367b9024 | 58 | void onDataWritten(uint16_t charHandle) |
yihui | 0:e910d9bb040f | 59 | { |
Rohit Grover |
2:e060367b9024 | 60 | if (charHandle == txCharacteristic.getHandle()) { |
Rohit Grover |
2:e060367b9024 | 61 | DEBUG("onDataWritten()\n\r"); |
Rohit Grover |
2:e060367b9024 | 62 | uint16_t bytesRead; |
Rohit Grover |
2:e060367b9024 | 63 | ble.readCharacteristicValue(txCharacteristic.getHandle(), txPayload, &bytesRead); |
Rohit Grover |
2:e060367b9024 | 64 | DEBUG("ECHO: %s\n\r", (char *)txPayload); |
Rohit Grover |
2:e060367b9024 | 65 | ble.updateCharacteristicValue(rxCharacteristic.getHandle(), txPayload, bytesRead); |
yihui | 0:e910d9bb040f | 66 | } |
Rohit Grover |
2:e060367b9024 | 67 | } |
yihui | 0:e910d9bb040f | 68 | |
Rohit Grover |
2:e060367b9024 | 69 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 70 | { |
Rohit Grover |
2:e060367b9024 | 71 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Rohit Grover |
2:e060367b9024 | 72 | } |
yihui | 0:e910d9bb040f | 73 | |
yihui | 0:e910d9bb040f | 74 | int main(void) |
yihui | 0:e910d9bb040f | 75 | { |
Rohit Grover |
2:e060367b9024 | 76 | led1 = 1; |
Rohit Grover |
2:e060367b9024 | 77 | Ticker ticker; |
Rohit Grover |
2:e060367b9024 | 78 | ticker.attach(periodicCallback, 1); |
yihui | 0:e910d9bb040f | 79 | |
Rohit Grover |
2:e060367b9024 | 80 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
2:e060367b9024 | 81 | ble.init(); |
Rohit Grover |
2:e060367b9024 | 82 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 83 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 84 | |
Rohit Grover |
2:e060367b9024 | 85 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 86 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 87 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 88 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Rohit Grover |
2:e060367b9024 | 89 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 90 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Rohit Grover |
2:e060367b9024 | 91 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
yihui | 0:e910d9bb040f | 92 | |
Rohit Grover |
2:e060367b9024 | 93 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 94 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 95 | |
Rohit Grover |
2:e060367b9024 | 96 | ble.addService(uartService); |
yihui | 0:e910d9bb040f | 97 | |
Rohit Grover |
2:e060367b9024 | 98 | while (true) { |
Rohit Grover |
2:e060367b9024 | 99 | ble.waitForEvent(); |
yihui | 0:e910d9bb040f | 100 | } |
yihui | 0:e910d9bb040f | 101 | } |