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

Committer:
vaida
Date:
Tue Oct 30 17:20:10 2018 +0000
Revision:
6:887df9bbe705
Parent:
5:c75ea8bfa9fb
Child:
7:9e2172e6550a
changed value types and added printing

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 0:07212de2fec1 19 #include <mbed.h>
vazbyte 1:df8884d38960 20 #include "MPU9250.h"
vazbyte 0:07212de2fec1 21 #include "ble/BLE.h"
vazbyte 0:07212de2fec1 22 #include "ble/Gap.h"
vazbyte 1:df8884d38960 23 #include "ble/services/HeartRateService.h"
vazbyte 1:df8884d38960 24
vazbyte 0:07212de2fec1 25
vazbyte 0:07212de2fec1 26 DigitalOut led1(LED1, 1);
vazbyte 0:07212de2fec1 27
vazbyte 1:df8884d38960 28 const static char DEVICE_NAME[] = "ProVaida";
vazbyte 1:df8884d38960 29 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
vazbyte 0:07212de2fec1 30
vazbyte 4:af0530752b76 31 int16_t destination[3];
vazbyte 5:c75ea8bfa9fb 32 //uint8_t hrmCounter = 0;
vazbyte 5:c75ea8bfa9fb 33 int16_t hrmCounter = 0;
vazbyte 1:df8884d38960 34 static HeartRateService* hrService;
vazbyte 4:af0530752b76 35 //MPU9250 mpu = MPU9250(p26, p27);
vazbyte 4:af0530752b76 36 MPU9250 mpu = MPU9250(P0_26, P0_27);
vazbyte 0:07212de2fec1 37
vazbyte 0:07212de2fec1 38 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
vazbyte 0:07212de2fec1 39
vazbyte 0:07212de2fec1 40 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
vazbyte 0:07212de2fec1 41 {
vazbyte 0:07212de2fec1 42 BLE::Instance().gap().startAdvertising();
vazbyte 0:07212de2fec1 43 }
vazbyte 0:07212de2fec1 44
vazbyte 2:1957a4985d6e 45 void updateSensorValue() {
vazbyte 3:a33709dad06c 46 mpu.readAccelData(destination);
vazbyte 3:a33709dad06c 47
vaida 6:887df9bbe705 48 int16_t acc_x = destination[0] / 1000.0;
vaida 6:887df9bbe705 49 int16_t acc_y = destination[1] / 1000.0;
vaida 6:887df9bbe705 50 int16_t acc_z = destination[2] / 1000.0;
vazbyte 3:a33709dad06c 51
vaida 6:887df9bbe705 52 int16_t sqr_acc_x = acc_x*acc_x;
vaida 6:887df9bbe705 53 int16_t sqr_acc_y = acc_y*acc_y;
vaida 6:887df9bbe705 54 int16_t sqr_acc_z = acc_z*acc_z;
vaida 6:887df9bbe705 55 printf("sqr_acc_x: " );
vaida 6:887df9bbe705 56 printf("%d\n", sqr_acc_x);
vazbyte 3:a33709dad06c 57
vazbyte 3:a33709dad06c 58 double sum_acc = sqr_acc_x + sqr_acc_y + sqr_acc_z;
vaida 6:887df9bbe705 59 printf("sum_acc: " );
vaida 6:887df9bbe705 60 printf("%f\n", sum_acc);
vazbyte 3:a33709dad06c 61
vaida 6:887df9bbe705 62 int16_t value = sqrt(sum_acc);
vazbyte 4:af0530752b76 63 hrmCounter = acc_x;
vaida 6:887df9bbe705 64 printf("value: " );
vaida 6:887df9bbe705 65 printf("%d\n", value);
vaida 6:887df9bbe705 66 /* printf("%u\n", (unsigned int)acc_x);*/
vazbyte 1:df8884d38960 67
vazbyte 1:df8884d38960 68 hrService->updateHeartRate(hrmCounter);
vazbyte 0:07212de2fec1 69 }
vazbyte 0:07212de2fec1 70
vazbyte 0:07212de2fec1 71 void blinkCallback(void)
vazbyte 0:07212de2fec1 72 {
vazbyte 0:07212de2fec1 73 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
vazbyte 0:07212de2fec1 74 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 75 if (ble.gap().getState().connected) {
vazbyte 0:07212de2fec1 76 eventQueue.call(updateSensorValue);
vazbyte 0:07212de2fec1 77 }
vazbyte 0:07212de2fec1 78 }
vazbyte 0:07212de2fec1 79
vazbyte 0:07212de2fec1 80 /**
vazbyte 1:df8884d38960 81 * This function is called when the ble initialization process has failed
vazbyte 0:07212de2fec1 82 */
vazbyte 0:07212de2fec1 83 void onBleInitError(BLE &ble, ble_error_t error)
vazbyte 0:07212de2fec1 84 {
vazbyte 0:07212de2fec1 85 /* Initialization error handling should go here */
vazbyte 0:07212de2fec1 86 }
vazbyte 0:07212de2fec1 87
vazbyte 0:07212de2fec1 88 void printMacAddress()
vazbyte 0:07212de2fec1 89 {
vazbyte 0:07212de2fec1 90 /* Print out device MAC address to the console*/
vazbyte 0:07212de2fec1 91 Gap::AddressType_t addr_type;
vazbyte 0:07212de2fec1 92 Gap::Address_t address;
vazbyte 0:07212de2fec1 93 BLE::Instance().gap().getAddress(&addr_type, address);
vazbyte 0:07212de2fec1 94 printf("DEVICE MAC ADDRESS: ");
vazbyte 0:07212de2fec1 95 for (int i = 5; i >= 1; i--){
vazbyte 0:07212de2fec1 96 printf("%02x:", address[i]);
vazbyte 0:07212de2fec1 97 }
vazbyte 0:07212de2fec1 98 printf("%02x\r\n", address[0]);
vazbyte 0:07212de2fec1 99 }
vazbyte 0:07212de2fec1 100
vazbyte 0:07212de2fec1 101 /**
vazbyte 0:07212de2fec1 102 * Callback triggered when the ble initialization process has finished
vazbyte 0:07212de2fec1 103 */
vazbyte 0:07212de2fec1 104 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
vazbyte 0:07212de2fec1 105 {
vazbyte 0:07212de2fec1 106 BLE& ble = params->ble;
vazbyte 0:07212de2fec1 107 ble_error_t error = params->error;
vazbyte 0:07212de2fec1 108
vazbyte 0:07212de2fec1 109 if (error != BLE_ERROR_NONE) {
vazbyte 0:07212de2fec1 110 /* In case of error, forward the error handling to onBleInitError */
vazbyte 0:07212de2fec1 111 onBleInitError(ble, error);
vazbyte 0:07212de2fec1 112 return;
vazbyte 0:07212de2fec1 113 }
vazbyte 0:07212de2fec1 114
vazbyte 0:07212de2fec1 115 /* Ensure that it is the default instance of BLE */
vazbyte 0:07212de2fec1 116 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
vazbyte 0:07212de2fec1 117 return;
vazbyte 0:07212de2fec1 118 }
vazbyte 0:07212de2fec1 119
vazbyte 0:07212de2fec1 120 ble.gap().onDisconnection(disconnectionCallback);
vazbyte 0:07212de2fec1 121
vazbyte 0:07212de2fec1 122 /* Setup primary service */
vazbyte 1:df8884d38960 123 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
vazbyte 0:07212de2fec1 124
vazbyte 0:07212de2fec1 125 /* Setup advertising */
vazbyte 0:07212de2fec1 126 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vazbyte 0:07212de2fec1 127 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
vazbyte 0:07212de2fec1 128 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
vazbyte 0:07212de2fec1 129 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vazbyte 0:07212de2fec1 130 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
vazbyte 0:07212de2fec1 131 ble.gap().startAdvertising();
vazbyte 0:07212de2fec1 132
vaida 6:887df9bbe705 133
vazbyte 0:07212de2fec1 134 printMacAddress();
vaida 6:887df9bbe705 135
vazbyte 0:07212de2fec1 136 }
vazbyte 0:07212de2fec1 137
vazbyte 0:07212de2fec1 138 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
vazbyte 0:07212de2fec1 139 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 140 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
vazbyte 0:07212de2fec1 141 }
vazbyte 0:07212de2fec1 142
vazbyte 0:07212de2fec1 143 int main()
vazbyte 4:af0530752b76 144 {
vazbyte 2:1957a4985d6e 145 mpu.initMPU9250();
vazbyte 4:af0530752b76 146 eventQueue.call_every(400, blinkCallback);
vazbyte 0:07212de2fec1 147
vazbyte 0:07212de2fec1 148 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 149 ble.onEventsToProcess(scheduleBleEventsProcessing);
vazbyte 0:07212de2fec1 150 ble.init(bleInitComplete);
vazbyte 0:07212de2fec1 151
vazbyte 0:07212de2fec1 152 eventQueue.dispatch_forever();
vazbyte 0:07212de2fec1 153
vazbyte 0:07212de2fec1 154 return 0;
vazbyte 0:07212de2fec1 155 }