UART to Console with NRF51DK board and InAir9 module

Dependencies:   BLE_API SX1272Lib mbed nRF51822

Fork of BLE_UARTConsole by Bluetooth Low Energy

Committer:
rgrover1
Date:
Tue Jul 07 06:58:31 2015 +0000
Revision:
8:f9bf6d403cde
Parent:
5:2607923acfa1
Child:
9:5f0732aa3008
updated to the latest of the underlying libraries.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:2130d7f559dc 1 /* mbed Microcontroller Library
rgrover1 0:2130d7f559dc 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:2130d7f559dc 3 *
rgrover1 0:2130d7f559dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:2130d7f559dc 5 * you may not use this file except in compliance with the License.
rgrover1 0:2130d7f559dc 6 * You may obtain a copy of the License at
rgrover1 0:2130d7f559dc 7 *
rgrover1 0:2130d7f559dc 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:2130d7f559dc 9 *
rgrover1 0:2130d7f559dc 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:2130d7f559dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:2130d7f559dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:2130d7f559dc 13 * See the License for the specific language governing permissions and
rgrover1 0:2130d7f559dc 14 * limitations under the License.
rgrover1 0:2130d7f559dc 15 */
rgrover1 0:2130d7f559dc 16
rgrover1 5:2607923acfa1 17 #include <string.h>
rgrover1 0:2130d7f559dc 18 #include "mbed.h"
rgrover1 8:f9bf6d403cde 19 #include "BLE.h"
rgrover1 0:2130d7f559dc 20
rgrover1 0:2130d7f559dc 21 #include "UARTService.h"
rgrover1 0:2130d7f559dc 22
rgrover1 0:2130d7f559dc 23 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
rgrover1 0:2130d7f559dc 24 * it will have an impact on code-size and power consumption. */
rgrover1 0:2130d7f559dc 25
rgrover1 0:2130d7f559dc 26 #if NEED_CONSOLE_OUTPUT
rgrover1 5:2607923acfa1 27 #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
rgrover1 0:2130d7f559dc 28 #else
rgrover1 0:2130d7f559dc 29 #define DEBUG(...) /* nothing */
rgrover1 0:2130d7f559dc 30 #endif /* #if NEED_CONSOLE_OUTPUT */
rgrover1 0:2130d7f559dc 31
rgrover1 0:2130d7f559dc 32 BLEDevice ble;
rgrover1 0:2130d7f559dc 33 DigitalOut led1(LED1);
rgrover1 5:2607923acfa1 34 UARTService *uart;
rgrover1 0:2130d7f559dc 35
rgrover1 0:2130d7f559dc 36 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:2130d7f559dc 37 {
rgrover1 0:2130d7f559dc 38 DEBUG("Disconnected!\n\r");
rgrover1 0:2130d7f559dc 39 DEBUG("Restarting the advertising process\n\r");
rgrover1 0:2130d7f559dc 40 ble.startAdvertising();
rgrover1 0:2130d7f559dc 41 }
rgrover1 0:2130d7f559dc 42
rgrover1 0:2130d7f559dc 43 void periodicCallback(void)
rgrover1 0:2130d7f559dc 44 {
rgrover1 0:2130d7f559dc 45 led1 = !led1;
rgrover1 0:2130d7f559dc 46 DEBUG("ping\r\n");
rgrover1 0:2130d7f559dc 47 }
rgrover1 0:2130d7f559dc 48
rgrover1 0:2130d7f559dc 49 int main(void)
rgrover1 0:2130d7f559dc 50 {
rgrover1 0:2130d7f559dc 51 led1 = 1;
rgrover1 0:2130d7f559dc 52 Ticker ticker;
rgrover1 0:2130d7f559dc 53 ticker.attach(periodicCallback, 1);
rgrover1 0:2130d7f559dc 54
rgrover1 0:2130d7f559dc 55 DEBUG("Initialising the nRF51822\n\r");
rgrover1 0:2130d7f559dc 56 ble.init();
rgrover1 0:2130d7f559dc 57 ble.onDisconnection(disconnectionCallback);
rgrover1 5:2607923acfa1 58
rgrover1 5:2607923acfa1 59 uart = new UARTService(ble);
rgrover1 0:2130d7f559dc 60
rgrover1 0:2130d7f559dc 61 /* setup advertising */
rgrover1 0:2130d7f559dc 62 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
rgrover1 0:2130d7f559dc 63 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 0:2130d7f559dc 64 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 0:2130d7f559dc 65 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
rgrover1 0:2130d7f559dc 66 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 0:2130d7f559dc 67 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
rgrover1 0:2130d7f559dc 68
rgrover1 0:2130d7f559dc 69 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
rgrover1 0:2130d7f559dc 70 ble.startAdvertising();
rgrover1 0:2130d7f559dc 71
rgrover1 0:2130d7f559dc 72 while (true) {
rgrover1 0:2130d7f559dc 73 ble.waitForEvent();
rgrover1 0:2130d7f559dc 74 }
rgrover1 0:2130d7f559dc 75 }