Heart Rate program only for TYBLE16

Dependencies:   TYBLE16_BASE

Please refer flowing link.
/users/kenjiArai/notebook/tyble16-module-will-become-a-mbed-family--mbedliza/

Committer:
kenjiArai
Date:
Sun Feb 25 03:41:33 2018 +0000
Revision:
1:ba83bf091a45
Parent:
0:81403b36fabe
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:81403b36fabe 1 /* mbed Microcontroller Library
kenjiArai 0:81403b36fabe 2 * Copyright (c) 2006-2015 ARM Limited
kenjiArai 0:81403b36fabe 3 *
kenjiArai 0:81403b36fabe 4 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 0:81403b36fabe 5 * you may not use this file except in compliance with the License.
kenjiArai 0:81403b36fabe 6 * You may obtain a copy of the License at
kenjiArai 0:81403b36fabe 7 *
kenjiArai 0:81403b36fabe 8 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:81403b36fabe 9 *
kenjiArai 0:81403b36fabe 10 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:81403b36fabe 11 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 0:81403b36fabe 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:81403b36fabe 13 * See the License for the specific language governing permissions and
kenjiArai 0:81403b36fabe 14 * limitations under the License.
kenjiArai 0:81403b36fabe 15 */
kenjiArai 0:81403b36fabe 16 /*
kenjiArai 0:81403b36fabe 17 Modified by Kenji Arai, Feburary 25th, 2018
kenjiArai 0:81403b36fabe 18 */
kenjiArai 0:81403b36fabe 19
kenjiArai 0:81403b36fabe 20 #include "mbed.h"
kenjiArai 0:81403b36fabe 21 #include "TYBLE16_BASE.h"
kenjiArai 0:81403b36fabe 22 #include "ble/BLE.h"
kenjiArai 0:81403b36fabe 23 #include "ble/services/HeartRateService.h"
kenjiArai 0:81403b36fabe 24 #include "ble/services/BatteryService.h"
kenjiArai 0:81403b36fabe 25 #include "ble/services/DeviceInformationService.h"
kenjiArai 0:81403b36fabe 26
kenjiArai 0:81403b36fabe 27 DigitalOut led1(P0_5);
kenjiArai 0:81403b36fabe 28 Serial pc(P0_1, P0_3);
kenjiArai 0:81403b36fabe 29
kenjiArai 0:81403b36fabe 30 const static char DEVICE_NAME[] = "TYBLE16";
kenjiArai 0:81403b36fabe 31
kenjiArai 0:81403b36fabe 32 static const uint16_t uuid16_list[] =
kenjiArai 0:81403b36fabe 33 {GattService::UUID_HEART_RATE_SERVICE,
kenjiArai 0:81403b36fabe 34 GattService::UUID_DEVICE_INFORMATION_SERVICE};
kenjiArai 0:81403b36fabe 35
kenjiArai 0:81403b36fabe 36 static volatile bool triggerSensorPolling = false;
kenjiArai 0:81403b36fabe 37
kenjiArai 0:81403b36fabe 38 uint8_t hrmCounter = 100; // init HRM to 100bps
kenjiArai 0:81403b36fabe 39
kenjiArai 0:81403b36fabe 40 HeartRateService *hrService;
kenjiArai 0:81403b36fabe 41 DeviceInformationService *deviceInfo;
kenjiArai 0:81403b36fabe 42
kenjiArai 0:81403b36fabe 43 void disconnectionCallback(
kenjiArai 0:81403b36fabe 44 const Gap::DisconnectionCallbackParams_t *params
kenjiArai 0:81403b36fabe 45 )
kenjiArai 0:81403b36fabe 46 {
kenjiArai 0:81403b36fabe 47 // restart advertising
kenjiArai 0:81403b36fabe 48 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
kenjiArai 0:81403b36fabe 49 }
kenjiArai 0:81403b36fabe 50
kenjiArai 0:81403b36fabe 51 void periodicCallback(void)
kenjiArai 0:81403b36fabe 52 {
kenjiArai 0:81403b36fabe 53 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
kenjiArai 0:81403b36fabe 54
kenjiArai 0:81403b36fabe 55 /* Note that the periodicCallback() executes in interrupt context,
kenjiArai 0:81403b36fabe 56 * so it is safer to do
kenjiArai 0:81403b36fabe 57 * heavy-weight sensor polling from the main thread. */
kenjiArai 0:81403b36fabe 58 triggerSensorPolling = true;
kenjiArai 0:81403b36fabe 59 }
kenjiArai 0:81403b36fabe 60
kenjiArai 0:81403b36fabe 61 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
kenjiArai 0:81403b36fabe 62 {
kenjiArai 0:81403b36fabe 63 BLE &ble = params->ble;
kenjiArai 0:81403b36fabe 64 ble_error_t error = params->error;
kenjiArai 0:81403b36fabe 65
kenjiArai 0:81403b36fabe 66 if (error != BLE_ERROR_NONE) {
kenjiArai 0:81403b36fabe 67 return;
kenjiArai 0:81403b36fabe 68 }
kenjiArai 0:81403b36fabe 69
kenjiArai 0:81403b36fabe 70 ble.gap().onDisconnection(disconnectionCallback);
kenjiArai 0:81403b36fabe 71
kenjiArai 0:81403b36fabe 72 /* Setup primary service. */
kenjiArai 0:81403b36fabe 73 hrService = new HeartRateService(
kenjiArai 0:81403b36fabe 74 ble,
kenjiArai 0:81403b36fabe 75 hrmCounter,
kenjiArai 0:81403b36fabe 76 HeartRateService::LOCATION_FINGER
kenjiArai 0:81403b36fabe 77 );
kenjiArai 0:81403b36fabe 78
kenjiArai 0:81403b36fabe 79 /* Setup auxiliary service. */
kenjiArai 0:81403b36fabe 80 deviceInfo = new DeviceInformationService(
kenjiArai 0:81403b36fabe 81 ble,
kenjiArai 0:81403b36fabe 82 "ARM",
kenjiArai 0:81403b36fabe 83 "Model1",
kenjiArai 0:81403b36fabe 84 "SN1",
kenjiArai 0:81403b36fabe 85 "hw-rev1",
kenjiArai 0:81403b36fabe 86 "fw-rev1",
kenjiArai 0:81403b36fabe 87 "soft-rev1"
kenjiArai 0:81403b36fabe 88 );
kenjiArai 0:81403b36fabe 89
kenjiArai 0:81403b36fabe 90 /* Setup advertising. */
kenjiArai 0:81403b36fabe 91 ble.gap().accumulateAdvertisingPayload(
kenjiArai 0:81403b36fabe 92 GapAdvertisingData::BREDR_NOT_SUPPORTED |
kenjiArai 0:81403b36fabe 93 GapAdvertisingData::LE_GENERAL_DISCOVERABLE
kenjiArai 0:81403b36fabe 94 );
kenjiArai 0:81403b36fabe 95 ble.gap().accumulateAdvertisingPayload(
kenjiArai 0:81403b36fabe 96 GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
kenjiArai 0:81403b36fabe 97 (uint8_t *)uuid16_list, sizeof(uuid16_list)
kenjiArai 0:81403b36fabe 98 );
kenjiArai 0:81403b36fabe 99 ble.gap().accumulateAdvertisingPayload(
kenjiArai 0:81403b36fabe 100 GapAdvertisingData::GENERIC_HEART_RATE_SENSOR
kenjiArai 0:81403b36fabe 101 );
kenjiArai 0:81403b36fabe 102 ble.gap().accumulateAdvertisingPayload(
kenjiArai 0:81403b36fabe 103 GapAdvertisingData::COMPLETE_LOCAL_NAME,
kenjiArai 0:81403b36fabe 104 (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)
kenjiArai 0:81403b36fabe 105 );
kenjiArai 0:81403b36fabe 106 ble.gap().setAdvertisingType(
kenjiArai 0:81403b36fabe 107 GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED
kenjiArai 0:81403b36fabe 108 );
kenjiArai 0:81403b36fabe 109 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
kenjiArai 0:81403b36fabe 110 ble.gap().startAdvertising();
kenjiArai 0:81403b36fabe 111 }
kenjiArai 0:81403b36fabe 112
kenjiArai 0:81403b36fabe 113 int main(void)
kenjiArai 0:81403b36fabe 114 {
kenjiArai 0:81403b36fabe 115 led1 = 1;
kenjiArai 0:81403b36fabe 116 Ticker ticker;
kenjiArai 0:81403b36fabe 117 ticker.attach(periodicCallback, 1); // blink LED every second
kenjiArai 0:81403b36fabe 118
kenjiArai 0:81403b36fabe 119 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
kenjiArai 0:81403b36fabe 120 ble.init(bleInitComplete);
kenjiArai 0:81403b36fabe 121
kenjiArai 0:81403b36fabe 122 /* SpinWait for initialization to complete. This is necessary
kenjiArai 0:81403b36fabe 123 * because the BLE object is used in the main loop below. */
kenjiArai 0:81403b36fabe 124 while (ble.hasInitialized() == false) { /* spin loop */ }
kenjiArai 0:81403b36fabe 125 // Check TYBLE-16 configuration
kenjiArai 0:81403b36fabe 126 cpu_sys();
kenjiArai 1:ba83bf091a45 127 if (compile_condition() == false) {
kenjiArai 0:81403b36fabe 128 pc.printf("This is wrong configuration!!\r\n");
kenjiArai 0:81403b36fabe 129 while(true) {
kenjiArai 0:81403b36fabe 130 led1 = !led1;
kenjiArai 0:81403b36fabe 131 wait(0.2);
kenjiArai 0:81403b36fabe 132 }
kenjiArai 0:81403b36fabe 133 }
kenjiArai 0:81403b36fabe 134 //
kenjiArai 0:81403b36fabe 135 // infinite loop
kenjiArai 0:81403b36fabe 136 while (true) {
kenjiArai 0:81403b36fabe 137 // check for trigger from periodicCallback()
kenjiArai 0:81403b36fabe 138 if (triggerSensorPolling && ble.getGapState().connected) {
kenjiArai 0:81403b36fabe 139 triggerSensorPolling = false;
kenjiArai 0:81403b36fabe 140
kenjiArai 0:81403b36fabe 141 // Do blocking calls or whatever is necessary for sensor polling.
kenjiArai 0:81403b36fabe 142 // In our case, we simply update the HRM measurement.
kenjiArai 0:81403b36fabe 143 hrmCounter++;
kenjiArai 0:81403b36fabe 144 if (hrmCounter == 175) { // 100 <= HRM bps <=175
kenjiArai 0:81403b36fabe 145 hrmCounter = 100;
kenjiArai 0:81403b36fabe 146 }
kenjiArai 0:81403b36fabe 147
kenjiArai 0:81403b36fabe 148 hrService->updateHeartRate(hrmCounter);
kenjiArai 0:81403b36fabe 149 } else {
kenjiArai 0:81403b36fabe 150 ble.waitForEvent(); // low power wait for event
kenjiArai 0:81403b36fabe 151 }
kenjiArai 0:81403b36fabe 152 }
kenjiArai 0:81403b36fabe 153 }