test

Dependencies:   BLE_API mbed nRF51822

Committer:
Sergiika
Date:
Tue Sep 18 15:27:06 2018 +0000
Revision:
0:c73344a2b8b3
bad compile

Who changed what in which revision?

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