bike service data logging

Dependencies:   BLE_API DataLogging X_NUCLEO_IDB0XA1 mbed

Committer:
ptuytsch
Date:
Sat Apr 16 15:38:40 2016 +0000
Revision:
1:e104d15b8344
Parent:
0:6a263a6fb8cf
Bike service/data logging

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptuytsch 0:6a263a6fb8cf 1 /* mbed Microcontroller Library
ptuytsch 0:6a263a6fb8cf 2 * Copyright (c) 2006-2013 ARM Limited
ptuytsch 0:6a263a6fb8cf 3 *
ptuytsch 0:6a263a6fb8cf 4 * Licensed under the Apache License, Version 2.0 (the "License");
ptuytsch 0:6a263a6fb8cf 5 * you may not use this file except in compliance with the License.
ptuytsch 0:6a263a6fb8cf 6 * You may obtain a copy of the License at
ptuytsch 0:6a263a6fb8cf 7 *
ptuytsch 0:6a263a6fb8cf 8 * http://www.apache.org/licenses/LICENSE-2.0
ptuytsch 0:6a263a6fb8cf 9 *
ptuytsch 0:6a263a6fb8cf 10 * Unless required by applicable law or agreed to in writing, software
ptuytsch 0:6a263a6fb8cf 11 * distributed under the License is distributed on an "AS IS" BASIS,
ptuytsch 0:6a263a6fb8cf 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ptuytsch 0:6a263a6fb8cf 13 * See the License for the specific language governing permissions and
ptuytsch 0:6a263a6fb8cf 14 * limitations under the License.
ptuytsch 0:6a263a6fb8cf 15 */
ptuytsch 0:6a263a6fb8cf 16
ptuytsch 0:6a263a6fb8cf 17 #include "mbed.h"
ptuytsch 0:6a263a6fb8cf 18 #include "Data.h"
ptuytsch 0:6a263a6fb8cf 19 #include "ble/BLE.h"
ptuytsch 0:6a263a6fb8cf 20 //#include "ble/services/ButtonService.h"
ptuytsch 0:6a263a6fb8cf 21 #include "ble/services/BikeService.h"
ptuytsch 0:6a263a6fb8cf 22 #include "ble/services/HealthThermometerService.h"
ptuytsch 0:6a263a6fb8cf 23 //#include "ble/services/BatteryService.h"
ptuytsch 0:6a263a6fb8cf 24
ptuytsch 0:6a263a6fb8cf 25 //DigitalOut led1(LED1);
ptuytsch 0:6a263a6fb8cf 26 //InterruptIn button(PC_13);
ptuytsch 0:6a263a6fb8cf 27
ptuytsch 0:6a263a6fb8cf 28 const static char DEVICE_NAME[] = "Veloke";
ptuytsch 0:6a263a6fb8cf 29 static const uint16_t uuid16_list[] = {BikeService::BIKE_SERVICE_UUID, /*GattService::UUID_BATTERY_SERVICE*/ /*ButtonService::BUTTON_SERVICE_UUID*/};
ptuytsch 0:6a263a6fb8cf 30 Data d(PC_13);
ptuytsch 0:6a263a6fb8cf 31
ptuytsch 0:6a263a6fb8cf 32 /*enum {
ptuytsch 0:6a263a6fb8cf 33 RELEASED = 0,
ptuytsch 0:6a263a6fb8cf 34 PRESSED,
ptuytsch 0:6a263a6fb8cf 35 IDLE
ptuytsch 0:6a263a6fb8cf 36 };
ptuytsch 0:6a263a6fb8cf 37 static uint8_t buttonState = IDLE;*/
ptuytsch 0:6a263a6fb8cf 38
ptuytsch 0:6a263a6fb8cf 39 //static ButtonService *buttonServicePtr;
ptuytsch 0:6a263a6fb8cf 40 static BikeService *bikeServicePtr;
ptuytsch 0:6a263a6fb8cf 41 //static BatteryService *bsp;
ptuytsch 0:6a263a6fb8cf 42 static bool update = false;
ptuytsch 0:6a263a6fb8cf 43 //static uint8_t afstand = 0;
ptuytsch 0:6a263a6fb8cf 44 //static uint8_t gemiddelde = 0;
ptuytsch 0:6a263a6fb8cf 45
ptuytsch 0:6a263a6fb8cf 46 /*void buttonPressedCallback(void)
ptuytsch 0:6a263a6fb8cf 47 {*/
ptuytsch 0:6a263a6fb8cf 48 /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
ptuytsch 0:6a263a6fb8cf 49 * BLE device API from the main thread. */
ptuytsch 0:6a263a6fb8cf 50 /* printf("pushed\n");
ptuytsch 0:6a263a6fb8cf 51 counter++;
ptuytsch 0:6a263a6fb8cf 52 buttonState = PRESSED;
ptuytsch 0:6a263a6fb8cf 53 }*/
ptuytsch 0:6a263a6fb8cf 54
ptuytsch 0:6a263a6fb8cf 55 /*void buttonReleasedCallback(void)
ptuytsch 0:6a263a6fb8cf 56 {*/
ptuytsch 0:6a263a6fb8cf 57 /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
ptuytsch 0:6a263a6fb8cf 58 * BLE device API from the main thread. */
ptuytsch 0:6a263a6fb8cf 59 /* printf("released\n");
ptuytsch 0:6a263a6fb8cf 60 buttonState = RELEASED;
ptuytsch 0:6a263a6fb8cf 61 }*/
ptuytsch 0:6a263a6fb8cf 62
ptuytsch 0:6a263a6fb8cf 63 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
ptuytsch 0:6a263a6fb8cf 64 {
ptuytsch 0:6a263a6fb8cf 65 BLE::Instance().gap().startAdvertising();
ptuytsch 0:6a263a6fb8cf 66 }
ptuytsch 0:6a263a6fb8cf 67
ptuytsch 0:6a263a6fb8cf 68 void periodicCallback(void)
ptuytsch 0:6a263a6fb8cf 69 {
ptuytsch 0:6a263a6fb8cf 70 //led1 = !led1; Do blinky on LED1 to indicate system aliveness.
ptuytsch 0:6a263a6fb8cf 71 //afstand = (uint8_t)d.getDistance();
ptuytsch 0:6a263a6fb8cf 72 //gemiddelde = (uint8_t)d.getAverage();
ptuytsch 0:6a263a6fb8cf 73 printf("distance: %f \r\nTime Passed: %i:%i\nAverage: %f\nSpeed: %f\n\n",d.getDistance(), d.getTime()/60, d.getTime()%60, d.getAverage(), d.getSpeed());
ptuytsch 0:6a263a6fb8cf 74 update = true;
ptuytsch 0:6a263a6fb8cf 75 }
ptuytsch 0:6a263a6fb8cf 76
ptuytsch 0:6a263a6fb8cf 77 /**
ptuytsch 0:6a263a6fb8cf 78 * This function is called when the ble initialization process has failled
ptuytsch 0:6a263a6fb8cf 79 */
ptuytsch 0:6a263a6fb8cf 80 void onBleInitError(BLE &ble, ble_error_t error)
ptuytsch 0:6a263a6fb8cf 81 {
ptuytsch 0:6a263a6fb8cf 82 /* Initialization error handling should go here */
ptuytsch 0:6a263a6fb8cf 83 }
ptuytsch 0:6a263a6fb8cf 84
ptuytsch 0:6a263a6fb8cf 85 /**
ptuytsch 0:6a263a6fb8cf 86 * Callback triggered when the ble initialization process has finished
ptuytsch 0:6a263a6fb8cf 87 */
ptuytsch 0:6a263a6fb8cf 88 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ptuytsch 0:6a263a6fb8cf 89 {
ptuytsch 0:6a263a6fb8cf 90 BLE& ble = params->ble;
ptuytsch 0:6a263a6fb8cf 91 ble_error_t error = params->error;
ptuytsch 0:6a263a6fb8cf 92
ptuytsch 0:6a263a6fb8cf 93 if (error != BLE_ERROR_NONE) {
ptuytsch 0:6a263a6fb8cf 94 /* In case of error, forward the error handling to onBleInitError */
ptuytsch 0:6a263a6fb8cf 95 onBleInitError(ble, error);
ptuytsch 0:6a263a6fb8cf 96 return;
ptuytsch 0:6a263a6fb8cf 97 }
ptuytsch 0:6a263a6fb8cf 98
ptuytsch 0:6a263a6fb8cf 99 /* Ensure that it is the default instance of BLE */
ptuytsch 0:6a263a6fb8cf 100 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
ptuytsch 0:6a263a6fb8cf 101 return;
ptuytsch 0:6a263a6fb8cf 102 }
ptuytsch 0:6a263a6fb8cf 103
ptuytsch 0:6a263a6fb8cf 104 ble.gap().onDisconnection(disconnectionCallback);
ptuytsch 0:6a263a6fb8cf 105
ptuytsch 0:6a263a6fb8cf 106 /* Setup primary service */
ptuytsch 0:6a263a6fb8cf 107 bikeServicePtr = new BikeService(ble,d.getDistance(),d.getAverage()/*, false*/ /* initial value for button pressed */);
ptuytsch 0:6a263a6fb8cf 108 //bsp = new BatteryService(ble,counter);
ptuytsch 0:6a263a6fb8cf 109 //buttonServicePtr = new ButtonService(ble,false);
ptuytsch 0:6a263a6fb8cf 110
ptuytsch 0:6a263a6fb8cf 111 /* setup advertising */
ptuytsch 0:6a263a6fb8cf 112 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ptuytsch 0:6a263a6fb8cf 113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ptuytsch 0:6a263a6fb8cf 114 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ptuytsch 0:6a263a6fb8cf 115 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ptuytsch 0:6a263a6fb8cf 116 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ptuytsch 0:6a263a6fb8cf 117 ble.gap().startAdvertising();
ptuytsch 0:6a263a6fb8cf 118
ptuytsch 0:6a263a6fb8cf 119 }
ptuytsch 0:6a263a6fb8cf 120
ptuytsch 0:6a263a6fb8cf 121 int main(void)
ptuytsch 0:6a263a6fb8cf 122 {
ptuytsch 0:6a263a6fb8cf 123
ptuytsch 0:6a263a6fb8cf 124
ptuytsch 0:6a263a6fb8cf 125 //led1 = 1;
ptuytsch 0:6a263a6fb8cf 126 Ticker ticker;
ptuytsch 0:6a263a6fb8cf 127 ticker.attach(periodicCallback, 1);
ptuytsch 0:6a263a6fb8cf 128 //button.fall(buttonPressedCallback);
ptuytsch 0:6a263a6fb8cf 129 //button.rise(buttonReleasedCallback);
ptuytsch 0:6a263a6fb8cf 130
ptuytsch 0:6a263a6fb8cf 131 BLE &ble = BLE::Instance();
ptuytsch 0:6a263a6fb8cf 132 BLE::Instance().init(bleInitComplete);
ptuytsch 0:6a263a6fb8cf 133
ptuytsch 0:6a263a6fb8cf 134 /* SpinWait for initialization to complete. This is necessary because the
ptuytsch 0:6a263a6fb8cf 135 * BLE object is used in the main loop below. */
ptuytsch 0:6a263a6fb8cf 136 while (ble.hasInitialized() == false); /* spin loop */
ptuytsch 0:6a263a6fb8cf 137
ptuytsch 0:6a263a6fb8cf 138 /*while (true) {
ptuytsch 0:6a263a6fb8cf 139 if (buttonState != IDLE) {
ptuytsch 0:6a263a6fb8cf 140 bikeServicePtr->updateBikeDistance(counter);
ptuytsch 0:6a263a6fb8cf 141 buttonState = IDLE;
ptuytsch 0:6a263a6fb8cf 142 }
ptuytsch 0:6a263a6fb8cf 143
ptuytsch 0:6a263a6fb8cf 144 ble.waitForEvent();
ptuytsch 0:6a263a6fb8cf 145 }*/
ptuytsch 0:6a263a6fb8cf 146 while(true){
ptuytsch 0:6a263a6fb8cf 147 if (update == true){
ptuytsch 0:6a263a6fb8cf 148 bikeServicePtr->updateBikeDistance(d.getDistance());
ptuytsch 0:6a263a6fb8cf 149 bikeServicePtr->updateBikeAverage(d.getAverage());
ptuytsch 0:6a263a6fb8cf 150 bikeServicePtr->updateBikePulse(d.getLastCount());
ptuytsch 0:6a263a6fb8cf 151 //test = (int)d.getDistance();
ptuytsch 0:6a263a6fb8cf 152 //bsp->updateBatteryLevel(test);
ptuytsch 0:6a263a6fb8cf 153 update = false;
ptuytsch 0:6a263a6fb8cf 154 }
ptuytsch 0:6a263a6fb8cf 155 ble.waitForEvent();
ptuytsch 0:6a263a6fb8cf 156 }
ptuytsch 0:6a263a6fb8cf 157 }