changed name

Fork of mbed-os-example-ble-BatteryLevel by mbed-os-examples

Committer:
vazbyte
Date:
Sun Oct 28 12:06:45 2018 +0000
Revision:
76:9a6403ea5c9b
Parent:
75:fc7f3b141378
n

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 4:52bffaff7f0b 1 /* mbed Microcontroller Library
mbed_official 4:52bffaff7f0b 2 * Copyright (c) 2006-2014 ARM Limited
mbed_official 4:52bffaff7f0b 3 *
mbed_official 4:52bffaff7f0b 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 4:52bffaff7f0b 5 * you may not use this file except in compliance with the License.
mbed_official 4:52bffaff7f0b 6 * You may obtain a copy of the License at
mbed_official 4:52bffaff7f0b 7 *
mbed_official 4:52bffaff7f0b 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 4:52bffaff7f0b 9 *
mbed_official 4:52bffaff7f0b 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 4:52bffaff7f0b 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 4:52bffaff7f0b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 4:52bffaff7f0b 13 * See the License for the specific language governing permissions and
mbed_official 4:52bffaff7f0b 14 * limitations under the License.
mbed_official 4:52bffaff7f0b 15 */
mbed_official 4:52bffaff7f0b 16
mbed_official 13:eaa1343657a8 17 #include <events/mbed_events.h>
mbed_official 4:52bffaff7f0b 18 #include <mbed.h>
mbed_official 4:52bffaff7f0b 19 #include "ble/BLE.h"
mbed_official 4:52bffaff7f0b 20 #include "ble/Gap.h"
mbed_official 4:52bffaff7f0b 21 #include "ble/services/BatteryService.h"
vazbyte 74:923be844d0fe 22 #include "ble/services/HeartRateService.h"
mbed_official 4:52bffaff7f0b 23
mbed_official 4:52bffaff7f0b 24 DigitalOut led1(LED1, 1);
mbed_official 4:52bffaff7f0b 25
vazbyte 73:4bce85f91e5a 26 const static char DEVICE_NAME[] = "ProVaida";
vazbyte 74:923be844d0fe 27 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
vazbyte 74:923be844d0fe 28 //static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
mbed_official 4:52bffaff7f0b 29
vazbyte 74:923be844d0fe 30 uint8_t hrmCounter = 100;
vazbyte 74:923be844d0fe 31 static HeartRateService* hrService;
mbed_official 4:52bffaff7f0b 32
mbed_official 29:7a6aff8c9468 33 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
mbed_official 4:52bffaff7f0b 34
mbed_official 4:52bffaff7f0b 35 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
mbed_official 4:52bffaff7f0b 36 {
mbed_official 4:52bffaff7f0b 37 BLE::Instance().gap().startAdvertising();
mbed_official 4:52bffaff7f0b 38 }
mbed_official 4:52bffaff7f0b 39
mbed_official 4:52bffaff7f0b 40 void updateSensorValue() {
vazbyte 74:923be844d0fe 41 hrmCounter++;
vazbyte 74:923be844d0fe 42 if (hrmCounter > 175) {
vazbyte 74:923be844d0fe 43 hrmCounter = 100;
mbed_official 4:52bffaff7f0b 44 }
vazbyte 74:923be844d0fe 45 hrService->updateHeartRate(hrmCounter);
vazbyte 74:923be844d0fe 46 //batteryServicePtr->updateBatteryLevel(batteryLevel);
mbed_official 4:52bffaff7f0b 47 }
mbed_official 4:52bffaff7f0b 48
mbed_official 4:52bffaff7f0b 49 void blinkCallback(void)
mbed_official 4:52bffaff7f0b 50 {
mbed_official 4:52bffaff7f0b 51 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
mbed_official 4:52bffaff7f0b 52
mbed_official 4:52bffaff7f0b 53 BLE &ble = BLE::Instance();
mbed_official 4:52bffaff7f0b 54 if (ble.gap().getState().connected) {
mbed_official 13:eaa1343657a8 55 eventQueue.call(updateSensorValue);
mbed_official 4:52bffaff7f0b 56 }
mbed_official 4:52bffaff7f0b 57 }
mbed_official 4:52bffaff7f0b 58
mbed_official 4:52bffaff7f0b 59 /**
vazbyte 74:923be844d0fe 60 * This function is called when the ble initialization process has failed
mbed_official 4:52bffaff7f0b 61 */
mbed_official 4:52bffaff7f0b 62 void onBleInitError(BLE &ble, ble_error_t error)
mbed_official 4:52bffaff7f0b 63 {
mbed_official 4:52bffaff7f0b 64 /* Initialization error handling should go here */
mbed_official 4:52bffaff7f0b 65 }
mbed_official 4:52bffaff7f0b 66
mbed_official 46:6b66d08f304e 67 void printMacAddress()
mbed_official 46:6b66d08f304e 68 {
mbed_official 46:6b66d08f304e 69 /* Print out device MAC address to the console*/
mbed_official 46:6b66d08f304e 70 Gap::AddressType_t addr_type;
mbed_official 46:6b66d08f304e 71 Gap::Address_t address;
mbed_official 46:6b66d08f304e 72 BLE::Instance().gap().getAddress(&addr_type, address);
mbed_official 46:6b66d08f304e 73 printf("DEVICE MAC ADDRESS: ");
mbed_official 46:6b66d08f304e 74 for (int i = 5; i >= 1; i--){
mbed_official 46:6b66d08f304e 75 printf("%02x:", address[i]);
mbed_official 46:6b66d08f304e 76 }
mbed_official 46:6b66d08f304e 77 printf("%02x\r\n", address[0]);
mbed_official 46:6b66d08f304e 78 }
mbed_official 46:6b66d08f304e 79
mbed_official 4:52bffaff7f0b 80 /**
mbed_official 4:52bffaff7f0b 81 * Callback triggered when the ble initialization process has finished
mbed_official 4:52bffaff7f0b 82 */
mbed_official 4:52bffaff7f0b 83 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
mbed_official 4:52bffaff7f0b 84 {
mbed_official 4:52bffaff7f0b 85 BLE& ble = params->ble;
mbed_official 4:52bffaff7f0b 86 ble_error_t error = params->error;
mbed_official 4:52bffaff7f0b 87
mbed_official 4:52bffaff7f0b 88 if (error != BLE_ERROR_NONE) {
mbed_official 4:52bffaff7f0b 89 /* In case of error, forward the error handling to onBleInitError */
mbed_official 4:52bffaff7f0b 90 onBleInitError(ble, error);
mbed_official 4:52bffaff7f0b 91 return;
mbed_official 4:52bffaff7f0b 92 }
mbed_official 4:52bffaff7f0b 93
mbed_official 4:52bffaff7f0b 94 /* Ensure that it is the default instance of BLE */
mbed_official 4:52bffaff7f0b 95 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
mbed_official 4:52bffaff7f0b 96 return;
mbed_official 4:52bffaff7f0b 97 }
mbed_official 4:52bffaff7f0b 98
mbed_official 4:52bffaff7f0b 99 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 4:52bffaff7f0b 100
mbed_official 4:52bffaff7f0b 101 /* Setup primary service */
vazbyte 76:9a6403ea5c9b 102 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
mbed_official 4:52bffaff7f0b 103
mbed_official 4:52bffaff7f0b 104 /* Setup advertising */
mbed_official 4:52bffaff7f0b 105 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
mbed_official 4:52bffaff7f0b 106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
mbed_official 4:52bffaff7f0b 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
mbed_official 4:52bffaff7f0b 108 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
mbed_official 4:52bffaff7f0b 109 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
mbed_official 4:52bffaff7f0b 110 ble.gap().startAdvertising();
mbed_official 46:6b66d08f304e 111
mbed_official 46:6b66d08f304e 112 printMacAddress();
mbed_official 4:52bffaff7f0b 113 }
mbed_official 4:52bffaff7f0b 114
mbed_official 4:52bffaff7f0b 115 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 4:52bffaff7f0b 116 BLE &ble = BLE::Instance();
mbed_official 13:eaa1343657a8 117 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 4:52bffaff7f0b 118 }
mbed_official 4:52bffaff7f0b 119
mbed_official 4:52bffaff7f0b 120 int main()
mbed_official 4:52bffaff7f0b 121 {
mbed_official 13:eaa1343657a8 122 eventQueue.call_every(500, blinkCallback);
mbed_official 4:52bffaff7f0b 123
mbed_official 4:52bffaff7f0b 124 BLE &ble = BLE::Instance();
mbed_official 4:52bffaff7f0b 125 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 4:52bffaff7f0b 126 ble.init(bleInitComplete);
mbed_official 4:52bffaff7f0b 127
mbed_official 13:eaa1343657a8 128 eventQueue.dispatch_forever();
mbed_official 4:52bffaff7f0b 129
mbed_official 4:52bffaff7f0b 130 return 0;
mbed_official 4:52bffaff7f0b 131 }