Sample BLE Test

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API X_NUCLEO_IKS01A1

Committer:
olivasil
Date:
Fri Feb 22 11:29:35 2019 +0000
Revision:
4:49f750a9846e
Parent:
3:a51ca6313ad2
Sample test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:eb7f02ad28a7 1 /* mbed Microcontroller Library
screamer 0:eb7f02ad28a7 2 * Copyright (c) 2006-2015 ARM Limited
screamer 0:eb7f02ad28a7 3 *
screamer 0:eb7f02ad28a7 4 * Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:eb7f02ad28a7 5 * you may not use this file except in compliance with the License.
screamer 0:eb7f02ad28a7 6 * You may obtain a copy of the License at
screamer 0:eb7f02ad28a7 7 *
screamer 0:eb7f02ad28a7 8 * http://www.apache.org/licenses/LICENSE-2.0
screamer 0:eb7f02ad28a7 9 *
screamer 0:eb7f02ad28a7 10 * Unless required by applicable law or agreed to in writing, software
screamer 0:eb7f02ad28a7 11 * distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:eb7f02ad28a7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:eb7f02ad28a7 13 * See the License for the specific language governing permissions and
screamer 0:eb7f02ad28a7 14 * limitations under the License.
screamer 0:eb7f02ad28a7 15 */
screamer 0:eb7f02ad28a7 16
screamer 0:eb7f02ad28a7 17 #include "mbed.h"
screamer 0:eb7f02ad28a7 18 #include "ble/BLE.h"
screamer 0:eb7f02ad28a7 19 #include "ble/services/HeartRateService.h"
screamer 0:eb7f02ad28a7 20 #include "ble/services/BatteryService.h"
screamer 0:eb7f02ad28a7 21 #include "ble/services/DeviceInformationService.h"
screamer 0:eb7f02ad28a7 22
olivasil 4:49f750a9846e 23 #include "x_nucleo_iks01a1.h"
olivasil 4:49f750a9846e 24
olivasil 4:49f750a9846e 25
screamer 0:eb7f02ad28a7 26 BLE ble;
screamer 0:eb7f02ad28a7 27 DigitalOut led1(LED1);
olivasil 4:49f750a9846e 28 InterruptIn event(USER_BUTTON);
screamer 0:eb7f02ad28a7 29
screamer 0:eb7f02ad28a7 30 const static char DEVICE_NAME[] = "HRM1";
screamer 0:eb7f02ad28a7 31 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
screamer 0:eb7f02ad28a7 32 GattService::UUID_DEVICE_INFORMATION_SERVICE};
screamer 0:eb7f02ad28a7 33 static volatile bool triggerSensorPolling = false;
screamer 0:eb7f02ad28a7 34
olivasil 4:49f750a9846e 35 /* Instantiatethe expansion board */
olivasil 4:49f750a9846e 36 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
olivasil 4:49f750a9846e 37 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
olivasil 4:49f750a9846e 38
olivasil 4:49f750a9846e 39
olivasil 4:49f750a9846e 40 int button_pressed = 0;
olivasil 4:49f750a9846e 41
olivasil 4:49f750a9846e 42 void pressed()
olivasil 4:49f750a9846e 43 {
olivasil 4:49f750a9846e 44 button_pressed = 1;
olivasil 4:49f750a9846e 45 }
olivasil 4:49f750a9846e 46
olivasil 4:49f750a9846e 47
apalmieri 2:bc0c0d442a24 48 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
screamer 0:eb7f02ad28a7 49 {
screamer 0:eb7f02ad28a7 50 ble.gap().startAdvertising(); // restart advertising
screamer 0:eb7f02ad28a7 51 }
screamer 0:eb7f02ad28a7 52
screamer 0:eb7f02ad28a7 53 void periodicCallback(void)
screamer 0:eb7f02ad28a7 54 {
screamer 0:eb7f02ad28a7 55 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
screamer 0:eb7f02ad28a7 56 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
screamer 0:eb7f02ad28a7 57 * heavy-weight sensor polling from the main thread. */
screamer 0:eb7f02ad28a7 58 triggerSensorPolling = true;
screamer 0:eb7f02ad28a7 59 }
screamer 0:eb7f02ad28a7 60
screamer 0:eb7f02ad28a7 61 int main(void)
screamer 0:eb7f02ad28a7 62 {
screamer 0:eb7f02ad28a7 63 led1 = 1;
screamer 0:eb7f02ad28a7 64 Ticker ticker;
screamer 0:eb7f02ad28a7 65 ticker.attach(periodicCallback, 1); // blink LED every second
olivasil 4:49f750a9846e 66 float value1;
screamer 0:eb7f02ad28a7 67
screamer 0:eb7f02ad28a7 68 ble.init();
screamer 0:eb7f02ad28a7 69 ble.gap().onDisconnection(disconnectionCallback);
screamer 0:eb7f02ad28a7 70
screamer 0:eb7f02ad28a7 71 /* Setup primary service. */
screamer 0:eb7f02ad28a7 72 uint8_t hrmCounter = 100; // init HRM to 100bps
screamer 0:eb7f02ad28a7 73 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
screamer 0:eb7f02ad28a7 74
screamer 0:eb7f02ad28a7 75 /* Setup auxiliary service. */
screamer 0:eb7f02ad28a7 76 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
screamer 0:eb7f02ad28a7 77
screamer 0:eb7f02ad28a7 78 /* Setup advertising. */
screamer 0:eb7f02ad28a7 79 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
screamer 0:eb7f02ad28a7 80 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
screamer 0:eb7f02ad28a7 81 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
screamer 0:eb7f02ad28a7 82 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
screamer 0:eb7f02ad28a7 83 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
screamer 0:eb7f02ad28a7 84 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
screamer 0:eb7f02ad28a7 85 ble.gap().startAdvertising();
olivasil 4:49f750a9846e 86
olivasil 4:49f750a9846e 87 event.fall(&pressed);
screamer 0:eb7f02ad28a7 88
screamer 0:eb7f02ad28a7 89 // infinite loop
screamer 0:eb7f02ad28a7 90 while (1) {
screamer 0:eb7f02ad28a7 91 // check for trigger from periodicCallback()
screamer 0:eb7f02ad28a7 92 if (triggerSensorPolling && ble.getGapState().connected) {
screamer 0:eb7f02ad28a7 93 triggerSensorPolling = false;
screamer 0:eb7f02ad28a7 94
screamer 0:eb7f02ad28a7 95 // Do blocking calls or whatever is necessary for sensor polling.
screamer 0:eb7f02ad28a7 96 // In our case, we simply update the HRM measurement.
olivasil 4:49f750a9846e 97 temp_sensor1->GetTemperature(&value1);
olivasil 4:49f750a9846e 98 //printf("HTS221: [temp] %7s°C\r\n", printDouble(buffer1, value1));
olivasil 4:49f750a9846e 99
olivasil 4:49f750a9846e 100 if (button_pressed == 1)
olivasil 4:49f750a9846e 101 {
olivasil 4:49f750a9846e 102 hrmCounter++;
olivasil 4:49f750a9846e 103 button_pressed = 0;
olivasil 4:49f750a9846e 104 }
screamer 0:eb7f02ad28a7 105
screamer 0:eb7f02ad28a7 106 // 100 <= HRM bps <=175
screamer 0:eb7f02ad28a7 107 if (hrmCounter == 175) {
screamer 0:eb7f02ad28a7 108 hrmCounter = 100;
screamer 0:eb7f02ad28a7 109 }
screamer 0:eb7f02ad28a7 110
screamer 0:eb7f02ad28a7 111 // update bps
olivasil 4:49f750a9846e 112 //hrService.updateHeartRate(hrmCounter);
olivasil 4:49f750a9846e 113 hrService.updateHeartRate((uint8_t)value1);
screamer 0:eb7f02ad28a7 114 } else {
screamer 0:eb7f02ad28a7 115 ble.waitForEvent(); // low power wait for event
screamer 0:eb7f02ad28a7 116 }
screamer 0:eb7f02ad28a7 117 }
screamer 0:eb7f02ad28a7 118 }