zach_thesholding

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_notifications_with_orig_mbed by Nicholas Kosarek

Committer:
znew711
Date:
Tue Apr 18 17:05:04 2017 +0000
Revision:
11:fd1cf9dbf3a4
Parent:
10:7943b5c1117a
Child:
12:e38bb61023ed
first attempt;

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"
znew711 11:fd1cf9dbf3a4 20 #include <time.h>
rgrover1 0:28f095301cb2 21
znew711 11:fd1cf9dbf3a4 22 const static char DEVICE_NAME[] = "LUMBERJACK_NANO";
rgrover1 0:28f095301cb2 23 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
rgrover1 0:28f095301cb2 24
znew711 11:fd1cf9dbf3a4 25 Serial pc(USBTX, USBRX);
znew711 11:fd1cf9dbf3a4 26
rgrover1 9:0f6951db24f1 27
andresag 10:7943b5c1117a 28 static ButtonService *buttonServicePtr;
znew711 11:fd1cf9dbf3a4 29 bool isThereAConnection = false;
rgrover1 0:28f095301cb2 30
znew711 11:fd1cf9dbf3a4 31 void sleep(unsigned int mseconds)
rgrover1 0:28f095301cb2 32 {
znew711 11:fd1cf9dbf3a4 33 clock_t goal = mseconds + clock();
znew711 11:fd1cf9dbf3a4 34 while (goal > clock());
rgrover1 0:28f095301cb2 35 }
rgrover1 0:28f095301cb2 36
rgrover1 8:a7ba7aaba460 37 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 38 {
andresag 10:7943b5c1117a 39 BLE::Instance().gap().startAdvertising();
znew711 11:fd1cf9dbf3a4 40 isThereAConnection = false;
rgrover1 0:28f095301cb2 41 }
rgrover1 0:28f095301cb2 42
znew711 11:fd1cf9dbf3a4 43 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 44 {
znew711 11:fd1cf9dbf3a4 45 pc.printf("Connection recieved!\r\n");
znew711 11:fd1cf9dbf3a4 46 isThereAConnection = true;
rgrover1 0:28f095301cb2 47 }
rgrover1 0:28f095301cb2 48
andresag 10:7943b5c1117a 49 /**
andresag 10:7943b5c1117a 50 * This function is called when the ble initialization process has failled
andresag 10:7943b5c1117a 51 */
andresag 10:7943b5c1117a 52 void onBleInitError(BLE &ble, ble_error_t error)
andresag 10:7943b5c1117a 53 {
andresag 10:7943b5c1117a 54 /* Initialization error handling should go here */
andresag 10:7943b5c1117a 55 }
andresag 10:7943b5c1117a 56
andresag 10:7943b5c1117a 57 /**
andresag 10:7943b5c1117a 58 * Callback triggered when the ble initialization process has finished
andresag 10:7943b5c1117a 59 */
andresag 10:7943b5c1117a 60 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 10:7943b5c1117a 61 {
andresag 10:7943b5c1117a 62 BLE& ble = params->ble;
andresag 10:7943b5c1117a 63 ble_error_t error = params->error;
andresag 10:7943b5c1117a 64
andresag 10:7943b5c1117a 65 if (error != BLE_ERROR_NONE) {
andresag 10:7943b5c1117a 66 /* In case of error, forward the error handling to onBleInitError */
andresag 10:7943b5c1117a 67 onBleInitError(ble, error);
andresag 10:7943b5c1117a 68 return;
andresag 10:7943b5c1117a 69 }
andresag 10:7943b5c1117a 70
andresag 10:7943b5c1117a 71 /* Ensure that it is the default instance of BLE */
andresag 10:7943b5c1117a 72 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 10:7943b5c1117a 73 return;
andresag 10:7943b5c1117a 74 }
andresag 10:7943b5c1117a 75
andresag 10:7943b5c1117a 76 ble.gap().onDisconnection(disconnectionCallback);
znew711 11:fd1cf9dbf3a4 77 ble.gap().onConnection(connectionCallback);
andresag 10:7943b5c1117a 78
andresag 10:7943b5c1117a 79 /* Setup primary service */
andresag 10:7943b5c1117a 80 buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
andresag 10:7943b5c1117a 81
andresag 10:7943b5c1117a 82 /* setup advertising */
andresag 10:7943b5c1117a 83 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
andresag 10:7943b5c1117a 84 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
andresag 10:7943b5c1117a 85 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andresag 10:7943b5c1117a 86 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andresag 10:7943b5c1117a 87 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
znew711 11:fd1cf9dbf3a4 88 pc.printf("start advertising now \r\n");
andresag 10:7943b5c1117a 89 ble.gap().startAdvertising();
andresag 10:7943b5c1117a 90
andresag 10:7943b5c1117a 91 }
andresag 10:7943b5c1117a 92
rgrover1 0:28f095301cb2 93 int main(void)
rgrover1 0:28f095301cb2 94 {
znew711 11:fd1cf9dbf3a4 95 pc.printf("entering main!! \r\n");
znew711 11:fd1cf9dbf3a4 96 uint8_t counter = 0;
rgrover1 0:28f095301cb2 97
andresag 10:7943b5c1117a 98 BLE &ble = BLE::Instance();
andresag 10:7943b5c1117a 99 ble.init(bleInitComplete);
andresag 10:7943b5c1117a 100
znew711 11:fd1cf9dbf3a4 101 pc.printf("entereing spin loop\r\n");
andresag 10:7943b5c1117a 102 /* SpinWait for initialization to complete. This is necessary because the
andresag 10:7943b5c1117a 103 * BLE object is used in the main loop below. */
andresag 10:7943b5c1117a 104 while (ble.hasInitialized() == false) { /* spin loop */ }
znew711 11:fd1cf9dbf3a4 105 pc.printf("leaving spin loop\r\n");
andresag 10:7943b5c1117a 106
rgrover1 0:28f095301cb2 107 while (true) {
znew711 11:fd1cf9dbf3a4 108 pc.printf("loop!\r\n");
znew711 11:fd1cf9dbf3a4 109 if(isThereAConnection) {
znew711 11:fd1cf9dbf3a4 110 pc.printf("sending Notification\r\n");
znew711 11:fd1cf9dbf3a4 111 buttonServicePtr->updateButtonState(counter);
znew711 11:fd1cf9dbf3a4 112 counter++;
znew711 11:fd1cf9dbf3a4 113 //sleep(1000);
znew711 11:fd1cf9dbf3a4 114 } else {
znew711 11:fd1cf9dbf3a4 115 //
rgrover1 9:0f6951db24f1 116 }
rgrover1 0:28f095301cb2 117 ble.waitForEvent();
rgrover1 0:28f095301cb2 118 }
rgrover1 0:28f095301cb2 119 }