Nora Vazbyte
/
99Problems-BLEAint1
app critics will say it's money, cash, toes
source/main.cpp@18:4cbd88c0a374, 2018-11-21 (annotated)
- Committer:
- vazbyte
- Date:
- Wed Nov 21 10:40:41 2018 +0000
- Revision:
- 18:4cbd88c0a374
- Parent:
- 17:b4a080229f5c
- Child:
- 19:1998218bbb63
- Child:
- 20:7cc04c7e1178
a little less flash flash flash flash
Who changed what in which revision?
User | Revision | Line number | New 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 | 17:b4a080229f5c | 27 | DigitalOut Led0(p19); |
vazbyte | 17:b4a080229f5c | 28 | DigitalOut Led1(p20); |
vazbyte | 17:b4a080229f5c | 29 | DigitalOut Led2(p11); |
vazbyte | 17:b4a080229f5c | 30 | DigitalOut Led3(p12); |
vazbyte | 17:b4a080229f5c | 31 | DigitalOut Led4(p13); |
vazbyte | 17:b4a080229f5c | 32 | DigitalOut Led5(p14); |
vazbyte | 17:b4a080229f5c | 33 | DigitalOut Led6(p15); |
vazbyte | 17:b4a080229f5c | 34 | DigitalOut Led7(p16); |
vazbyte | 17:b4a080229f5c | 35 | DigitalOut Led8(p17); |
vazbyte | 17:b4a080229f5c | 36 | DigitalOut Led9(p18); |
vazbyte | 0:07212de2fec1 | 37 | |
vazbyte | 18:4cbd88c0a374 | 38 | //DigitalOut led1(LED1, 1); |
vazbyte | 0:07212de2fec1 | 39 | |
vazbyte | 1:df8884d38960 | 40 | const static char DEVICE_NAME[] = "ProVaida"; |
vazbyte | 1:df8884d38960 | 41 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; |
vazbyte | 0:07212de2fec1 | 42 | |
vazbyte | 4:af0530752b76 | 43 | int16_t destination[3]; |
vaida | 8:1735fddd5491 | 44 | short hrmCounter = 0; |
vaida | 9:40874a5c5ad0 | 45 | double step_threshold = 14.45; |
vazbyte | 12:3934b9d6d5a3 | 46 | |
vaida | 9:40874a5c5ad0 | 47 | double oldAcceleration = 0.0; |
vaida | 15:a502564c7a88 | 48 | int callback_cycles = 1; // used to be 4 |
vazbyte | 13:99ed8a3db67a | 49 | int step; |
vaida | 15:a502564c7a88 | 50 | int run_threshold = 5; // used to be 7 |
vaida | 15:a502564c7a88 | 51 | int run_count = 0; |
vazbyte | 10:8cf4069d064a | 52 | |
vazbyte | 17:b4a080229f5c | 53 | int totalsteps = 0; |
vazbyte | 17:b4a080229f5c | 54 | |
vazbyte | 1:df8884d38960 | 55 | static HeartRateService* hrService; |
vazbyte | 4:af0530752b76 | 56 | MPU9250 mpu = MPU9250(P0_26, P0_27); |
vazbyte | 0:07212de2fec1 | 57 | |
vazbyte | 0:07212de2fec1 | 58 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
vazbyte | 0:07212de2fec1 | 59 | |
vazbyte | 0:07212de2fec1 | 60 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
vazbyte | 0:07212de2fec1 | 61 | { |
vazbyte | 0:07212de2fec1 | 62 | BLE::Instance().gap().startAdvertising(); |
vazbyte | 0:07212de2fec1 | 63 | } |
vazbyte | 0:07212de2fec1 | 64 | |
vazbyte | 2:1957a4985d6e | 65 | void updateSensorValue() { |
vazbyte | 13:99ed8a3db67a | 66 | step = 0; |
vazbyte | 13:99ed8a3db67a | 67 | callback_cycles++; |
vazbyte | 13:99ed8a3db67a | 68 | |
vazbyte | 3:a33709dad06c | 69 | mpu.readAccelData(destination); |
vazbyte | 3:a33709dad06c | 70 | |
vaida | 8:1735fddd5491 | 71 | double acc_x = destination[0] / 1000.0; |
vaida | 8:1735fddd5491 | 72 | double acc_y = destination[1] / 1000.0; |
vaida | 8:1735fddd5491 | 73 | double acc_z = destination[2] / 1000.0; |
vazbyte | 3:a33709dad06c | 74 | |
vazbyte | 7:9e2172e6550a | 75 | double sqr_acc_x = acc_x*acc_x; |
vazbyte | 7:9e2172e6550a | 76 | double sqr_acc_y = acc_y*acc_y; |
vazbyte | 7:9e2172e6550a | 77 | double sqr_acc_z = acc_z*acc_z; |
vazbyte | 3:a33709dad06c | 78 | |
vazbyte | 3:a33709dad06c | 79 | double sum_acc = sqr_acc_x + sqr_acc_y + sqr_acc_z; |
vaida | 9:40874a5c5ad0 | 80 | double accel = sqrt(sum_acc); |
vazbyte | 11:5023e8c93e4d | 81 | |
vazbyte | 14:aa0029dbb3e2 | 82 | //printf("calback cycles: " ); |
vazbyte | 14:aa0029dbb3e2 | 83 | //printf("%i\n", callback_cycles); |
vazbyte | 10:8cf4069d064a | 84 | |
vazbyte | 14:aa0029dbb3e2 | 85 | if (accel < step_threshold && oldAcceleration >= step_threshold && (callback_cycles > 3)) { |
vaida | 15:a502564c7a88 | 86 | if (callback_cycles <= run_threshold) { |
vaida | 15:a502564c7a88 | 87 | if (run_count >= 2) { |
vaida | 15:a502564c7a88 | 88 | step = 2; |
vaida | 15:a502564c7a88 | 89 | } |
vaida | 15:a502564c7a88 | 90 | else { |
vaida | 16:567f06e22645 | 91 | step = 1; |
vaida | 15:a502564c7a88 | 92 | run_count++; |
vaida | 15:a502564c7a88 | 93 | } |
vaida | 15:a502564c7a88 | 94 | } |
vaida | 15:a502564c7a88 | 95 | else { |
vaida | 15:a502564c7a88 | 96 | step = 1; |
vaida | 15:a502564c7a88 | 97 | run_count = 0; |
vaida | 15:a502564c7a88 | 98 | } |
vazbyte | 13:99ed8a3db67a | 99 | callback_cycles = 0; |
vazbyte | 10:8cf4069d064a | 100 | } |
vazbyte | 17:b4a080229f5c | 101 | if (step != 0) |
vazbyte | 17:b4a080229f5c | 102 | { |
vazbyte | 17:b4a080229f5c | 103 | printf("STEP: " ); |
vazbyte | 17:b4a080229f5c | 104 | printf("%i\n", step); |
vazbyte | 17:b4a080229f5c | 105 | |
vazbyte | 17:b4a080229f5c | 106 | totalsteps++; |
vazbyte | 17:b4a080229f5c | 107 | |
vazbyte | 17:b4a080229f5c | 108 | printf("totalSTEPs: " ); |
vazbyte | 17:b4a080229f5c | 109 | printf("%i\n", totalsteps); |
vazbyte | 17:b4a080229f5c | 110 | |
vazbyte | 17:b4a080229f5c | 111 | Led0 = ((totalsteps >> 0) % 2); |
vazbyte | 17:b4a080229f5c | 112 | Led1 = ((totalsteps >> 1) % 2); |
vazbyte | 17:b4a080229f5c | 113 | Led2 = ((totalsteps >> 2) % 2); |
vazbyte | 17:b4a080229f5c | 114 | Led3 = ((totalsteps >> 3) % 2); |
vazbyte | 17:b4a080229f5c | 115 | Led4 = ((totalsteps >> 4) % 2); |
vazbyte | 17:b4a080229f5c | 116 | Led5 = ((totalsteps >> 5) % 2); |
vazbyte | 17:b4a080229f5c | 117 | Led6 = ((totalsteps >> 6) % 2); |
vazbyte | 17:b4a080229f5c | 118 | Led7 = ((totalsteps >> 7) % 2); |
vazbyte | 17:b4a080229f5c | 119 | Led8 = ((totalsteps >> 8) % 2); |
vazbyte | 17:b4a080229f5c | 120 | Led9 = ((totalsteps >> 9) % 2); |
vazbyte | 17:b4a080229f5c | 121 | } |
vaida | 9:40874a5c5ad0 | 122 | |
vaida | 9:40874a5c5ad0 | 123 | oldAcceleration = accel; |
vaida | 9:40874a5c5ad0 | 124 | |
vaida | 9:40874a5c5ad0 | 125 | hrmCounter = (short) step; |
vazbyte | 14:aa0029dbb3e2 | 126 | //printf("STEP: " ); |
vazbyte | 14:aa0029dbb3e2 | 127 | //printf("%hu\n", hrmCounter); |
vaida | 9:40874a5c5ad0 | 128 | |
vazbyte | 1:df8884d38960 | 129 | hrService->updateHeartRate(hrmCounter); |
vazbyte | 0:07212de2fec1 | 130 | } |
vazbyte | 0:07212de2fec1 | 131 | |
vazbyte | 0:07212de2fec1 | 132 | void blinkCallback(void) |
vazbyte | 0:07212de2fec1 | 133 | { |
vazbyte | 18:4cbd88c0a374 | 134 | //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
vazbyte | 0:07212de2fec1 | 135 | BLE &ble = BLE::Instance(); |
vazbyte | 0:07212de2fec1 | 136 | if (ble.gap().getState().connected) { |
vazbyte | 0:07212de2fec1 | 137 | eventQueue.call(updateSensorValue); |
vazbyte | 0:07212de2fec1 | 138 | } |
vazbyte | 0:07212de2fec1 | 139 | } |
vazbyte | 0:07212de2fec1 | 140 | |
vazbyte | 0:07212de2fec1 | 141 | /** |
vazbyte | 1:df8884d38960 | 142 | * This function is called when the ble initialization process has failed |
vazbyte | 0:07212de2fec1 | 143 | */ |
vazbyte | 0:07212de2fec1 | 144 | void onBleInitError(BLE &ble, ble_error_t error) |
vazbyte | 0:07212de2fec1 | 145 | { |
vazbyte | 0:07212de2fec1 | 146 | /* Initialization error handling should go here */ |
vazbyte | 0:07212de2fec1 | 147 | } |
vazbyte | 0:07212de2fec1 | 148 | |
vazbyte | 0:07212de2fec1 | 149 | void printMacAddress() |
vazbyte | 0:07212de2fec1 | 150 | { |
vazbyte | 0:07212de2fec1 | 151 | /* Print out device MAC address to the console*/ |
vazbyte | 0:07212de2fec1 | 152 | Gap::AddressType_t addr_type; |
vazbyte | 0:07212de2fec1 | 153 | Gap::Address_t address; |
vazbyte | 0:07212de2fec1 | 154 | BLE::Instance().gap().getAddress(&addr_type, address); |
vazbyte | 0:07212de2fec1 | 155 | printf("DEVICE MAC ADDRESS: "); |
vazbyte | 0:07212de2fec1 | 156 | for (int i = 5; i >= 1; i--){ |
vazbyte | 0:07212de2fec1 | 157 | printf("%02x:", address[i]); |
vazbyte | 0:07212de2fec1 | 158 | } |
vazbyte | 0:07212de2fec1 | 159 | printf("%02x\r\n", address[0]); |
vazbyte | 0:07212de2fec1 | 160 | } |
vazbyte | 0:07212de2fec1 | 161 | |
vazbyte | 0:07212de2fec1 | 162 | /** |
vazbyte | 0:07212de2fec1 | 163 | * Callback triggered when the ble initialization process has finished |
vazbyte | 0:07212de2fec1 | 164 | */ |
vazbyte | 0:07212de2fec1 | 165 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
vazbyte | 0:07212de2fec1 | 166 | { |
vazbyte | 0:07212de2fec1 | 167 | BLE& ble = params->ble; |
vazbyte | 0:07212de2fec1 | 168 | ble_error_t error = params->error; |
vazbyte | 0:07212de2fec1 | 169 | |
vazbyte | 0:07212de2fec1 | 170 | if (error != BLE_ERROR_NONE) { |
vazbyte | 0:07212de2fec1 | 171 | /* In case of error, forward the error handling to onBleInitError */ |
vazbyte | 0:07212de2fec1 | 172 | onBleInitError(ble, error); |
vazbyte | 0:07212de2fec1 | 173 | return; |
vazbyte | 0:07212de2fec1 | 174 | } |
vazbyte | 0:07212de2fec1 | 175 | |
vazbyte | 0:07212de2fec1 | 176 | /* Ensure that it is the default instance of BLE */ |
vazbyte | 0:07212de2fec1 | 177 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
vazbyte | 0:07212de2fec1 | 178 | return; |
vazbyte | 0:07212de2fec1 | 179 | } |
vazbyte | 0:07212de2fec1 | 180 | |
vazbyte | 0:07212de2fec1 | 181 | ble.gap().onDisconnection(disconnectionCallback); |
vazbyte | 0:07212de2fec1 | 182 | |
vazbyte | 0:07212de2fec1 | 183 | /* Setup primary service */ |
vazbyte | 1:df8884d38960 | 184 | hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
vazbyte | 0:07212de2fec1 | 185 | |
vazbyte | 0:07212de2fec1 | 186 | /* Setup advertising */ |
vazbyte | 0:07212de2fec1 | 187 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
vazbyte | 0:07212de2fec1 | 188 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
vazbyte | 0:07212de2fec1 | 189 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
vazbyte | 0:07212de2fec1 | 190 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
vazbyte | 0:07212de2fec1 | 191 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
vazbyte | 0:07212de2fec1 | 192 | ble.gap().startAdvertising(); |
vazbyte | 0:07212de2fec1 | 193 | |
vaida | 6:887df9bbe705 | 194 | |
vazbyte | 0:07212de2fec1 | 195 | printMacAddress(); |
vaida | 6:887df9bbe705 | 196 | |
vazbyte | 0:07212de2fec1 | 197 | } |
vazbyte | 0:07212de2fec1 | 198 | |
vazbyte | 0:07212de2fec1 | 199 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
vazbyte | 0:07212de2fec1 | 200 | BLE &ble = BLE::Instance(); |
vazbyte | 0:07212de2fec1 | 201 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
vazbyte | 0:07212de2fec1 | 202 | } |
vazbyte | 0:07212de2fec1 | 203 | |
vazbyte | 0:07212de2fec1 | 204 | int main() |
vazbyte | 4:af0530752b76 | 205 | { |
vazbyte | 2:1957a4985d6e | 206 | mpu.initMPU9250(); |
vazbyte | 10:8cf4069d064a | 207 | |
vazbyte | 13:99ed8a3db67a | 208 | eventQueue.call_every(80, blinkCallback); |
vazbyte | 0:07212de2fec1 | 209 | |
vazbyte | 0:07212de2fec1 | 210 | BLE &ble = BLE::Instance(); |
vazbyte | 0:07212de2fec1 | 211 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
vazbyte | 0:07212de2fec1 | 212 | ble.init(bleInitComplete); |
vazbyte | 0:07212de2fec1 | 213 | |
vazbyte | 0:07212de2fec1 | 214 | eventQueue.dispatch_forever(); |
vazbyte | 0:07212de2fec1 | 215 | |
vazbyte | 0:07212de2fec1 | 216 | return 0; |
vazbyte | 0:07212de2fec1 | 217 | } |