mbed First Project Code

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 "mbed.h"
00018 #include "ble/BLE.h"
00019 
00020 #include "ble/services/UARTService.h"
00021 
00022 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
00023                                * it will have an impact on code-size and power consumption. */
00024 
00025 #if NEED_CONSOLE_OUTPUT
00026 #define DEBUG(...) { printf(__VA_ARGS__); }
00027 #else
00028 #define DEBUG(...) /* nothing */
00029 #endif /* #if NEED_CONSOLE_OUTPUT */
00030 
00031 BLEDevice  ble;
00032 DigitalOut led1(LED1);
00033 AnalogIn bat(p20);
00034 
00035 UARTService *uartServicePtr;
00036 
00037 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00038 {
00039     DEBUG("Disconnected!\n\r");
00040     DEBUG("Restarting the advertising process\n\r");
00041     ble.startAdvertising();
00042 }
00043 
00044 void onDataWritten(const GattWriteCallbackParams *params)
00045 {
00046     if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
00047         uint16_t bytesRead = params->len;
00048         DEBUG("received %u bytes\n\r", bytesRead);
00049         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
00050     }
00051 }
00052 
00053 void periodicCallback(void)
00054 {
00055     led1 = !led1;
00056 }
00057 
00058 int main(void)
00059 {
00060     led1 = 1;
00061     Ticker ticker;
00062     ticker.attach(periodicCallback, 1);
00063 
00064     DEBUG("Initialising the nRF51822\n\r");
00065     ble.init();
00066     ble.onDisconnection(disconnectionCallback);
00067     ble.onDataWritten(onDataWritten);
00068 
00069     /* setup advertising */
00070     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00071     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00072     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00073                                      (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
00074     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00075                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00076 
00077     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
00078     ble.startAdvertising();
00079 
00080     UARTService uartService(ble);
00081     uartServicePtr = &uartService;
00082     
00083     if (bat.read() != 0)
00084     {
00085         printf("%f", bat.read());
00086     }
00087     
00088     while (true) {
00089         ble.waitForEvent();
00090     }
00091 }