![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
BLE
Dependencies: ADT7410 BLE_API mbed nRF51822
See https://developer.mbed.org/users/yasuyuki/notebook/BLE/
main.cpp@0:1dbfda43d8b4, 2015-06-22 (annotated)
- Committer:
- yasuyuki
- Date:
- Mon Jun 22 22:01:52 2015 +0000
- Revision:
- 0:1dbfda43d8b4
- Child:
- 1:6458709fb459
release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasuyuki | 0:1dbfda43d8b4 | 1 | //********************** |
yasuyuki | 0:1dbfda43d8b4 | 2 | // BLE demo for mbed_HRM1017 |
yasuyuki | 0:1dbfda43d8b4 | 3 | // |
yasuyuki | 0:1dbfda43d8b4 | 4 | // (C)Copyright 2015 All rights reserved by Y.Onodera |
yasuyuki | 0:1dbfda43d8b4 | 5 | // http://einstlab.web.fc2.com |
yasuyuki | 0:1dbfda43d8b4 | 6 | //********************** |
yasuyuki | 0:1dbfda43d8b4 | 7 | #if defined(TARGET_HRM1017) |
yasuyuki | 0:1dbfda43d8b4 | 8 | #include "mbed.h" |
yasuyuki | 0:1dbfda43d8b4 | 9 | #include "ADT7410.h" |
yasuyuki | 0:1dbfda43d8b4 | 10 | #include "BLEDevice.h" // BLEライブラリのヘッダ |
yasuyuki | 0:1dbfda43d8b4 | 11 | #include "BatteryService.h" // BatteryServiceのヘッダ |
yasuyuki | 0:1dbfda43d8b4 | 12 | #include "HeartRateService.h" // HeartRateServiceのヘッダ |
yasuyuki | 0:1dbfda43d8b4 | 13 | #include "HealthThermometerService.h" // HealthThermometerServiceのヘッダ |
yasuyuki | 0:1dbfda43d8b4 | 14 | |
yasuyuki | 0:1dbfda43d8b4 | 15 | #define THERMO |
yasuyuki | 0:1dbfda43d8b4 | 16 | //#define HEART |
yasuyuki | 0:1dbfda43d8b4 | 17 | //#define BATTERY |
yasuyuki | 0:1dbfda43d8b4 | 18 | |
yasuyuki | 0:1dbfda43d8b4 | 19 | BLEDevice ble; // BLEのインスタンス生成 |
yasuyuki | 0:1dbfda43d8b4 | 20 | DigitalOut led1(LED1); |
yasuyuki | 0:1dbfda43d8b4 | 21 | DigitalOut led2(LED2); |
yasuyuki | 0:1dbfda43d8b4 | 22 | DigitalOut sw(P0_0); |
yasuyuki | 0:1dbfda43d8b4 | 23 | Ticker tick; |
yasuyuki | 0:1dbfda43d8b4 | 24 | #ifdef THERMO |
yasuyuki | 0:1dbfda43d8b4 | 25 | I2C i2c(P0_22,P0_20); |
yasuyuki | 0:1dbfda43d8b4 | 26 | ADT7410 temperature(i2c); |
yasuyuki | 0:1dbfda43d8b4 | 27 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 28 | |
yasuyuki | 0:1dbfda43d8b4 | 29 | #define DEVICE_NAME "mbed_BLE" |
yasuyuki | 0:1dbfda43d8b4 | 30 | // UUIDリスト |
yasuyuki | 0:1dbfda43d8b4 | 31 | #ifdef THERMO |
yasuyuki | 0:1dbfda43d8b4 | 32 | uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE, GattService::UUID_BATTERY_SERVICE}; |
yasuyuki | 0:1dbfda43d8b4 | 33 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 34 | #ifdef HEART |
yasuyuki | 0:1dbfda43d8b4 | 35 | uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_BATTERY_SERVICE}; |
yasuyuki | 0:1dbfda43d8b4 | 36 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 37 | #ifdef BATTERY |
yasuyuki | 0:1dbfda43d8b4 | 38 | uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; |
yasuyuki | 0:1dbfda43d8b4 | 39 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 40 | bool tickFlag = false; |
yasuyuki | 0:1dbfda43d8b4 | 41 | |
yasuyuki | 0:1dbfda43d8b4 | 42 | |
yasuyuki | 0:1dbfda43d8b4 | 43 | // インターバルタイマー |
yasuyuki | 0:1dbfda43d8b4 | 44 | void tickerCallback(void) |
yasuyuki | 0:1dbfda43d8b4 | 45 | { |
yasuyuki | 0:1dbfda43d8b4 | 46 | led1 = !led1; |
yasuyuki | 0:1dbfda43d8b4 | 47 | tickFlag = true; |
yasuyuki | 0:1dbfda43d8b4 | 48 | } |
yasuyuki | 0:1dbfda43d8b4 | 49 | |
yasuyuki | 0:1dbfda43d8b4 | 50 | |
yasuyuki | 0:1dbfda43d8b4 | 51 | // BLE接続処理 |
yasuyuki | 0:1dbfda43d8b4 | 52 | #define OLD |
yasuyuki | 0:1dbfda43d8b4 | 53 | #ifdef OLD |
yasuyuki | 0:1dbfda43d8b4 | 54 | // old version BLE_API until rev.341 |
yasuyuki | 0:1dbfda43d8b4 | 55 | void connectionCallback( |
yasuyuki | 0:1dbfda43d8b4 | 56 | Gap::Handle_t handle, |
yasuyuki | 0:1dbfda43d8b4 | 57 | Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, |
yasuyuki | 0:1dbfda43d8b4 | 58 | const Gap::ConnectionParams_t *params) |
yasuyuki | 0:1dbfda43d8b4 | 59 | #else |
yasuyuki | 0:1dbfda43d8b4 | 60 | // new version |
yasuyuki | 0:1dbfda43d8b4 | 61 | void connectionCallback( |
yasuyuki | 0:1dbfda43d8b4 | 62 | Gap::Handle_t handle, |
yasuyuki | 0:1dbfda43d8b4 | 63 | Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, |
yasuyuki | 0:1dbfda43d8b4 | 64 | Gap::addr_type_t ownAddrType, const Gap::address_t ownAddr, |
yasuyuki | 0:1dbfda43d8b4 | 65 | const Gap::ConnectionParams_t *params) |
yasuyuki | 0:1dbfda43d8b4 | 66 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 67 | { |
yasuyuki | 0:1dbfda43d8b4 | 68 | led2 = 1; |
yasuyuki | 0:1dbfda43d8b4 | 69 | ble.stopAdvertising(); |
yasuyuki | 0:1dbfda43d8b4 | 70 | } |
yasuyuki | 0:1dbfda43d8b4 | 71 | |
yasuyuki | 0:1dbfda43d8b4 | 72 | |
yasuyuki | 0:1dbfda43d8b4 | 73 | // BLE切断処理 |
yasuyuki | 0:1dbfda43d8b4 | 74 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yasuyuki | 0:1dbfda43d8b4 | 75 | { |
yasuyuki | 0:1dbfda43d8b4 | 76 | led2 = 0; |
yasuyuki | 0:1dbfda43d8b4 | 77 | ble.startAdvertising(); |
yasuyuki | 0:1dbfda43d8b4 | 78 | } |
yasuyuki | 0:1dbfda43d8b4 | 79 | |
yasuyuki | 0:1dbfda43d8b4 | 80 | |
yasuyuki | 0:1dbfda43d8b4 | 81 | // BLE接続タイムアウト処理 |
yasuyuki | 0:1dbfda43d8b4 | 82 | void connectTimeoutCallback(void) |
yasuyuki | 0:1dbfda43d8b4 | 83 | { |
yasuyuki | 0:1dbfda43d8b4 | 84 | // to do |
yasuyuki | 0:1dbfda43d8b4 | 85 | // Sleep |
yasuyuki | 0:1dbfda43d8b4 | 86 | // __WFE(); |
yasuyuki | 0:1dbfda43d8b4 | 87 | // __SEV(); |
yasuyuki | 0:1dbfda43d8b4 | 88 | // __WFE(); |
yasuyuki | 0:1dbfda43d8b4 | 89 | // System-off |
yasuyuki | 0:1dbfda43d8b4 | 90 | // NRF_POWER->SYSTEMOFF = 1; |
yasuyuki | 0:1dbfda43d8b4 | 91 | } |
yasuyuki | 0:1dbfda43d8b4 | 92 | |
yasuyuki | 0:1dbfda43d8b4 | 93 | |
yasuyuki | 0:1dbfda43d8b4 | 94 | void initAdvertising(void) |
yasuyuki | 0:1dbfda43d8b4 | 95 | { |
yasuyuki | 0:1dbfda43d8b4 | 96 | |
yasuyuki | 0:1dbfda43d8b4 | 97 | // フラグを設定 |
yasuyuki | 0:1dbfda43d8b4 | 98 | // LE_LIMITED_DISCOVERABLE = 期間限定で検出可能デバイス |
yasuyuki | 0:1dbfda43d8b4 | 99 | // LE_GENERAL_DISCOVERABLE = 検出可能デバイス |
yasuyuki | 0:1dbfda43d8b4 | 100 | // BREDR_NOT_SUPPORTED = Bluetooth Classicは未サポート, BLEオンリー |
yasuyuki | 0:1dbfda43d8b4 | 101 | ble.accumulateAdvertisingPayload( |
yasuyuki | 0:1dbfda43d8b4 | 102 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED); |
yasuyuki | 0:1dbfda43d8b4 | 103 | // UUIDリストを設定 |
yasuyuki | 0:1dbfda43d8b4 | 104 | ble.accumulateAdvertisingPayload( |
yasuyuki | 0:1dbfda43d8b4 | 105 | GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
yasuyuki | 0:1dbfda43d8b4 | 106 | (uint8_t *)uuid16_list, |
yasuyuki | 0:1dbfda43d8b4 | 107 | sizeof(uuid16_list)); |
yasuyuki | 0:1dbfda43d8b4 | 108 | // デバイス名を設定(for GAP), default Device Name="nRF5x" |
yasuyuki | 0:1dbfda43d8b4 | 109 | // ble.setDeviceName(DEVICE_NAME); |
yasuyuki | 0:1dbfda43d8b4 | 110 | // デバイス名を設定(for Advertising) |
yasuyuki | 0:1dbfda43d8b4 | 111 | ble.accumulateAdvertisingPayload( |
yasuyuki | 0:1dbfda43d8b4 | 112 | GapAdvertisingData::COMPLETE_LOCAL_NAME, |
yasuyuki | 0:1dbfda43d8b4 | 113 | (const uint8_t *)DEVICE_NAME, |
yasuyuki | 0:1dbfda43d8b4 | 114 | // strlen(DEVICE_NAME)); |
yasuyuki | 0:1dbfda43d8b4 | 115 | sizeof(DEVICE_NAME)); |
yasuyuki | 0:1dbfda43d8b4 | 116 | |
yasuyuki | 0:1dbfda43d8b4 | 117 | // ADV_CONNECTABLE_UNDIRECTED = 接続可能デバイスに設定 |
yasuyuki | 0:1dbfda43d8b4 | 118 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
yasuyuki | 0:1dbfda43d8b4 | 119 | |
yasuyuki | 0:1dbfda43d8b4 | 120 | // アドバタイジング間隔の設定=100ms, 0.625ms単位 |
yasuyuki | 0:1dbfda43d8b4 | 121 | ble.setAdvertisingInterval(160); // 0.625ms * 160 = 100ms |
yasuyuki | 0:1dbfda43d8b4 | 122 | // ble.setAdvertisingInterval(1600); // 0.625ms * 1600 = 1000ms |
yasuyuki | 0:1dbfda43d8b4 | 123 | |
yasuyuki | 0:1dbfda43d8b4 | 124 | // Appearance |
yasuyuki | 0:1dbfda43d8b4 | 125 | #ifdef THERMO |
yasuyuki | 0:1dbfda43d8b4 | 126 | ble.accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
yasuyuki | 0:1dbfda43d8b4 | 127 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 128 | #ifdef HEART |
yasuyuki | 0:1dbfda43d8b4 | 129 | // ble.setAppearance(ENERIC_HEART_RATE_SENSOR); |
yasuyuki | 0:1dbfda43d8b4 | 130 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
yasuyuki | 0:1dbfda43d8b4 | 131 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 132 | // アドバタイジングタイムアウト=180sec(デフォルトは0でタイムアウトなし) |
yasuyuki | 0:1dbfda43d8b4 | 133 | // ble.setAdvertisingTimeout(180); |
yasuyuki | 0:1dbfda43d8b4 | 134 | // アドバタイジング開始 |
yasuyuki | 0:1dbfda43d8b4 | 135 | ble.startAdvertising(); |
yasuyuki | 0:1dbfda43d8b4 | 136 | |
yasuyuki | 0:1dbfda43d8b4 | 137 | } |
yasuyuki | 0:1dbfda43d8b4 | 138 | |
yasuyuki | 0:1dbfda43d8b4 | 139 | |
yasuyuki | 0:1dbfda43d8b4 | 140 | int main(void) |
yasuyuki | 0:1dbfda43d8b4 | 141 | { |
yasuyuki | 0:1dbfda43d8b4 | 142 | led1 = 0; |
yasuyuki | 0:1dbfda43d8b4 | 143 | led2 = 0; |
yasuyuki | 0:1dbfda43d8b4 | 144 | sw = 1; // off |
yasuyuki | 0:1dbfda43d8b4 | 145 | |
yasuyuki | 0:1dbfda43d8b4 | 146 | ble.init(); // BLE(nRF51822)の初期化 |
yasuyuki | 0:1dbfda43d8b4 | 147 | // Valid values are -40, -20, -16, -12, -8, -4, 0, 4) |
yasuyuki | 0:1dbfda43d8b4 | 148 | if(ble.setTxPower(-40)!=BLE_ERROR_NONE) |
yasuyuki | 0:1dbfda43d8b4 | 149 | led2 = 1; |
yasuyuki | 0:1dbfda43d8b4 | 150 | |
yasuyuki | 0:1dbfda43d8b4 | 151 | // バッテリ残量のイベント関数を設定 |
yasuyuki | 0:1dbfda43d8b4 | 152 | tick.attach(&tickerCallback, 1); |
yasuyuki | 0:1dbfda43d8b4 | 153 | |
yasuyuki | 0:1dbfda43d8b4 | 154 | // BLEのイベント関数を設定 |
yasuyuki | 0:1dbfda43d8b4 | 155 | ble.onConnection(connectionCallback); |
yasuyuki | 0:1dbfda43d8b4 | 156 | ble.onDisconnection(disconnectionCallback); |
yasuyuki | 0:1dbfda43d8b4 | 157 | ble.onTimeout(connectTimeoutCallback); |
yasuyuki | 0:1dbfda43d8b4 | 158 | |
yasuyuki | 0:1dbfda43d8b4 | 159 | // BatteryServiceのインスタンス生成=サービスを登録 |
yasuyuki | 0:1dbfda43d8b4 | 160 | uint8_t batt=0; |
yasuyuki | 0:1dbfda43d8b4 | 161 | BatteryService bs(ble); |
yasuyuki | 0:1dbfda43d8b4 | 162 | |
yasuyuki | 0:1dbfda43d8b4 | 163 | #ifdef THERMO |
yasuyuki | 0:1dbfda43d8b4 | 164 | // HeathTermometerServiceのインスタンス生成=サービスを登録 |
yasuyuki | 0:1dbfda43d8b4 | 165 | float currentTemperature = 0.0; |
yasuyuki | 0:1dbfda43d8b4 | 166 | HealthThermometerService ts(ble, currentTemperature, HealthThermometerService::LOCATION_EAR); |
yasuyuki | 0:1dbfda43d8b4 | 167 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 168 | #ifdef HEART |
yasuyuki | 0:1dbfda43d8b4 | 169 | // HeartRateServiceのインスタンス生成=サービスを登録 |
yasuyuki | 0:1dbfda43d8b4 | 170 | uint8_t hrmCounter = 100; |
yasuyuki | 0:1dbfda43d8b4 | 171 | HeartRateService hr(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
yasuyuki | 0:1dbfda43d8b4 | 172 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 173 | |
yasuyuki | 0:1dbfda43d8b4 | 174 | // アドバタイジングの設定 |
yasuyuki | 0:1dbfda43d8b4 | 175 | initAdvertising(); |
yasuyuki | 0:1dbfda43d8b4 | 176 | |
yasuyuki | 0:1dbfda43d8b4 | 177 | // イベント待ちループ |
yasuyuki | 0:1dbfda43d8b4 | 178 | while(true) { |
yasuyuki | 0:1dbfda43d8b4 | 179 | ble.waitForEvent(); |
yasuyuki | 0:1dbfda43d8b4 | 180 | if(tickFlag){ |
yasuyuki | 0:1dbfda43d8b4 | 181 | // バッテリ残量の取り込み |
yasuyuki | 0:1dbfda43d8b4 | 182 | if(++batt>100)batt=0; |
yasuyuki | 0:1dbfda43d8b4 | 183 | // バッテリ残量のデータ更新 |
yasuyuki | 0:1dbfda43d8b4 | 184 | bs.updateBatteryLevel(batt); |
yasuyuki | 0:1dbfda43d8b4 | 185 | #ifdef THERMO |
yasuyuki | 0:1dbfda43d8b4 | 186 | sw = 0; // on |
yasuyuki | 0:1dbfda43d8b4 | 187 | // currentTemperature-=0.1; |
yasuyuki | 0:1dbfda43d8b4 | 188 | wait_ms(100); |
yasuyuki | 0:1dbfda43d8b4 | 189 | currentTemperature=temperature.value()/128.0; |
yasuyuki | 0:1dbfda43d8b4 | 190 | ts.updateTemperature(currentTemperature); |
yasuyuki | 0:1dbfda43d8b4 | 191 | sw = 1; // off |
yasuyuki | 0:1dbfda43d8b4 | 192 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 193 | #ifdef HEART |
yasuyuki | 0:1dbfda43d8b4 | 194 | hr.updateHeartRate(hrmCounter); |
yasuyuki | 0:1dbfda43d8b4 | 195 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 196 | tickFlag = false; |
yasuyuki | 0:1dbfda43d8b4 | 197 | } |
yasuyuki | 0:1dbfda43d8b4 | 198 | } |
yasuyuki | 0:1dbfda43d8b4 | 199 | } |
yasuyuki | 0:1dbfda43d8b4 | 200 | |
yasuyuki | 0:1dbfda43d8b4 | 201 | #endif |
yasuyuki | 0:1dbfda43d8b4 | 202 |