demo with ST sensor and ST BLE

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed-src-ST-demo

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
NickZhouARM
Date:
Mon Jun 06 08:19:06 2016 +0000
Revision:
17:d854777ad2af
Parent:
16:8621076e799a
working demo for ST sensors and ST BLE

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"
NickZhouARM 16:8621076e799a 20 #include "x_nucleo_iks01a1.h"
NickZhouARM 16:8621076e799a 21
NickZhouARM 16:8621076e799a 22 Serial pc(SERIAL_TX, SERIAL_RX);
screamer 0:eb7f02ad28a7 23
apalmieri 13:227a0149b677 24 DigitalOut led1(LED1, 1);
screamer 0:eb7f02ad28a7 25
NickZhouARM 16:8621076e799a 26 /* Instantiate the expansion board */
NickZhouARM 16:8621076e799a 27 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
NickZhouARM 16:8621076e799a 28
NickZhouARM 16:8621076e799a 29 /* Retrieve the composing elements of the expansion board */
NickZhouARM 16:8621076e799a 30 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
NickZhouARM 16:8621076e799a 31 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
NickZhouARM 16:8621076e799a 32 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
NickZhouARM 16:8621076e799a 33 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
NickZhouARM 16:8621076e799a 34 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
NickZhouARM 16:8621076e799a 35 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
NickZhouARM 16:8621076e799a 36 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
NickZhouARM 16:8621076e799a 37
NickZhouARM 16:8621076e799a 38 /* Helper function for printing floats & doubles */
NickZhouARM 16:8621076e799a 39 static char *printDouble(char* str, double v, int decimalDigits=2)
NickZhouARM 16:8621076e799a 40 {
NickZhouARM 16:8621076e799a 41 int i = 1;
NickZhouARM 16:8621076e799a 42 int intPart, fractPart;
NickZhouARM 16:8621076e799a 43 int len;
NickZhouARM 16:8621076e799a 44 char *ptr;
NickZhouARM 16:8621076e799a 45
NickZhouARM 16:8621076e799a 46 /* prepare decimal digits multiplicator */
NickZhouARM 16:8621076e799a 47 for (;decimalDigits!=0; i*=10, decimalDigits--);
NickZhouARM 16:8621076e799a 48
NickZhouARM 16:8621076e799a 49 /* calculate integer & fractinal parts */
NickZhouARM 16:8621076e799a 50 intPart = (int)v;
NickZhouARM 16:8621076e799a 51 fractPart = (int)((v-(double)(int)v)*i);
NickZhouARM 16:8621076e799a 52
NickZhouARM 16:8621076e799a 53 /* fill in integer part */
NickZhouARM 16:8621076e799a 54 sprintf(str, "%i.", intPart);
NickZhouARM 16:8621076e799a 55
NickZhouARM 16:8621076e799a 56 /* prepare fill in of fractional part */
NickZhouARM 16:8621076e799a 57 len = strlen(str);
NickZhouARM 16:8621076e799a 58 ptr = &str[len];
NickZhouARM 16:8621076e799a 59
NickZhouARM 16:8621076e799a 60 /* fill in leading fractional zeros */
NickZhouARM 16:8621076e799a 61 for (i/=10;i>1; i/=10, ptr++) {
NickZhouARM 16:8621076e799a 62 if(fractPart >= i) break;
NickZhouARM 16:8621076e799a 63 *ptr = '0';
NickZhouARM 16:8621076e799a 64 }
NickZhouARM 16:8621076e799a 65
NickZhouARM 16:8621076e799a 66 /* fill in (rest of) fractional part */
NickZhouARM 16:8621076e799a 67 sprintf(ptr, "%i", fractPart);
NickZhouARM 16:8621076e799a 68
NickZhouARM 16:8621076e799a 69 return str;
NickZhouARM 16:8621076e799a 70 }
NickZhouARM 16:8621076e799a 71
screamer 0:eb7f02ad28a7 72 const static char DEVICE_NAME[] = "HRM1";
apalmieri 13:227a0149b677 73 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
apalmieri 13:227a0149b677 74
screamer 0:eb7f02ad28a7 75 static volatile bool triggerSensorPolling = false;
screamer 0:eb7f02ad28a7 76
apalmieri 2:bc0c0d442a24 77 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
screamer 0:eb7f02ad28a7 78 {
apalmieri 13:227a0149b677 79 (void)params;
apalmieri 13:227a0149b677 80 BLE::Instance().gap().startAdvertising(); // restart advertising
screamer 0:eb7f02ad28a7 81 }
screamer 0:eb7f02ad28a7 82
screamer 0:eb7f02ad28a7 83 void periodicCallback(void)
screamer 0:eb7f02ad28a7 84 {
screamer 0:eb7f02ad28a7 85 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
screamer 0:eb7f02ad28a7 86 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
screamer 0:eb7f02ad28a7 87 * heavy-weight sensor polling from the main thread. */
screamer 0:eb7f02ad28a7 88 triggerSensorPolling = true;
screamer 0:eb7f02ad28a7 89 }
screamer 0:eb7f02ad28a7 90
apalmieri 13:227a0149b677 91 void onBleInitError(BLE &ble, ble_error_t error)
apalmieri 13:227a0149b677 92 {
apalmieri 13:227a0149b677 93 (void)ble;
apalmieri 13:227a0149b677 94 (void)error;
apalmieri 13:227a0149b677 95 /* Initialization error handling should go here */
apalmieri 13:227a0149b677 96 }
apalmieri 13:227a0149b677 97
apalmieri 13:227a0149b677 98 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
screamer 0:eb7f02ad28a7 99 {
apalmieri 13:227a0149b677 100 BLE& ble = params->ble;
apalmieri 13:227a0149b677 101 ble_error_t error = params->error;
screamer 0:eb7f02ad28a7 102
NickZhouARM 16:8621076e799a 103 uint8_t id;
NickZhouARM 16:8621076e799a 104 float value1, value2;
NickZhouARM 16:8621076e799a 105 char buffer1[32], buffer2[32];
NickZhouARM 16:8621076e799a 106 int32_t axes[3];
NickZhouARM 16:8621076e799a 107
NickZhouARM 16:8621076e799a 108
NickZhouARM 16:8621076e799a 109 pc.printf("1\n");
apalmieri 13:227a0149b677 110 if (error != BLE_ERROR_NONE) {
apalmieri 13:227a0149b677 111 onBleInitError(ble, error);
apalmieri 13:227a0149b677 112 return;
apalmieri 13:227a0149b677 113 }
NickZhouARM 16:8621076e799a 114 pc.printf("2\n");
apalmieri 13:227a0149b677 115 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
apalmieri 13:227a0149b677 116 return;
apalmieri 13:227a0149b677 117 }
NickZhouARM 16:8621076e799a 118 pc.printf("3\n");
screamer 0:eb7f02ad28a7 119 ble.gap().onDisconnection(disconnectionCallback);
screamer 0:eb7f02ad28a7 120
screamer 0:eb7f02ad28a7 121 /* Setup primary service. */
screamer 0:eb7f02ad28a7 122 uint8_t hrmCounter = 100; // init HRM to 100bps
screamer 0:eb7f02ad28a7 123 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
NickZhouARM 16:8621076e799a 124 pc.printf("4\n");
screamer 0:eb7f02ad28a7 125 /* Setup advertising. */
screamer 0:eb7f02ad28a7 126 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
screamer 0:eb7f02ad28a7 127 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
screamer 0:eb7f02ad28a7 128 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
screamer 0:eb7f02ad28a7 129 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
screamer 0:eb7f02ad28a7 130 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
screamer 0:eb7f02ad28a7 131 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
screamer 0:eb7f02ad28a7 132 ble.gap().startAdvertising();
NickZhouARM 16:8621076e799a 133 pc.printf("5\n");
NickZhouARM 16:8621076e799a 134
NickZhouARM 16:8621076e799a 135 humidity_sensor->ReadID(&id);
NickZhouARM 16:8621076e799a 136 pc.printf("HTS221 humidity & temperature = 0x%X\r\n", id);
NickZhouARM 16:8621076e799a 137 pressure_sensor->ReadID(&id);
NickZhouARM 16:8621076e799a 138 pc.printf("LPS25H pressure & temperature = 0x%X\r\n", id);
NickZhouARM 16:8621076e799a 139 magnetometer->ReadID(&id);
NickZhouARM 16:8621076e799a 140 pc.printf("LIS3MDL magnetometer = 0x%X\r\n", id);
NickZhouARM 16:8621076e799a 141 gyroscope->ReadID(&id);
NickZhouARM 16:8621076e799a 142 pc.printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
NickZhouARM 16:8621076e799a 143
NickZhouARM 16:8621076e799a 144 wait(3);
NickZhouARM 16:8621076e799a 145
screamer 0:eb7f02ad28a7 146 // infinite loop
apalmieri 13:227a0149b677 147 while (true) {
screamer 0:eb7f02ad28a7 148 // check for trigger from periodicCallback()
NickZhouARM 16:8621076e799a 149 //pc.printf("6\n");
screamer 0:eb7f02ad28a7 150 if (triggerSensorPolling && ble.getGapState().connected) {
screamer 0:eb7f02ad28a7 151 triggerSensorPolling = false;
screamer 0:eb7f02ad28a7 152
screamer 0:eb7f02ad28a7 153 // Do blocking calls or whatever is necessary for sensor polling.
screamer 0:eb7f02ad28a7 154 // In our case, we simply update the HRM measurement.
screamer 0:eb7f02ad28a7 155 hrmCounter++;
screamer 0:eb7f02ad28a7 156
screamer 0:eb7f02ad28a7 157 // 100 <= HRM bps <=175
screamer 0:eb7f02ad28a7 158 if (hrmCounter == 175) {
screamer 0:eb7f02ad28a7 159 hrmCounter = 100;
screamer 0:eb7f02ad28a7 160 }
screamer 0:eb7f02ad28a7 161
screamer 0:eb7f02ad28a7 162 // update bps
screamer 0:eb7f02ad28a7 163 hrService.updateHeartRate(hrmCounter);
NickZhouARM 16:8621076e799a 164
NickZhouARM 16:8621076e799a 165 temp_sensor1->GetTemperature(&value1);
NickZhouARM 16:8621076e799a 166 humidity_sensor->GetHumidity(&value2);
NickZhouARM 16:8621076e799a 167 pc.printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
NickZhouARM 16:8621076e799a 168
NickZhouARM 16:8621076e799a 169 temp_sensor2->GetFahrenheit(&value1);
NickZhouARM 16:8621076e799a 170 pressure_sensor->GetPressure(&value2);
NickZhouARM 16:8621076e799a 171 pc.printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
NickZhouARM 16:8621076e799a 172
NickZhouARM 16:8621076e799a 173 pc.printf("---\r\n");
NickZhouARM 16:8621076e799a 174
NickZhouARM 16:8621076e799a 175 magnetometer->Get_M_Axes(axes);
NickZhouARM 16:8621076e799a 176 pc.printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
NickZhouARM 16:8621076e799a 177
NickZhouARM 16:8621076e799a 178 accelerometer->Get_X_Axes(axes);
NickZhouARM 16:8621076e799a 179 pc.printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
NickZhouARM 16:8621076e799a 180
NickZhouARM 16:8621076e799a 181 gyroscope->Get_G_Axes(axes);
NickZhouARM 16:8621076e799a 182 pc.printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
NickZhouARM 16:8621076e799a 183
screamer 0:eb7f02ad28a7 184 } else {
screamer 0:eb7f02ad28a7 185 ble.waitForEvent(); // low power wait for event
screamer 0:eb7f02ad28a7 186 }
screamer 0:eb7f02ad28a7 187 }
screamer 0:eb7f02ad28a7 188 }
apalmieri 13:227a0149b677 189
apalmieri 13:227a0149b677 190 int main(void)
apalmieri 13:227a0149b677 191 {
NickZhouARM 16:8621076e799a 192
apalmieri 13:227a0149b677 193 Ticker ticker;
NickZhouARM 16:8621076e799a 194 pc.printf("Hello World !\n");
apalmieri 13:227a0149b677 195 ticker.attach(periodicCallback, 1); // blink LED every second
apalmieri 13:227a0149b677 196
apalmieri 13:227a0149b677 197 BLE::Instance().init(bleInitComplete);
apalmieri 13:227a0149b677 198 }