Send notification to pi from ble nano using ButtonService

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_notifications by Zachary Newman

Committer:
nkosarek
Date:
Wed Apr 19 01:47:36 2017 +0000
Revision:
14:700ba072d1a0
Parent:
13:d4448d59ab69
Working code to send one byte via notification is commented out. Attempt to send 16 bytes is currently compiled.

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
rgrover1 0:28f095301cb2 92 int main(void)
rgrover1 0:28f095301cb2 93 {
znew711 11:fd1cf9dbf3a4 94 pc.printf("entering main!! \r\n");
nkosarek 14:700ba072d1a0 95 //uint8_t counter = 65; //'A'
nkosarek 14:700ba072d1a0 96 uint8_t counter[16] = {65, 65, 65, 65, 65,
nkosarek 14:700ba072d1a0 97 65, 65, 65, 65, 65,
nkosarek 14:700ba072d1a0 98 65, 65, 65, 65, 65,
nkosarek 14:700ba072d1a0 99 0};
rgrover1 0:28f095301cb2 100
andresag 10:7943b5c1117a 101 BLE &ble = BLE::Instance();
andresag 10:7943b5c1117a 102 ble.init(bleInitComplete);
andresag 10:7943b5c1117a 103
znew711 12:e38bb61023ed 104 pc.printf("entering spin loop\r\n");
andresag 10:7943b5c1117a 105 /* SpinWait for initialization to complete. This is necessary because the
andresag 10:7943b5c1117a 106 * BLE object is used in the main loop below. */
andresag 10:7943b5c1117a 107 while (ble.hasInitialized() == false) { /* spin loop */ }
znew711 11:fd1cf9dbf3a4 108 pc.printf("leaving spin loop\r\n");
andresag 10:7943b5c1117a 109
rgrover1 0:28f095301cb2 110 while (true) {
znew711 11:fd1cf9dbf3a4 111 pc.printf("loop!\r\n");
znew711 11:fd1cf9dbf3a4 112 if(isThereAConnection) {
nkosarek 14:700ba072d1a0 113 //pc.printf("sending Notification: %d\r\n", counter);
nkosarek 14:700ba072d1a0 114 pc.printf("sending Notification: %d\r\n", counter[0]);
znew711 11:fd1cf9dbf3a4 115 buttonServicePtr->updateButtonState(counter);
nkosarek 14:700ba072d1a0 116 //counter++;
nkosarek 14:700ba072d1a0 117 for( int i = 0; i < 16; i++ ) {
nkosarek 14:700ba072d1a0 118 counter[i] = counter[i]+1;
nkosarek 14:700ba072d1a0 119 }
znew711 11:fd1cf9dbf3a4 120 } else {
znew711 11:fd1cf9dbf3a4 121 //
rgrover1 9:0f6951db24f1 122 }
znew711 12:e38bb61023ed 123 //ble.waitForEvent();
znew711 12:e38bb61023ed 124 sleep(1000);
rgrover1 0:28f095301cb2 125 }
rgrover1 0:28f095301cb2 126 }