Combines a working system to save force, acceleration and gyro data to an SD card in a MAX32630 with BLE_Heartrate taken from the mbed site.

Dependencies:   USBMSD_BD BMI160 HX711 max32630fthr USBDevice

Committer:
qaz
Date:
Wed Oct 23 15:19:40 2019 +0000
Revision:
80:caccea4da07b
Parent:
73:633b44bce5fc
Child:
81:b8ef2a762318
Reason is Peter Bankuti's comment here about 14/12/18 version: https://os.mbed.com/questions/78005/Using-MAX32630FTHR-PAN1326B-bluetooth-mo/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 1:72c60abef7e7 1 /* mbed Microcontroller Library
mbed_official 1:72c60abef7e7 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 1:72c60abef7e7 3 *
mbed_official 1:72c60abef7e7 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 1:72c60abef7e7 5 * you may not use this file except in compliance with the License.
mbed_official 1:72c60abef7e7 6 * You may obtain a copy of the License at
mbed_official 1:72c60abef7e7 7 *
mbed_official 1:72c60abef7e7 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 1:72c60abef7e7 9 *
mbed_official 1:72c60abef7e7 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 1:72c60abef7e7 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 1:72c60abef7e7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 1:72c60abef7e7 13 * See the License for the specific language governing permissions and
mbed_official 1:72c60abef7e7 14 * limitations under the License.
mbed_official 1:72c60abef7e7 15 */
mbed_official 1:72c60abef7e7 16
mbed_official 10:ac3615194d04 17 #include <events/mbed_events.h>
mbed_official 1:72c60abef7e7 18 #include <mbed.h>
mbed_official 1:72c60abef7e7 19 #include "ble/BLE.h"
qaz 80:caccea4da07b 20 #include "ble/Gap.h"
mbed_official 1:72c60abef7e7 21 #include "ble/services/HeartRateService.h"
mbed_official 1:72c60abef7e7 22
qaz 80:caccea4da07b 23 DigitalOut led1(LED1, 1);
mbed_official 1:72c60abef7e7 24
qaz 80:caccea4da07b 25 const static char DEVICE_NAME[] = "HRM";
qaz 80:caccea4da07b 26 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
qaz 80:caccea4da07b 27
qaz 80:caccea4da07b 28 static uint8_t hrmCounter = 100; // init HRM to 100bps
qaz 80:caccea4da07b 29 static HeartRateService *hrServicePtr;
qaz 80:caccea4da07b 30
qaz 80:caccea4da07b 31 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
mbed_official 1:72c60abef7e7 32
qaz 80:caccea4da07b 33 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
qaz 80:caccea4da07b 34 {
qaz 80:caccea4da07b 35 BLE::Instance().gap().startAdvertising(); // restart advertising
qaz 80:caccea4da07b 36 }
mbed_official 1:72c60abef7e7 37
qaz 80:caccea4da07b 38 void updateSensorValue() {
qaz 80:caccea4da07b 39 // Do blocking calls or whatever is necessary for sensor polling.
qaz 80:caccea4da07b 40 // In our case, we simply update the HRM measurement.
qaz 80:caccea4da07b 41 hrmCounter++;
mbed_official 1:72c60abef7e7 42
qaz 80:caccea4da07b 43 // 100 <= HRM bps <=175
qaz 80:caccea4da07b 44 if (hrmCounter == 175) {
qaz 80:caccea4da07b 45 hrmCounter = 100;
mbed_official 1:72c60abef7e7 46 }
mbed_official 1:72c60abef7e7 47
qaz 80:caccea4da07b 48 hrServicePtr->updateHeartRate(hrmCounter);
qaz 80:caccea4da07b 49 }
mbed_official 1:72c60abef7e7 50
qaz 80:caccea4da07b 51 void periodicCallback(void)
qaz 80:caccea4da07b 52 {
qaz 80:caccea4da07b 53 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
mbed_official 1:72c60abef7e7 54
qaz 80:caccea4da07b 55 if (BLE::Instance().getGapState().connected) {
qaz 80:caccea4da07b 56 eventQueue.call(updateSensorValue);
qaz 80:caccea4da07b 57 }
qaz 80:caccea4da07b 58 }
mbed_official 1:72c60abef7e7 59
qaz 80:caccea4da07b 60 void onBleInitError(BLE &ble, ble_error_t error)
qaz 80:caccea4da07b 61 {
qaz 80:caccea4da07b 62 (void)ble;
qaz 80:caccea4da07b 63 (void)error;
qaz 80:caccea4da07b 64 /* Initialization error handling should go here */
qaz 80:caccea4da07b 65 }
mbed_official 1:72c60abef7e7 66
qaz 80:caccea4da07b 67 void printMacAddress()
qaz 80:caccea4da07b 68 {
qaz 80:caccea4da07b 69 /* Print out device MAC address to the console*/
qaz 80:caccea4da07b 70 Gap::AddressType_t addr_type;
qaz 80:caccea4da07b 71 Gap::Address_t address;
qaz 80:caccea4da07b 72 BLE::Instance().gap().getAddress(&addr_type, address);
qaz 80:caccea4da07b 73 printf("DEVICE MAC ADDRESS: ");
qaz 80:caccea4da07b 74 for (int i = 5; i >= 1; i--){
qaz 80:caccea4da07b 75 printf("%02x:", address[i]);
qaz 80:caccea4da07b 76 }
qaz 80:caccea4da07b 77 printf("%02x\r\n", address[0]);
qaz 80:caccea4da07b 78 }
mbed_official 43:fb2855f7754b 79
qaz 80:caccea4da07b 80 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
qaz 80:caccea4da07b 81 {
qaz 80:caccea4da07b 82 BLE& ble = params->ble;
qaz 80:caccea4da07b 83 ble_error_t error = params->error;
mbed_official 1:72c60abef7e7 84
qaz 80:caccea4da07b 85 if (error != BLE_ERROR_NONE) {
qaz 80:caccea4da07b 86 onBleInitError(ble, error);
qaz 80:caccea4da07b 87 return;
mbed_official 1:72c60abef7e7 88 }
mbed_official 1:72c60abef7e7 89
qaz 80:caccea4da07b 90 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
qaz 80:caccea4da07b 91 return;
mbed_official 1:72c60abef7e7 92 }
mbed_official 1:72c60abef7e7 93
qaz 80:caccea4da07b 94 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 1:72c60abef7e7 95
qaz 80:caccea4da07b 96 /* Setup primary service. */
qaz 80:caccea4da07b 97 hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
mbed_official 1:72c60abef7e7 98
qaz 80:caccea4da07b 99 /* Setup advertising. */
qaz 80:caccea4da07b 100 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
qaz 80:caccea4da07b 101 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
qaz 80:caccea4da07b 102 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
qaz 80:caccea4da07b 103 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
qaz 80:caccea4da07b 104 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
qaz 80:caccea4da07b 105 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
qaz 80:caccea4da07b 106 ble.gap().startAdvertising();
mbed_official 43:fb2855f7754b 107
qaz 80:caccea4da07b 108 printMacAddress();
qaz 80:caccea4da07b 109 }
mbed_official 1:72c60abef7e7 110
qaz 80:caccea4da07b 111 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
qaz 80:caccea4da07b 112 BLE &ble = BLE::Instance();
qaz 80:caccea4da07b 113 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 1:72c60abef7e7 114 }
mbed_official 1:72c60abef7e7 115
mbed_official 1:72c60abef7e7 116 int main()
mbed_official 1:72c60abef7e7 117 {
qaz 80:caccea4da07b 118 eventQueue.call_every(500, periodicCallback);
mbed_official 1:72c60abef7e7 119
qaz 80:caccea4da07b 120 BLE &ble = BLE::Instance();
qaz 80:caccea4da07b 121 ble.onEventsToProcess(scheduleBleEventsProcessing);
qaz 80:caccea4da07b 122 ble.init(bleInitComplete);
qaz 80:caccea4da07b 123
qaz 80:caccea4da07b 124 eventQueue.dispatch_forever();
mbed_official 1:72c60abef7e7 125
mbed_official 1:72c60abef7e7 126 return 0;
mbed_official 1:72c60abef7e7 127 }