For example with Central/Peripheral: simple service to toggle a led via BLE

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LED_PERIPHERAL by Alex Gernhaeuser

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 #include "LEDService.h"
00020 
00021 DigitalOut alivenessLED(p16, 0);
00022 DigitalOut actuatedLED(p6, 0);
00023 
00024 Serial pc(p5, p4);
00025 
00026 const static char     DEVICE_NAME[] = "LED";
00027 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
00028 
00029 uint8_t ts = 1;
00030 
00031 LEDService *ledServicePtr;
00032 
00033 Ticker ticker;
00034 bool onConnection = false;
00035 
00036 void periodicCallback(void)
00037 {
00038     if(!onConnection){ 
00039         alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
00040         actuatedLED = 1;
00041     }
00042 }
00043 
00044 /**
00045  * This callback allows the LEDService to receive updates to the ledState Characteristic.
00046  *
00047  * @param[in] params
00048  *     Information about the characterisitc being updated.
00049  */
00050 void onDataWrittenCallback(const GattWriteCallbackParams *params) 
00051 {
00052     if ((params->handle == ledServicePtr->getValueHandle())) {
00053         actuatedLED = *(params->data);
00054         onConnection = true;
00055         alivenessLED = 1;
00056     }
00057 }
00058 
00059 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00060 {
00061     BLE::Instance().gap().startAdvertising();
00062     onConnection = false;
00063 }
00064 
00065 
00066 /**
00067  * This function is called when the ble initialization process has failed
00068  */
00069 void onBleInitError(BLE &ble, ble_error_t error)
00070 {
00071     /* Initialization error handling should go here */
00072 }
00073 
00074 /**
00075  * Callback triggered when the ble initialization process has finished
00076  */
00077 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00078 {
00079     BLE&        ble   = params->ble;
00080     ble_error_t error = params->error;
00081     
00082     if (error != BLE_ERROR_NONE) {
00083         /* In case of error, forward the error handling to onBleInitError */
00084         onBleInitError(ble, error);
00085         return;
00086     }
00087 
00088     /* Ensure that it is the default instance of BLE */
00089     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00090         return;
00091     }
00092  
00093     ble.gap().onDisconnection(disconnectionCallback);
00094     ble.gattServer().onDataWritten(onDataWrittenCallback);
00095 
00096     bool initialValueForLEDCharacteristic = false;
00097     ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
00098 
00099     /* setup advertising */
00100     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00101     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00102     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00103     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00104     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00105     ble.gap().startAdvertising();
00106 }
00107 
00108 
00109 int main(void)
00110 {
00111     pc.baud(115200);
00112     pc.printf("Initialization starts... \n");
00113     ticker.attach(periodicCallback, ts); /* Blink LED every second */
00114     actuatedLED = 1;
00115     
00116     BLE &ble = BLE::Instance();
00117     ble.init(bleInitComplete);
00118 
00119     /* SpinWait for initialization to complete. This is necessary because the
00120      * BLE object is used in the main loop below. */
00121     //while (ble.hasInitialized()  == false) { /* spin loop */ }
00122     pc.printf("Initialization finished \n");
00123 
00124     while (true) {
00125         ble.waitForEvent();
00126     }
00127 }