main.cpp NFR 1

Dependencies:   BLE_API mbed nRF51822

Committer:
KKongs
Date:
Mon Apr 16 12:35:35 2018 +0000
Revision:
0:a2e964d708dd
_NFR52_DK_main.cpp

Who changed what in which revision?

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