Kenji Arai / TYBLE16_mbedlized_HeartRate

Dependencies:   TYBLE16_BASE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 /*
00017     Modified by Kenji Arai, Feburary 25th, 2018
00018  */
00019 
00020 #include "mbed.h"
00021 #include "TYBLE16_BASE.h"
00022 #include "ble/BLE.h"
00023 #include "ble/services/HeartRateService.h"
00024 #include "ble/services/BatteryService.h"
00025 #include "ble/services/DeviceInformationService.h"
00026 
00027 DigitalOut  led1(P0_5);
00028 Serial      pc(P0_1, P0_3);
00029 
00030 const static char     DEVICE_NAME[] = "TYBLE16";
00031 
00032 static const uint16_t uuid16_list[] =
00033                             {GattService::UUID_HEART_RATE_SERVICE,
00034                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
00035 
00036 static volatile bool  triggerSensorPolling = false;
00037 
00038 uint8_t hrmCounter = 100; // init HRM to 100bps
00039 
00040 HeartRateService         *hrService;
00041 DeviceInformationService *deviceInfo;
00042 
00043 void disconnectionCallback(
00044     const Gap::DisconnectionCallbackParams_t *params
00045 )
00046 {
00047     // restart advertising
00048     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
00049 }
00050 
00051 void periodicCallback(void)
00052 {
00053     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00054     
00055     /* Note that the periodicCallback() executes in interrupt context,
00056      * so it is safer to do
00057      * heavy-weight sensor polling from the main thread. */
00058     triggerSensorPolling = true;
00059 }
00060 
00061 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00062 {
00063     BLE &ble          = params->ble;
00064     ble_error_t error = params->error;
00065 
00066     if (error != BLE_ERROR_NONE) {
00067         return;
00068     }
00069 
00070     ble.gap().onDisconnection(disconnectionCallback);
00071 
00072     /* Setup primary service. */
00073     hrService = new HeartRateService(
00074                                      ble,
00075                                      hrmCounter,
00076                                      HeartRateService::LOCATION_FINGER
00077                                      );
00078 
00079     /* Setup auxiliary service. */
00080     deviceInfo = new DeviceInformationService(
00081                                      ble,
00082                                      "ARM",
00083                                      "Model1",
00084                                      "SN1",
00085                                      "hw-rev1",
00086                                      "fw-rev1",
00087                                      "soft-rev1"
00088                                      );
00089 
00090     /* Setup advertising. */
00091     ble.gap().accumulateAdvertisingPayload(
00092         GapAdvertisingData::BREDR_NOT_SUPPORTED |
00093         GapAdvertisingData::LE_GENERAL_DISCOVERABLE
00094     );
00095     ble.gap().accumulateAdvertisingPayload(
00096         GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
00097         (uint8_t *)uuid16_list, sizeof(uuid16_list)
00098     );
00099     ble.gap().accumulateAdvertisingPayload(
00100         GapAdvertisingData::GENERIC_HEART_RATE_SENSOR
00101     );
00102     ble.gap().accumulateAdvertisingPayload(
00103         GapAdvertisingData::COMPLETE_LOCAL_NAME,
00104         (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)
00105     );
00106     ble.gap().setAdvertisingType(
00107         GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED
00108     );
00109     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00110     ble.gap().startAdvertising();
00111 }
00112 
00113 int main(void)
00114 {
00115     led1 = 1;
00116     Ticker ticker;
00117     ticker.attach(periodicCallback, 1);     // blink LED every second
00118 
00119     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00120     ble.init(bleInitComplete);
00121 
00122     /* SpinWait for initialization to complete. This is necessary 
00123      * because the BLE object is used in the main loop below. */
00124     while (ble.hasInitialized()  == false) { /* spin loop */ }
00125     // Check TYBLE-16 configuration
00126     cpu_sys();
00127     if (compile_condition() == false) {
00128         pc.printf("This is wrong configuration!!\r\n");
00129         while(true) {
00130             led1 = !led1;
00131             wait(0.2);
00132         }
00133     }
00134     //
00135     // infinite loop
00136     while (true) {
00137         // check for trigger from periodicCallback()
00138         if (triggerSensorPolling && ble.getGapState().connected) {
00139             triggerSensorPolling = false;
00140 
00141             // Do blocking calls or whatever is necessary for sensor polling.
00142             // In our case, we simply update the HRM measurement.
00143             hrmCounter++;
00144             if (hrmCounter == 175) { //  100 <= HRM bps <=175
00145                 hrmCounter = 100;
00146             }
00147 
00148             hrService->updateHeartRate(hrmCounter);
00149         } else {
00150             ble.waitForEvent(); // low power wait for event
00151         }
00152     }
00153 }