test

Dependencies:   BLE_API mbed nRF51822

Committer:
dmorris
Date:
Fri Apr 13 01:29:15 2018 +0000
Revision:
0:b5b89fa49f16
Child:
1:34796e08bfca
test;

Who changed what in which revision?

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