TestPublish

Dependencies:   BLE_API mbed nRF51822

Committer:
savashito
Date:
Thu Jul 12 20:32:54 2018 +0000
Revision:
0:edac93fe2628
First Commit

Who changed what in which revision?

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