added

Dependencies:   mbed microbit BLE_API nRF51822

Committer:
rgrover1
Date:
Wed Sep 17 11:37:31 2014 +0000
Revision:
0:2130d7f559dc
Child:
5:2607923acfa1
initial checkin; won't compile; serves as a placeholder

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 0:2130d7f559dc 17 #include "mbed.h"
rgrover1 0:2130d7f559dc 18 #include "BLEDevice.h"
rgrover1 0:2130d7f559dc 19
rgrover1 0:2130d7f559dc 20 #include "UARTService.h"
rgrover1 0:2130d7f559dc 21
rgrover1 0:2130d7f559dc 22 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
rgrover1 0:2130d7f559dc 23 * it will have an impact on code-size and power consumption. */
rgrover1 0:2130d7f559dc 24
rgrover1 0:2130d7f559dc 25 #if NEED_CONSOLE_OUTPUT
rgrover1 0:2130d7f559dc 26 #define DEBUG(...) { printf(__VA_ARGS__); }
rgrover1 0:2130d7f559dc 27 #else
rgrover1 0:2130d7f559dc 28 #define DEBUG(...) /* nothing */
rgrover1 0:2130d7f559dc 29 #endif /* #if NEED_CONSOLE_OUTPUT */
rgrover1 0:2130d7f559dc 30
rgrover1 0:2130d7f559dc 31 BLEDevice ble;
rgrover1 0:2130d7f559dc 32 DigitalOut led1(LED1);
rgrover1 0:2130d7f559dc 33
rgrover1 0:2130d7f559dc 34 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:2130d7f559dc 35 {
rgrover1 0:2130d7f559dc 36 DEBUG("Disconnected!\n\r");
rgrover1 0:2130d7f559dc 37 DEBUG("Restarting the advertising process\n\r");
rgrover1 0:2130d7f559dc 38 ble.startAdvertising();
rgrover1 0:2130d7f559dc 39 }
rgrover1 0:2130d7f559dc 40
rgrover1 0:2130d7f559dc 41 void periodicCallback(void)
rgrover1 0:2130d7f559dc 42 {
rgrover1 0:2130d7f559dc 43 led1 = !led1;
rgrover1 0:2130d7f559dc 44 DEBUG("ping\r\n");
rgrover1 0:2130d7f559dc 45 }
rgrover1 0:2130d7f559dc 46
rgrover1 0:2130d7f559dc 47 int main(void)
rgrover1 0:2130d7f559dc 48 {
rgrover1 0:2130d7f559dc 49 led1 = 1;
rgrover1 0:2130d7f559dc 50 Ticker ticker;
rgrover1 0:2130d7f559dc 51 ticker.attach(periodicCallback, 1);
rgrover1 0:2130d7f559dc 52
rgrover1 0:2130d7f559dc 53 DEBUG("Initialising the nRF51822\n\r");
rgrover1 0:2130d7f559dc 54 ble.init();
rgrover1 0:2130d7f559dc 55 ble.onDisconnection(disconnectionCallback);
rgrover1 0:2130d7f559dc 56
rgrover1 0:2130d7f559dc 57 /* setup advertising */
rgrover1 0:2130d7f559dc 58 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
rgrover1 0:2130d7f559dc 59 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 0:2130d7f559dc 60 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 0:2130d7f559dc 61 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
rgrover1 0:2130d7f559dc 62 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 0:2130d7f559dc 63 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
rgrover1 0:2130d7f559dc 64
rgrover1 0:2130d7f559dc 65 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
rgrover1 0:2130d7f559dc 66 ble.startAdvertising();
rgrover1 0:2130d7f559dc 67
rgrover1 0:2130d7f559dc 68 UARTService uartService(ble);
rgrover1 0:2130d7f559dc 69 uartService.retargetStdout();
rgrover1 0:2130d7f559dc 70
rgrover1 0:2130d7f559dc 71 while (true) {
rgrover1 0:2130d7f559dc 72 ble.waitForEvent();
rgrover1 0:2130d7f559dc 73 }
rgrover1 0:2130d7f559dc 74 }