testing shared repositories

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Committer:
bpd227
Date:
Fri Jun 29 01:13:46 2018 +0000
Revision:
0:d60fd3a04add
test?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bpd227 0:d60fd3a04add 1 /* mbed Microcontroller Library
bpd227 0:d60fd3a04add 2 * Copyright (c) 2006-2013 ARM Limited
bpd227 0:d60fd3a04add 3 *
bpd227 0:d60fd3a04add 4 * Licensed under the Apache License, Version 2.0 (the "License");
bpd227 0:d60fd3a04add 5 * you may not use this file except in compliance with the License.
bpd227 0:d60fd3a04add 6 * You may obtain a copy of the License at
bpd227 0:d60fd3a04add 7 *
bpd227 0:d60fd3a04add 8 * http://www.apache.org/licenses/LICENSE-2.0
bpd227 0:d60fd3a04add 9 *
bpd227 0:d60fd3a04add 10 * Unless required by applicable law or agreed to in writing, software
bpd227 0:d60fd3a04add 11 * distributed under the License is distributed on an "AS IS" BASIS,
bpd227 0:d60fd3a04add 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bpd227 0:d60fd3a04add 13 * See the License for the specific language governing permissions and
bpd227 0:d60fd3a04add 14 * limitations under the License.
bpd227 0:d60fd3a04add 15 */
bpd227 0:d60fd3a04add 16
bpd227 0:d60fd3a04add 17 #include "mbed.h"
bpd227 0:d60fd3a04add 18 #include "ble/BLE.h"
bpd227 0:d60fd3a04add 19 #include "ble/services/HealthThermometerService.h"
bpd227 0:d60fd3a04add 20
bpd227 0:d60fd3a04add 21 DigitalOut led1(LED1);
bpd227 0:d60fd3a04add 22
bpd227 0:d60fd3a04add 23 static HealthThermometerService *thermometerServicePtr;
bpd227 0:d60fd3a04add 24
bpd227 0:d60fd3a04add 25 static const char DEVICE_NAME[] = "Therm";
bpd227 0:d60fd3a04add 26 static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
bpd227 0:d60fd3a04add 27 static volatile bool triggerSensorPolling = false;
bpd227 0:d60fd3a04add 28 static float currentTemperature = 39.6;
bpd227 0:d60fd3a04add 29
bpd227 0:d60fd3a04add 30 /* Restart Advertising on disconnection*/
bpd227 0:d60fd3a04add 31 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
bpd227 0:d60fd3a04add 32 {
bpd227 0:d60fd3a04add 33 BLE::Instance().gap().startAdvertising();
bpd227 0:d60fd3a04add 34 }
bpd227 0:d60fd3a04add 35
bpd227 0:d60fd3a04add 36 void periodicCallback(void)
bpd227 0:d60fd3a04add 37 {
bpd227 0:d60fd3a04add 38 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
bpd227 0:d60fd3a04add 39
bpd227 0:d60fd3a04add 40 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
bpd227 0:d60fd3a04add 41 * heavy-weight sensor polling from the main thread. */
bpd227 0:d60fd3a04add 42 triggerSensorPolling = true;
bpd227 0:d60fd3a04add 43 }
bpd227 0:d60fd3a04add 44
bpd227 0:d60fd3a04add 45 /**
bpd227 0:d60fd3a04add 46 * This function is called when the ble initialization process has failed
bpd227 0:d60fd3a04add 47 */
bpd227 0:d60fd3a04add 48 void onBleInitError(BLE &ble, ble_error_t error)
bpd227 0:d60fd3a04add 49 {
bpd227 0:d60fd3a04add 50 /* Avoid compiler warnings */
bpd227 0:d60fd3a04add 51 (void) ble;
bpd227 0:d60fd3a04add 52 (void) error;
bpd227 0:d60fd3a04add 53 /* Initialization error handling should go here */
bpd227 0:d60fd3a04add 54 }
bpd227 0:d60fd3a04add 55
bpd227 0:d60fd3a04add 56 /**
bpd227 0:d60fd3a04add 57 * Callback triggered when the ble initialization process has finished
bpd227 0:d60fd3a04add 58 */
bpd227 0:d60fd3a04add 59 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
bpd227 0:d60fd3a04add 60 {
bpd227 0:d60fd3a04add 61 BLE& ble = params->ble;
bpd227 0:d60fd3a04add 62 ble_error_t error = params->error;
bpd227 0:d60fd3a04add 63
bpd227 0:d60fd3a04add 64 if (error != BLE_ERROR_NONE) {
bpd227 0:d60fd3a04add 65 /* In case of error, forward the error handling to onBleInitError */
bpd227 0:d60fd3a04add 66 onBleInitError(ble, error);
bpd227 0:d60fd3a04add 67 return;
bpd227 0:d60fd3a04add 68 }
bpd227 0:d60fd3a04add 69
bpd227 0:d60fd3a04add 70 /* Ensure that it is the default instance of BLE */
bpd227 0:d60fd3a04add 71 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
bpd227 0:d60fd3a04add 72 return;
bpd227 0:d60fd3a04add 73 }
bpd227 0:d60fd3a04add 74
bpd227 0:d60fd3a04add 75 ble.gap().onDisconnection(disconnectionCallback);
bpd227 0:d60fd3a04add 76
bpd227 0:d60fd3a04add 77 /* Setup primary service. */
bpd227 0:d60fd3a04add 78 thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR);
bpd227 0:d60fd3a04add 79
bpd227 0:d60fd3a04add 80 /* setup advertising */
bpd227 0:d60fd3a04add 81 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
bpd227 0:d60fd3a04add 82 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
bpd227 0:d60fd3a04add 83 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
bpd227 0:d60fd3a04add 84 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
bpd227 0:d60fd3a04add 85 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
bpd227 0:d60fd3a04add 86 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
bpd227 0:d60fd3a04add 87 ble.gap().startAdvertising();
bpd227 0:d60fd3a04add 88 }
bpd227 0:d60fd3a04add 89
bpd227 0:d60fd3a04add 90 int main(void)
bpd227 0:d60fd3a04add 91 {
bpd227 0:d60fd3a04add 92 led1 = 1;
bpd227 0:d60fd3a04add 93 Ticker ticker;
bpd227 0:d60fd3a04add 94 ticker.attach(periodicCallback, 1);
bpd227 0:d60fd3a04add 95
bpd227 0:d60fd3a04add 96 BLE &ble = BLE::Instance();
bpd227 0:d60fd3a04add 97 ble.init(bleInitComplete);
bpd227 0:d60fd3a04add 98
bpd227 0:d60fd3a04add 99 /* SpinWait for initialization to complete. This is necessary because the
bpd227 0:d60fd3a04add 100 * BLE object is used in the main loop below. */
bpd227 0:d60fd3a04add 101 while (ble.hasInitialized() == false) { /* spin loop */ }
bpd227 0:d60fd3a04add 102
bpd227 0:d60fd3a04add 103 while (true) {
bpd227 0:d60fd3a04add 104 if (triggerSensorPolling && ble.gap().getState().connected) {
bpd227 0:d60fd3a04add 105 triggerSensorPolling = false;
bpd227 0:d60fd3a04add 106
bpd227 0:d60fd3a04add 107 /* In our case, we simply update the dummy temperature measurement. */
bpd227 0:d60fd3a04add 108 currentTemperature += 0.1;
bpd227 0:d60fd3a04add 109 thermometerServicePtr->updateTemperature(currentTemperature);
bpd227 0:d60fd3a04add 110 } else {
bpd227 0:d60fd3a04add 111 ble.waitForEvent();
bpd227 0:d60fd3a04add 112 }
bpd227 0:d60fd3a04add 113 }
bpd227 0:d60fd3a04add 114 }