app critics will say it's money, cash, toes

Committer:
vazbyte
Date:
Mon Nov 12 16:27:10 2018 +0000
Revision:
13:99ed8a3db67a
Parent:
12:3934b9d6d5a3
Child:
14:aa0029dbb3e2
relying on callback cycles instead of shady clock()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vazbyte 0:07212de2fec1 1 /* mbed Microcontroller Library
vazbyte 0:07212de2fec1 2 * Copyright (c) 2006-2014 ARM Limited
vazbyte 0:07212de2fec1 3 *
vazbyte 0:07212de2fec1 4 * Licensed under the Apache License, Version 2.0 (the "License");
vazbyte 0:07212de2fec1 5 * you may not use this file except in compliance with the License.
vazbyte 0:07212de2fec1 6 * You may obtain a copy of the License at
vazbyte 0:07212de2fec1 7 *
vazbyte 0:07212de2fec1 8 * http://www.apache.org/licenses/LICENSE-2.0
vazbyte 0:07212de2fec1 9 *
vazbyte 0:07212de2fec1 10 * Unless required by applicable law or agreed to in writing, software
vazbyte 0:07212de2fec1 11 * distributed under the License is distributed on an "AS IS" BASIS,
vazbyte 0:07212de2fec1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vazbyte 0:07212de2fec1 13 * See the License for the specific language governing permissions and
vazbyte 0:07212de2fec1 14 * limitations under the License.
vazbyte 0:07212de2fec1 15 */
vazbyte 0:07212de2fec1 16
vazbyte 0:07212de2fec1 17 #include <events/mbed_events.h>
vazbyte 3:a33709dad06c 18 #include <math.h>
vazbyte 11:5023e8c93e4d 19
vazbyte 10:8cf4069d064a 20 #include <ctime>
vazbyte 0:07212de2fec1 21 #include <mbed.h>
vazbyte 1:df8884d38960 22 #include "MPU9250.h"
vazbyte 0:07212de2fec1 23 #include "ble/BLE.h"
vazbyte 0:07212de2fec1 24 #include "ble/Gap.h"
vazbyte 1:df8884d38960 25 #include "ble/services/HeartRateService.h"
vazbyte 1:df8884d38960 26
vazbyte 0:07212de2fec1 27
vazbyte 0:07212de2fec1 28 DigitalOut led1(LED1, 1);
vazbyte 0:07212de2fec1 29
vazbyte 1:df8884d38960 30 const static char DEVICE_NAME[] = "ProVaida";
vazbyte 1:df8884d38960 31 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
vazbyte 0:07212de2fec1 32
vazbyte 4:af0530752b76 33 int16_t destination[3];
vaida 8:1735fddd5491 34 short hrmCounter = 0;
vaida 9:40874a5c5ad0 35 double step_threshold = 14.45;
vazbyte 12:3934b9d6d5a3 36
vaida 9:40874a5c5ad0 37 double oldAcceleration = 0.0;
vazbyte 13:99ed8a3db67a 38 int callback_cycles = 4;
vazbyte 13:99ed8a3db67a 39 int step;
vazbyte 10:8cf4069d064a 40
vazbyte 1:df8884d38960 41 static HeartRateService* hrService;
vazbyte 4:af0530752b76 42 MPU9250 mpu = MPU9250(P0_26, P0_27);
vazbyte 0:07212de2fec1 43
vazbyte 0:07212de2fec1 44 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
vazbyte 0:07212de2fec1 45
vazbyte 0:07212de2fec1 46 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
vazbyte 0:07212de2fec1 47 {
vazbyte 0:07212de2fec1 48 BLE::Instance().gap().startAdvertising();
vazbyte 0:07212de2fec1 49 }
vazbyte 0:07212de2fec1 50
vazbyte 2:1957a4985d6e 51 void updateSensorValue() {
vazbyte 13:99ed8a3db67a 52 step = 0;
vazbyte 13:99ed8a3db67a 53 callback_cycles++;
vazbyte 13:99ed8a3db67a 54
vazbyte 3:a33709dad06c 55 mpu.readAccelData(destination);
vazbyte 3:a33709dad06c 56
vaida 8:1735fddd5491 57 double acc_x = destination[0] / 1000.0;
vaida 8:1735fddd5491 58 double acc_y = destination[1] / 1000.0;
vaida 8:1735fddd5491 59 double acc_z = destination[2] / 1000.0;
vazbyte 3:a33709dad06c 60
vazbyte 7:9e2172e6550a 61 double sqr_acc_x = acc_x*acc_x;
vazbyte 7:9e2172e6550a 62 double sqr_acc_y = acc_y*acc_y;
vazbyte 7:9e2172e6550a 63 double sqr_acc_z = acc_z*acc_z;
vazbyte 3:a33709dad06c 64
vazbyte 3:a33709dad06c 65 double sum_acc = sqr_acc_x + sqr_acc_y + sqr_acc_z;
vaida 9:40874a5c5ad0 66 double accel = sqrt(sum_acc);
vazbyte 11:5023e8c93e4d 67
vazbyte 13:99ed8a3db67a 68 printf("calback cycles: " );
vazbyte 13:99ed8a3db67a 69 printf("%i\n", callback_cycles);
vazbyte 10:8cf4069d064a 70
vazbyte 13:99ed8a3db67a 71 if (accel < step_threshold && oldAcceleration >= step_threshold && (callback_cycles > 4)) {
vazbyte 10:8cf4069d064a 72 step = 1;
vazbyte 13:99ed8a3db67a 73 callback_cycles = 0;
vazbyte 10:8cf4069d064a 74 }
vaida 9:40874a5c5ad0 75
vaida 9:40874a5c5ad0 76 oldAcceleration = accel;
vaida 9:40874a5c5ad0 77
vaida 9:40874a5c5ad0 78 hrmCounter = (short) step;
vaida 9:40874a5c5ad0 79 printf("STEP: " );
vaida 8:1735fddd5491 80 printf("%hu\n", hrmCounter);
vaida 9:40874a5c5ad0 81
vazbyte 1:df8884d38960 82 hrService->updateHeartRate(hrmCounter);
vazbyte 0:07212de2fec1 83 }
vazbyte 0:07212de2fec1 84
vazbyte 0:07212de2fec1 85 void blinkCallback(void)
vazbyte 0:07212de2fec1 86 {
vazbyte 0:07212de2fec1 87 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
vazbyte 0:07212de2fec1 88 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 89 if (ble.gap().getState().connected) {
vazbyte 0:07212de2fec1 90 eventQueue.call(updateSensorValue);
vazbyte 0:07212de2fec1 91 }
vazbyte 0:07212de2fec1 92 }
vazbyte 0:07212de2fec1 93
vazbyte 0:07212de2fec1 94 /**
vazbyte 1:df8884d38960 95 * This function is called when the ble initialization process has failed
vazbyte 0:07212de2fec1 96 */
vazbyte 0:07212de2fec1 97 void onBleInitError(BLE &ble, ble_error_t error)
vazbyte 0:07212de2fec1 98 {
vazbyte 0:07212de2fec1 99 /* Initialization error handling should go here */
vazbyte 0:07212de2fec1 100 }
vazbyte 0:07212de2fec1 101
vazbyte 0:07212de2fec1 102 void printMacAddress()
vazbyte 0:07212de2fec1 103 {
vazbyte 0:07212de2fec1 104 /* Print out device MAC address to the console*/
vazbyte 0:07212de2fec1 105 Gap::AddressType_t addr_type;
vazbyte 0:07212de2fec1 106 Gap::Address_t address;
vazbyte 0:07212de2fec1 107 BLE::Instance().gap().getAddress(&addr_type, address);
vazbyte 0:07212de2fec1 108 printf("DEVICE MAC ADDRESS: ");
vazbyte 0:07212de2fec1 109 for (int i = 5; i >= 1; i--){
vazbyte 0:07212de2fec1 110 printf("%02x:", address[i]);
vazbyte 0:07212de2fec1 111 }
vazbyte 0:07212de2fec1 112 printf("%02x\r\n", address[0]);
vazbyte 0:07212de2fec1 113 }
vazbyte 0:07212de2fec1 114
vazbyte 0:07212de2fec1 115 /**
vazbyte 0:07212de2fec1 116 * Callback triggered when the ble initialization process has finished
vazbyte 0:07212de2fec1 117 */
vazbyte 0:07212de2fec1 118 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
vazbyte 0:07212de2fec1 119 {
vazbyte 0:07212de2fec1 120 BLE& ble = params->ble;
vazbyte 0:07212de2fec1 121 ble_error_t error = params->error;
vazbyte 0:07212de2fec1 122
vazbyte 0:07212de2fec1 123 if (error != BLE_ERROR_NONE) {
vazbyte 0:07212de2fec1 124 /* In case of error, forward the error handling to onBleInitError */
vazbyte 0:07212de2fec1 125 onBleInitError(ble, error);
vazbyte 0:07212de2fec1 126 return;
vazbyte 0:07212de2fec1 127 }
vazbyte 0:07212de2fec1 128
vazbyte 0:07212de2fec1 129 /* Ensure that it is the default instance of BLE */
vazbyte 0:07212de2fec1 130 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
vazbyte 0:07212de2fec1 131 return;
vazbyte 0:07212de2fec1 132 }
vazbyte 0:07212de2fec1 133
vazbyte 0:07212de2fec1 134 ble.gap().onDisconnection(disconnectionCallback);
vazbyte 0:07212de2fec1 135
vazbyte 0:07212de2fec1 136 /* Setup primary service */
vazbyte 1:df8884d38960 137 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
vazbyte 0:07212de2fec1 138
vazbyte 0:07212de2fec1 139 /* Setup advertising */
vazbyte 0:07212de2fec1 140 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vazbyte 0:07212de2fec1 141 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
vazbyte 0:07212de2fec1 142 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
vazbyte 0:07212de2fec1 143 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vazbyte 0:07212de2fec1 144 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
vazbyte 0:07212de2fec1 145 ble.gap().startAdvertising();
vazbyte 0:07212de2fec1 146
vaida 6:887df9bbe705 147
vazbyte 0:07212de2fec1 148 printMacAddress();
vaida 6:887df9bbe705 149
vazbyte 0:07212de2fec1 150 }
vazbyte 0:07212de2fec1 151
vazbyte 0:07212de2fec1 152 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
vazbyte 0:07212de2fec1 153 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 154 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
vazbyte 0:07212de2fec1 155 }
vazbyte 0:07212de2fec1 156
vazbyte 0:07212de2fec1 157 int main()
vazbyte 4:af0530752b76 158 {
vazbyte 2:1957a4985d6e 159 mpu.initMPU9250();
vazbyte 10:8cf4069d064a 160
vazbyte 13:99ed8a3db67a 161 eventQueue.call_every(80, blinkCallback);
vazbyte 0:07212de2fec1 162
vazbyte 0:07212de2fec1 163 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 164 ble.onEventsToProcess(scheduleBleEventsProcessing);
vazbyte 0:07212de2fec1 165 ble.init(bleInitComplete);
vazbyte 0:07212de2fec1 166
vazbyte 0:07212de2fec1 167 eventQueue.dispatch_forever();
vazbyte 0:07212de2fec1 168
vazbyte 0:07212de2fec1 169 return 0;
vazbyte 0:07212de2fec1 170 }