Nora Vazbyte
/
99Problems-BLEAint1
app critics will say it's money, cash, toes
source/main.cpp@1:df8884d38960, 2018-10-28 (annotated)
- Committer:
- vazbyte
- Date:
- Sun Oct 28 12:51:59 2018 +0000
- Revision:
- 1:df8884d38960
- Parent:
- 0:07212de2fec1
- Child:
- 2:1957a4985d6e
still working dayum
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 | 0:07212de2fec1 | 18 | #include <mbed.h> |
vazbyte | 1:df8884d38960 | 19 | #include "MPU9250.h" |
vazbyte | 0:07212de2fec1 | 20 | #include "ble/BLE.h" |
vazbyte | 0:07212de2fec1 | 21 | #include "ble/Gap.h" |
vazbyte | 1:df8884d38960 | 22 | #include "ble/services/HeartRateService.h" |
vazbyte | 1:df8884d38960 | 23 | |
vazbyte | 1:df8884d38960 | 24 | #define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral |
vazbyte | 1:df8884d38960 | 25 | #define Ki 0.0f |
vazbyte | 1:df8884d38960 | 26 | |
vazbyte | 1:df8884d38960 | 27 | //****************************************************************************** |
vazbyte | 1:df8884d38960 | 28 | MPU9250::MPU9250(PinName sda, PinName scl) |
vazbyte | 1:df8884d38960 | 29 | { |
vazbyte | 1:df8884d38960 | 30 | i2c_ = new I2C(sda, scl); |
vazbyte | 1:df8884d38960 | 31 | i2c_->frequency(400000); |
vazbyte | 1:df8884d38960 | 32 | } |
vazbyte | 1:df8884d38960 | 33 | |
vazbyte | 1:df8884d38960 | 34 | //****************************************************************************** |
vazbyte | 1:df8884d38960 | 35 | MPU9250::MPU9250(I2C *i2c):i2c_(i2c){} |
vazbyte | 1:df8884d38960 | 36 | |
vazbyte | 1:df8884d38960 | 37 | //****************************************************************************** |
vazbyte | 0:07212de2fec1 | 38 | |
vazbyte | 0:07212de2fec1 | 39 | DigitalOut led1(LED1, 1); |
vazbyte | 0:07212de2fec1 | 40 | |
vazbyte | 1:df8884d38960 | 41 | const static char DEVICE_NAME[] = "ProVaida"; |
vazbyte | 1:df8884d38960 | 42 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; |
vazbyte | 0:07212de2fec1 | 43 | |
vazbyte | 1:df8884d38960 | 44 | uint8_t hrmCounter = 100; |
vazbyte | 1:df8884d38960 | 45 | static HeartRateService* hrService; |
vazbyte | 0:07212de2fec1 | 46 | |
vazbyte | 0:07212de2fec1 | 47 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
vazbyte | 0:07212de2fec1 | 48 | |
vazbyte | 0:07212de2fec1 | 49 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
vazbyte | 0:07212de2fec1 | 50 | { |
vazbyte | 0:07212de2fec1 | 51 | BLE::Instance().gap().startAdvertising(); |
vazbyte | 0:07212de2fec1 | 52 | } |
vazbyte | 0:07212de2fec1 | 53 | |
vazbyte | 1:df8884d38960 | 54 | |
vazbyte | 1:df8884d38960 | 55 | |
vazbyte | 1:df8884d38960 | 56 | |
vazbyte | 1:df8884d38960 | 57 | |
vazbyte | 1:df8884d38960 | 58 | |
vazbyte | 1:df8884d38960 | 59 | |
vazbyte | 0:07212de2fec1 | 60 | void updateSensorValue() { |
vazbyte | 1:df8884d38960 | 61 | hrmCounter++; |
vazbyte | 1:df8884d38960 | 62 | if (hrmCounter > 175) { |
vazbyte | 1:df8884d38960 | 63 | hrmCounter = 100; |
vazbyte | 0:07212de2fec1 | 64 | } |
vazbyte | 1:df8884d38960 | 65 | hrService->updateHeartRate(hrmCounter); |
vazbyte | 0:07212de2fec1 | 66 | } |
vazbyte | 0:07212de2fec1 | 67 | |
vazbyte | 0:07212de2fec1 | 68 | void blinkCallback(void) |
vazbyte | 0:07212de2fec1 | 69 | { |
vazbyte | 0:07212de2fec1 | 70 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
vazbyte | 0:07212de2fec1 | 71 | |
vazbyte | 0:07212de2fec1 | 72 | BLE &ble = BLE::Instance(); |
vazbyte | 0:07212de2fec1 | 73 | if (ble.gap().getState().connected) { |
vazbyte | 0:07212de2fec1 | 74 | eventQueue.call(updateSensorValue); |
vazbyte | 0:07212de2fec1 | 75 | } |
vazbyte | 0:07212de2fec1 | 76 | } |
vazbyte | 0:07212de2fec1 | 77 | |
vazbyte | 0:07212de2fec1 | 78 | /** |
vazbyte | 1:df8884d38960 | 79 | * This function is called when the ble initialization process has failed |
vazbyte | 0:07212de2fec1 | 80 | */ |
vazbyte | 0:07212de2fec1 | 81 | void onBleInitError(BLE &ble, ble_error_t error) |
vazbyte | 0:07212de2fec1 | 82 | { |
vazbyte | 0:07212de2fec1 | 83 | /* Initialization error handling should go here */ |
vazbyte | 0:07212de2fec1 | 84 | } |
vazbyte | 0:07212de2fec1 | 85 | |
vazbyte | 0:07212de2fec1 | 86 | void printMacAddress() |
vazbyte | 0:07212de2fec1 | 87 | { |
vazbyte | 0:07212de2fec1 | 88 | /* Print out device MAC address to the console*/ |
vazbyte | 0:07212de2fec1 | 89 | Gap::AddressType_t addr_type; |
vazbyte | 0:07212de2fec1 | 90 | Gap::Address_t address; |
vazbyte | 0:07212de2fec1 | 91 | BLE::Instance().gap().getAddress(&addr_type, address); |
vazbyte | 0:07212de2fec1 | 92 | printf("DEVICE MAC ADDRESS: "); |
vazbyte | 0:07212de2fec1 | 93 | for (int i = 5; i >= 1; i--){ |
vazbyte | 0:07212de2fec1 | 94 | printf("%02x:", address[i]); |
vazbyte | 0:07212de2fec1 | 95 | } |
vazbyte | 0:07212de2fec1 | 96 | printf("%02x\r\n", address[0]); |
vazbyte | 0:07212de2fec1 | 97 | } |
vazbyte | 0:07212de2fec1 | 98 | |
vazbyte | 0:07212de2fec1 | 99 | /** |
vazbyte | 0:07212de2fec1 | 100 | * Callback triggered when the ble initialization process has finished |
vazbyte | 0:07212de2fec1 | 101 | */ |
vazbyte | 0:07212de2fec1 | 102 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
vazbyte | 0:07212de2fec1 | 103 | { |
vazbyte | 0:07212de2fec1 | 104 | BLE& ble = params->ble; |
vazbyte | 0:07212de2fec1 | 105 | ble_error_t error = params->error; |
vazbyte | 0:07212de2fec1 | 106 | |
vazbyte | 0:07212de2fec1 | 107 | if (error != BLE_ERROR_NONE) { |
vazbyte | 0:07212de2fec1 | 108 | /* In case of error, forward the error handling to onBleInitError */ |
vazbyte | 0:07212de2fec1 | 109 | onBleInitError(ble, error); |
vazbyte | 0:07212de2fec1 | 110 | return; |
vazbyte | 0:07212de2fec1 | 111 | } |
vazbyte | 0:07212de2fec1 | 112 | |
vazbyte | 0:07212de2fec1 | 113 | /* Ensure that it is the default instance of BLE */ |
vazbyte | 0:07212de2fec1 | 114 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
vazbyte | 0:07212de2fec1 | 115 | return; |
vazbyte | 0:07212de2fec1 | 116 | } |
vazbyte | 0:07212de2fec1 | 117 | |
vazbyte | 0:07212de2fec1 | 118 | ble.gap().onDisconnection(disconnectionCallback); |
vazbyte | 0:07212de2fec1 | 119 | |
vazbyte | 0:07212de2fec1 | 120 | /* Setup primary service */ |
vazbyte | 1:df8884d38960 | 121 | hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
vazbyte | 0:07212de2fec1 | 122 | |
vazbyte | 0:07212de2fec1 | 123 | /* Setup advertising */ |
vazbyte | 0:07212de2fec1 | 124 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
vazbyte | 0:07212de2fec1 | 125 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
vazbyte | 0:07212de2fec1 | 126 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
vazbyte | 0:07212de2fec1 | 127 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
vazbyte | 0:07212de2fec1 | 128 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
vazbyte | 0:07212de2fec1 | 129 | ble.gap().startAdvertising(); |
vazbyte | 0:07212de2fec1 | 130 | |
vazbyte | 0:07212de2fec1 | 131 | printMacAddress(); |
vazbyte | 0:07212de2fec1 | 132 | } |
vazbyte | 0:07212de2fec1 | 133 | |
vazbyte | 0:07212de2fec1 | 134 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
vazbyte | 0:07212de2fec1 | 135 | BLE &ble = BLE::Instance(); |
vazbyte | 0:07212de2fec1 | 136 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
vazbyte | 0:07212de2fec1 | 137 | } |
vazbyte | 0:07212de2fec1 | 138 | |
vazbyte | 0:07212de2fec1 | 139 | int main() |
vazbyte | 0:07212de2fec1 | 140 | { |
vazbyte | 0:07212de2fec1 | 141 | eventQueue.call_every(500, blinkCallback); |
vazbyte | 0:07212de2fec1 | 142 | |
vazbyte | 0:07212de2fec1 | 143 | BLE &ble = BLE::Instance(); |
vazbyte | 0:07212de2fec1 | 144 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
vazbyte | 0:07212de2fec1 | 145 | ble.init(bleInitComplete); |
vazbyte | 0:07212de2fec1 | 146 | |
vazbyte | 0:07212de2fec1 | 147 | eventQueue.dispatch_forever(); |
vazbyte | 0:07212de2fec1 | 148 | |
vazbyte | 0:07212de2fec1 | 149 | return 0; |
vazbyte | 0:07212de2fec1 | 150 | } |