bike service data logging

Dependencies:   BLE_API DataLogging X_NUCLEO_IDB0XA1 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 #include "mbed.h"
00018 #include "Data.h"
00019 #include "ble/BLE.h"
00020 //#include "ble/services/ButtonService.h"
00021 #include "ble/services/BikeService.h"
00022 #include "ble/services/HealthThermometerService.h"
00023 //#include "ble/services/BatteryService.h"
00024  
00025 //DigitalOut  led1(LED1);
00026 //InterruptIn button(PC_13);
00027  
00028 const static char     DEVICE_NAME[] = "Veloke";
00029 static const uint16_t uuid16_list[] = {BikeService::BIKE_SERVICE_UUID, /*GattService::UUID_BATTERY_SERVICE*/ /*ButtonService::BUTTON_SERVICE_UUID*/};
00030 Data d(PC_13);
00031  
00032 /*enum {
00033     RELEASED = 0,
00034     PRESSED,
00035     IDLE
00036 };
00037 static uint8_t buttonState = IDLE;*/
00038  
00039 //static ButtonService *buttonServicePtr;
00040  static BikeService *bikeServicePtr;
00041  //static BatteryService *bsp;
00042  static bool update = false;
00043  //static uint8_t afstand = 0;
00044  //static uint8_t gemiddelde = 0;
00045  
00046 /*void buttonPressedCallback(void)
00047 {*/
00048     /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
00049      * BLE device API from the main thread. */
00050  /*    printf("pushed\n");
00051      counter++;
00052     buttonState = PRESSED;
00053 }*/
00054  
00055 /*void buttonReleasedCallback(void)
00056 {*/
00057     /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
00058      * BLE device API from the main thread. */
00059   /*   printf("released\n");
00060     buttonState = RELEASED;
00061 }*/
00062 
00063 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00064 {
00065     BLE::Instance().gap().startAdvertising();
00066 }
00067  
00068 void periodicCallback(void)
00069 {
00070     //led1 = !led1;  Do blinky on LED1 to indicate system aliveness.
00071     //afstand = (uint8_t)d.getDistance();
00072     //gemiddelde = (uint8_t)d.getAverage();
00073     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());
00074     update = true;
00075 }
00076  
00077 /**
00078  * This function is called when the ble initialization process has failled
00079  */
00080 void onBleInitError(BLE &ble, ble_error_t error)
00081 {
00082     /* Initialization error handling should go here */
00083 }
00084  
00085 /**
00086  * Callback triggered when the ble initialization process has finished
00087  */
00088 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00089 {
00090     BLE&        ble   = params->ble;
00091     ble_error_t error = params->error;
00092  
00093     if (error != BLE_ERROR_NONE) {
00094         /* In case of error, forward the error handling to onBleInitError */
00095         onBleInitError(ble, error);
00096         return;
00097     }
00098  
00099     /* Ensure that it is the default instance of BLE */
00100     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00101         return;
00102     }
00103  
00104     ble.gap().onDisconnection(disconnectionCallback);
00105  
00106     /* Setup primary service */
00107     bikeServicePtr = new BikeService(ble,d.getDistance(),d.getAverage()/*, false*/ /* initial value for button pressed */);
00108     //bsp = new BatteryService(ble,counter);
00109     //buttonServicePtr = new ButtonService(ble,false);
00110  
00111     /* setup advertising */
00112     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00113     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00114     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00115     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00116     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00117     ble.gap().startAdvertising();
00118  
00119 }
00120  
00121 int main(void)
00122 {
00123     
00124     
00125     //led1 = 1;
00126     Ticker ticker;
00127     ticker.attach(periodicCallback, 1);
00128     //button.fall(buttonPressedCallback);
00129     //button.rise(buttonReleasedCallback);
00130  
00131     BLE &ble = BLE::Instance();
00132     BLE::Instance().init(bleInitComplete);
00133     
00134     /* SpinWait for initialization to complete. This is necessary because the
00135      * BLE object is used in the main loop below. */
00136     while (ble.hasInitialized()  == false);  /* spin loop */ 
00137     
00138     /*while (true) {
00139         if (buttonState != IDLE) {
00140             bikeServicePtr->updateBikeDistance(counter);
00141             buttonState = IDLE;
00142         }
00143  
00144         ble.waitForEvent();
00145     }*/
00146     while(true){
00147         if (update == true){
00148             bikeServicePtr->updateBikeDistance(d.getDistance());
00149             bikeServicePtr->updateBikeAverage(d.getAverage());
00150             bikeServicePtr->updateBikePulse(d.getLastCount());
00151             //test = (int)d.getDistance();
00152             //bsp->updateBatteryLevel(test);
00153             update = false;
00154             }
00155         ble.waitForEvent();
00156         }
00157 }