embedded software for tortuga bike

Dependencies:   BLE_API BikeControl DataLogging X_NUCLEO_IDB0XA1 _24LCXXX mbed

Revision:
0:0803cdf04c32
Child:
1:ffdec767aa55
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jul 15 12:49:59 2016 +0000
@@ -0,0 +1,253 @@
+#include "mbed.h"
+#include "BikeData.h"
+#include "BatteryState.h"
+#include "ble/BLE.h"
+#include "ble/services/BikeService.h"
+#include "ble/services/BikeBatteryService.h"
+
+#define trailerBatteryPin  PB_0
+#define bikeBatteryPin  PC_5
+#define auxiliaryBatteryPin  PC_4
+
+/***************************************************************************
+                                VARIABLES
+****************************************************************************/
+
+
+
+//3 battery states
+BatteryState *trailerBattery = new BatteryState(trailerBatteryPin,BatteryState::Battery48V);
+BatteryState *bikeBattery = new BatteryState(bikeBatteryPin,BatteryState::Battery48V);
+BatteryState *auxiliaryBattery = new BatteryState(auxiliaryBatteryPin,BatteryState::Battery48V);
+
+//BLE VARIABLES
+static char     DEVICE_NAME[] = "TORTUGA";  //Define default name of the BLE device
+static const uint16_t uuid16_list[] = {BikeService::BIKE_SERVICE_UUID, 
+                                        //GattService::UUID_DEVICE_INFORMATION_SERVICE,
+                                        BatteryService::BATTERY_SERVICE_UUID,
+                                        }; // List of the Service UUID's that are used.
+
+//Pointers to diferent objects and BLE services.
+static BikeData *bd;
+static BikeService *bikeServicePtr;
+//static DeviceInformationService *DISptr;
+static BatteryService *batSerPtr;
+
+//bool that is set to true each second.
+static bool update = false;
+static bool updateHalf = false;
+
+
+/***************************************************************************
+                                BLE FUNCTIONS
+****************************************************************************/
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    //restart advertising when disconnected
+    BLE::Instance().gap().startAdvertising();
+}
+ 
+void periodicCallback(void)
+{
+    //Set update high to send new data over BLE
+    update = true;
+}
+ 
+
+ //This function is called when the ble initialization process has failled
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    /* Initialization error handling should go here */
+    printf("initialization error!\n");
+}
+ 
+
+//Callback triggered when the ble initialization process has finished
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+ 
+    if (error != BLE_ERROR_NONE) {      /* In case of error, forward the error handling to onBleInitError */
+        printf("BLE error\n");
+        onBleInitError(ble, error);
+        return;
+    }
+ 
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+ 
+    //set the disconnect function
+    ble.gap().onDisconnection(disconnectionCallback);
+    printf("BLE Disconnect callback set\n");
+ 
+    /* Setup primary services */
+    //DISptr = new DeviceInformationService(ble, "The Opportunity Factory", "1.0", "PJTMN06", "1.0", "0.9", "0.9");
+    batSerPtr = new BatteryService(ble);
+    bikeServicePtr = new BikeService(ble,bd);
+    printf("Services set\n");
+ 
+    /* setup advertising */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);//set apperance
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //different BLE options set
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));//Adding the list of UUID's
+    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));//passing the device name
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, bd->getBikeNameSize());//passing the device name
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);//setting connection type
+    ble.gap().setAdvertisingInterval(100); /* 1000ms. */
+    ble.gap().startAdvertising();
+    printf("advertising\n");
+}
+
+
+int main() {
+    
+    
+    printf("\n======================\n\n");
+    printf("SETUP \n");
+    
+    bd = new BikeData(PC_1); // creating BikeData object
+    printf("BikeData object created\n");
+    
+    
+    uint8_t size = bd->getBikeNameSize();
+    printf("name length: %i\n", size);
+    if (size == 0){
+        printf("Setting Name First Time\n");
+        static char     testName[] = "TortugaPJT06";
+        bd -> setBikeName((char *)testName,sizeof(testName));
+        printf("SetFistTimeName\n");
+        //printf( "size: %i\n",bd -> getBikeNameSize());
+        //printf("first character: %c",bd->getBikeName());
+    }
+    
+    bd->getBikeName(DEVICE_NAME);
+    printf("got device name\n");
+    BLE &ble = BLE::Instance(); // creating BLE object
+    printf("BLE object created\n");
+    BLE::Instance().init(bleInitComplete); // call init function
+    
+    while (ble.hasInitialized()  == false); // Wait until BLE is initialized
+    printf("BLE object intitialized\n");
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1); //creating the ticker that wil update each second.
+    printf("ticker 1 attached\n");
+    
+    printf("START \n");
+    
+    
+    while(true)
+     {
+         if (update){
+            float speed = bd->getSpeed();
+            printf("speed: %f\n", speed);
+            
+            //printf("update bikeService\n");
+            bikeServicePtr->update(); //update the bikeserice
+            //printf("update battery1\n");
+            batSerPtr->updateBatteryLevel1(trailerBattery->getBatteryPercentage());
+            //printf("update battery2\n");
+            batSerPtr->updateBatteryLevel2(bikeBattery->getBatteryPercentage());
+            //printf("update battery3\n");
+            batSerPtr->updateBatteryLevel3(auxiliaryBattery->getBatteryPercentage());
+            update = false;
+        }
+        ble.waitForEvent(); //wait for an event when idle.
+            
+            
+    }
+    
+    /*printf("\n======================");
+    printf("\n\n");
+    
+    Ticker t1;
+    t1.attach(periodicCallback,1);
+    
+    for(uint64_t i = 0 ; i <0xFFFFF;i++);*/
+    //uint32_t dummy = 0x0002;
+    /*I2C i2c(PB_9, PB_8);
+    Serial pc(SERIAL_TX, SERIAL_RX);
+    _24LCXXX mem(&i2c);
+    
+    printf("\nsizeof dummy: %i\n", sizeof(dummy));
+    printf("Writing: %#x\n", dummy);
+    mem.nbyte_write(0,&dummy,4);
+    dummy+=0x1111;
+    printf("Writing: %#x\n", dummy);
+    mem.nbyte_write(4,&dummy,sizeof(dummy));
+    dummy+=0x1111;
+    printf("Writing: %#x\n", dummy);
+    mem.nbyte_write(8,&dummy,sizeof(dummy));
+    dummy+=0x1111;
+    printf("Writing: %#x\n", dummy);
+    mem.nbyte_write(12,&dummy,sizeof(dummy));
+    dummy+=0x1111;
+    printf("Writing: %#x\n", dummy);
+    mem.nbyte_write(16,&dummy,sizeof(dummy));
+    printf("Done Writing\n");
+    
+    for (uint64_t i = 0 ; i<0xFFFFF ; i++); // waiting between reading en writing
+    
+    dummy = 0x0000;
+    printf("reading\n");
+    mem.nbyte_read(0,&dummy,sizeof(dummy));
+    printf("    res: %#x\n",dummy);
+    dummy = 0x0000;
+    printf("reading\n");
+    mem.nbyte_read(4,&dummy,sizeof(dummy));
+    printf("    res: %#x\n",dummy);
+    dummy = 0x0000;
+    printf("reading\n");
+    mem.nbyte_read(8,&dummy,sizeof(dummy));
+    printf("    res: %#x\n",dummy);
+    dummy = 0x0000;
+    printf("reading\n");
+    mem.nbyte_read(12,&dummy,sizeof(dummy));
+    printf("    res: %#x\n",dummy);
+    dummy = 0x0000;
+    printf("reading\n");
+    mem.nbyte_read(16,&dummy,sizeof(dummy));
+    printf("    res: %#x\n",dummy);
+    printf("Done reading\n");*/
+    
+    
+    
+    /*printf("updating distance in eeprom\n");
+    float d = 1000.2;
+    if(!mem.nbyte_write(0,&d,sizeof(float))){
+        printf("eeprom writing error distance\n");
+        } // update the eeprom values
+    printf("updating time in eeprom\n");    
+    uint32_t t = 0x10101010;
+    if(!mem.nbyte_write(4,&t,sizeof(uint32_t))){
+        printf("eeprom writing error time\n");
+        }
+    
+    
+    
+    
+    bd = new BikeData(PC_0);
+    
+    for (int i = 9 ; i<16 ; i++){
+        char dummy;
+        mem.nbyte_read(i,&dummy,sizeof(dummy));
+        printf("char: %c\n", dummy);
+        }
+        
+    
+    while(1){
+        
+        
+        
+        if (update){
+            //printf("u");
+            //printf("update\n");
+            float distance = bd->getDataSet(BikeData::OVERALL)->getDistance();
+            printf("bikedata overall distance: %f\n",distance);
+            update = false;
+            }   
+    }    */
+}