A console service.

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <string.h>
00018 #include "mbed.h"
00019 #include "BLE.h"
00020 
00021 #include "UARTService.h"
00022 
00023 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
00024                                * it will have an impact on code-size and power consumption. */
00025 
00026 #if NEED_CONSOLE_OUTPUT
00027 #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
00028 #else
00029 #define DEBUG(...) /* nothing */
00030 #endif /* #if NEED_CONSOLE_OUTPUT */
00031 
00032 BLEDevice  ble;
00033 DigitalOut led1(LED1);
00034 UARTService *uart;
00035 
00036 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00037 {
00038     DEBUG("Disconnected!\n\r");
00039     DEBUG("Restarting the advertising process\n\r");
00040     ble.startAdvertising();
00041 }
00042 
00043 void periodicCallback(void)
00044 {
00045     led1 = !led1;
00046     DEBUG("ping\r\n");
00047 }
00048 
00049 int main(void)
00050 {
00051     led1 = 1;
00052     Ticker ticker;
00053     ticker.attach(periodicCallback, 1);
00054 
00055     DEBUG("Initialising the nRF51822\n\r");
00056     ble.init();
00057     ble.onDisconnection(disconnectionCallback);
00058     
00059     uart = new UARTService(ble);
00060 
00061     /* setup advertising */
00062     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00063     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00064     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00065                                      (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
00066     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00067                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00068 
00069     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
00070     ble.startAdvertising();
00071 
00072     while (true) {
00073         ble.waitForEvent();
00074     }
00075 }