A template for applications where some small amount of data needs to be notified to a phone app over BLE. It is a good starting point for notifications.

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 #include "ButtonService.h"
00020 
00021 DigitalOut  led1(LED1);
00022 InterruptIn button(BUTTON1);
00023 
00024 const static char     DEVICE_NAME[] = "Button";
00025 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
00026 
00027 enum {
00028     RELEASED = 0,
00029     PRESSED,
00030     IDLE
00031 };
00032 static uint8_t buttonState = IDLE;
00033 
00034 static ButtonService *buttonServicePtr;
00035 
00036 void buttonPressedCallback(void)
00037 {
00038     /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
00039      * BLE device API from the main thread. */
00040     buttonState = PRESSED;
00041 }
00042 
00043 void buttonReleasedCallback(void)
00044 {
00045     /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
00046      * BLE device API from the main thread. */
00047     buttonState = RELEASED;
00048 }
00049 
00050 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00051 {
00052     BLE::Instance().gap().startAdvertising();
00053 }
00054 
00055 void periodicCallback(void)
00056 {
00057     led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
00058 }
00059 
00060 /**
00061  * This function is called when the ble initialization process has failled
00062  */
00063 void onBleInitError(BLE &ble, ble_error_t error)
00064 {
00065     /* Initialization error handling should go here */
00066 }
00067 
00068 /**
00069  * Callback triggered when the ble initialization process has finished
00070  */
00071 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00072 {
00073     BLE&        ble   = params->ble;
00074     ble_error_t error = params->error;
00075 
00076     if (error != BLE_ERROR_NONE) {
00077         /* In case of error, forward the error handling to onBleInitError */
00078         onBleInitError(ble, error);
00079         return;
00080     }
00081 
00082     /* Ensure that it is the default instance of BLE */
00083     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00084         return;
00085     }
00086 
00087     ble.gap().onDisconnection(disconnectionCallback);
00088 
00089     /* Setup primary service */
00090     buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
00091 
00092     /* setup advertising */
00093     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00094     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00095     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00096     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00097     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00098     ble.gap().startAdvertising();
00099 
00100 }
00101 
00102 int main(void)
00103 {
00104     led1 = 1;
00105     Ticker ticker;
00106     ticker.attach(periodicCallback, 1);
00107     button.fall(buttonPressedCallback);
00108     button.rise(buttonReleasedCallback);
00109 
00110     BLE &ble = BLE::Instance();
00111     ble.init(bleInitComplete);
00112     
00113     /* SpinWait for initialization to complete. This is necessary because the
00114      * BLE object is used in the main loop below. */
00115     while (ble.hasInitialized()  == false) { /* spin loop */ }
00116     
00117     while (true) {
00118         if (buttonState != IDLE) {
00119             buttonServicePtr->updateButtonState(buttonState);
00120             buttonState = IDLE;
00121         }
00122 
00123         ble.waitForEvent();
00124     }
00125 }