Sample BLE code using Led2 to debug

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Button by Bluetooth Low Energy

Committer:
trvd1707
Date:
Thu Sep 01 17:47:24 2016 +0000
Revision:
12:d6be80bffa01
Parent:
11:80a9d811aab1
Adding Led2 to debug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:28f095301cb2 1 /* mbed Microcontroller Library
rgrover1 0:28f095301cb2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:28f095301cb2 3 *
rgrover1 0:28f095301cb2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:28f095301cb2 5 * you may not use this file except in compliance with the License.
rgrover1 0:28f095301cb2 6 * You may obtain a copy of the License at
rgrover1 0:28f095301cb2 7 *
rgrover1 0:28f095301cb2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:28f095301cb2 9 *
rgrover1 0:28f095301cb2 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:28f095301cb2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:28f095301cb2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:28f095301cb2 13 * See the License for the specific language governing permissions and
rgrover1 0:28f095301cb2 14 * limitations under the License.
rgrover1 0:28f095301cb2 15 */
rgrover1 0:28f095301cb2 16
rgrover1 0:28f095301cb2 17 #include "mbed.h"
andresag 10:7943b5c1117a 18 #include "ble/BLE.h"
rgrover1 0:28f095301cb2 19 #include "ButtonService.h"
rgrover1 0:28f095301cb2 20
rgrover1 0:28f095301cb2 21 DigitalOut led1(LED1);
trvd1707 11:80a9d811aab1 22 DigitalOut led2(LED2);
rgrover1 0:28f095301cb2 23 InterruptIn button(BUTTON1);
rgrover1 0:28f095301cb2 24
rgrover1 0:28f095301cb2 25 const static char DEVICE_NAME[] = "Button";
rgrover1 0:28f095301cb2 26 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
rgrover1 0:28f095301cb2 27
rgrover1 9:0f6951db24f1 28 enum {
rgrover1 9:0f6951db24f1 29 RELEASED = 0,
rgrover1 9:0f6951db24f1 30 PRESSED,
rgrover1 9:0f6951db24f1 31 IDLE
rgrover1 9:0f6951db24f1 32 };
rgrover1 9:0f6951db24f1 33 static uint8_t buttonState = IDLE;
rgrover1 9:0f6951db24f1 34
andresag 10:7943b5c1117a 35 static ButtonService *buttonServicePtr;
rgrover1 0:28f095301cb2 36
rgrover1 0:28f095301cb2 37 void buttonPressedCallback(void)
rgrover1 0:28f095301cb2 38 {
rgrover1 9:0f6951db24f1 39 /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
rgrover1 9:0f6951db24f1 40 * BLE device API from the main thread. */
rgrover1 9:0f6951db24f1 41 buttonState = PRESSED;
trvd1707 11:80a9d811aab1 42 led2 = 1;
rgrover1 0:28f095301cb2 43 }
rgrover1 0:28f095301cb2 44
rgrover1 0:28f095301cb2 45 void buttonReleasedCallback(void)
rgrover1 0:28f095301cb2 46 {
rgrover1 9:0f6951db24f1 47 /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
rgrover1 9:0f6951db24f1 48 * BLE device API from the main thread. */
rgrover1 9:0f6951db24f1 49 buttonState = RELEASED;
trvd1707 11:80a9d811aab1 50 led2 = 0;
rgrover1 0:28f095301cb2 51 }
rgrover1 0:28f095301cb2 52
rgrover1 8:a7ba7aaba460 53 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 54 {
andresag 10:7943b5c1117a 55 BLE::Instance().gap().startAdvertising();
trvd1707 12:d6be80bffa01 56 led2=1;
rgrover1 0:28f095301cb2 57 }
rgrover1 0:28f095301cb2 58
rgrover1 0:28f095301cb2 59 void periodicCallback(void)
rgrover1 0:28f095301cb2 60 {
rgrover1 0:28f095301cb2 61 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
rgrover1 0:28f095301cb2 62 }
rgrover1 0:28f095301cb2 63
andresag 10:7943b5c1117a 64 /**
andresag 10:7943b5c1117a 65 * This function is called when the ble initialization process has failled
andresag 10:7943b5c1117a 66 */
andresag 10:7943b5c1117a 67 void onBleInitError(BLE &ble, ble_error_t error)
andresag 10:7943b5c1117a 68 {
andresag 10:7943b5c1117a 69 /* Initialization error handling should go here */
trvd1707 12:d6be80bffa01 70 while(1) {
trvd1707 12:d6be80bffa01 71 led2 = 0;
trvd1707 12:d6be80bffa01 72 wait(0.2);
trvd1707 12:d6be80bffa01 73 led2 = 1;
trvd1707 12:d6be80bffa01 74 wait(0.2);
trvd1707 12:d6be80bffa01 75 }
andresag 10:7943b5c1117a 76 }
andresag 10:7943b5c1117a 77
andresag 10:7943b5c1117a 78 /**
andresag 10:7943b5c1117a 79 * Callback triggered when the ble initialization process has finished
andresag 10:7943b5c1117a 80 */
andresag 10:7943b5c1117a 81 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 10:7943b5c1117a 82 {
trvd1707 12:d6be80bffa01 83 led2=0;
andresag 10:7943b5c1117a 84 BLE& ble = params->ble;
andresag 10:7943b5c1117a 85 ble_error_t error = params->error;
andresag 10:7943b5c1117a 86
andresag 10:7943b5c1117a 87 if (error != BLE_ERROR_NONE) {
andresag 10:7943b5c1117a 88 /* In case of error, forward the error handling to onBleInitError */
andresag 10:7943b5c1117a 89 onBleInitError(ble, error);
andresag 10:7943b5c1117a 90 return;
andresag 10:7943b5c1117a 91 }
andresag 10:7943b5c1117a 92
andresag 10:7943b5c1117a 93 /* Ensure that it is the default instance of BLE */
andresag 10:7943b5c1117a 94 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
trvd1707 12:d6be80bffa01 95 onBleInitError(ble, error);
andresag 10:7943b5c1117a 96 return;
andresag 10:7943b5c1117a 97 }
andresag 10:7943b5c1117a 98
andresag 10:7943b5c1117a 99 ble.gap().onDisconnection(disconnectionCallback);
andresag 10:7943b5c1117a 100
andresag 10:7943b5c1117a 101 /* Setup primary service */
andresag 10:7943b5c1117a 102 buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
andresag 10:7943b5c1117a 103
andresag 10:7943b5c1117a 104 /* setup advertising */
andresag 10:7943b5c1117a 105 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
andresag 10:7943b5c1117a 106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
andresag 10:7943b5c1117a 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andresag 10:7943b5c1117a 108 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andresag 10:7943b5c1117a 109 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
andresag 10:7943b5c1117a 110 ble.gap().startAdvertising();
andresag 10:7943b5c1117a 111
andresag 10:7943b5c1117a 112 }
andresag 10:7943b5c1117a 113
rgrover1 0:28f095301cb2 114 int main(void)
rgrover1 0:28f095301cb2 115 {
rgrover1 0:28f095301cb2 116 led1 = 1;
trvd1707 11:80a9d811aab1 117 led2 = 1;
rgrover1 0:28f095301cb2 118 Ticker ticker;
rgrover1 0:28f095301cb2 119 ticker.attach(periodicCallback, 1);
rgrover1 0:28f095301cb2 120 button.fall(buttonPressedCallback);
rgrover1 0:28f095301cb2 121 button.rise(buttonReleasedCallback);
rgrover1 0:28f095301cb2 122
andresag 10:7943b5c1117a 123 BLE &ble = BLE::Instance();
trvd1707 12:d6be80bffa01 124 ble_error_t error = ble.init(bleInitComplete) ;
trvd1707 12:d6be80bffa01 125 if ( error != BLE_ERROR_NONE){
trvd1707 12:d6be80bffa01 126 /* In case of error, forward the error handling to onBleInitError */
trvd1707 12:d6be80bffa01 127 onBleInitError(ble, error);
trvd1707 12:d6be80bffa01 128 }
andresag 10:7943b5c1117a 129
andresag 10:7943b5c1117a 130 /* SpinWait for initialization to complete. This is necessary because the
andresag 10:7943b5c1117a 131 * BLE object is used in the main loop below. */
trvd1707 12:d6be80bffa01 132 // while (ble.hasInitialized() == false) { /* spin loop */
trvd1707 12:d6be80bffa01 133 // }
rgrover1 0:28f095301cb2 134 while (true) {
andresag 10:7943b5c1117a 135 if (buttonState != IDLE) {
rgrover1 9:0f6951db24f1 136 buttonServicePtr->updateButtonState(buttonState);
rgrover1 9:0f6951db24f1 137 buttonState = IDLE;
rgrover1 9:0f6951db24f1 138 }
rgrover1 9:0f6951db24f1 139
rgrover1 0:28f095301cb2 140 ble.waitForEvent();
rgrover1 0:28f095301cb2 141 }
rgrover1 0:28f095301cb2 142 }