Amit Gandhi / Mbed 2 deprecated BLE_LED

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.h"
00019 #include "LEDService.h"
00020 
00021 BLE        ble;
00022 DigitalOut alivenessLED(LED1);
00023 DigitalOut actuatedLED(LED2);
00024 
00025 const static char     DEVICE_NAME[] = "LED";
00026 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
00027 
00028 LEDService *ledServicePtr;
00029 
00030 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00031 {
00032     ble.gap().startAdvertising();
00033 }
00034 
00035 void periodicCallback(void)
00036 {
00037     //alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
00038 }
00039 
00040 /**
00041  * This callback allows the LEDService to receive updates to the ledState Characteristic.
00042  *
00043  * @param[in] params
00044  *     Information about the characterisitc being updated.
00045  */
00046 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00047     if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
00048         actuatedLED = *(params->data);
00049     }
00050 }
00051 
00052 int main(void)
00053 {
00054     alivenessLED = 0;
00055     actuatedLED  = 0;
00056 
00057     Ticker ticker;
00058     ticker.attach(periodicCallback, 1);
00059 
00060     ble.init();
00061     ble.gap().onDisconnection(disconnectionCallback);
00062     ble.gattServer().onDataWritten(onDataWrittenCallback);
00063 
00064     bool initialValueForLEDCharacteristic = false;
00065     LEDService ledService(ble, initialValueForLEDCharacteristic);
00066     ledServicePtr = &ledService;
00067 
00068     /* setup advertising */
00069     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00070     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00071     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00072     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00073     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00074     ble.gap().startAdvertising();
00075 
00076     while (true) {
00077         ble.waitForEvent();
00078     }
00079 }