BLE Library with custom services for the tortuga bike

Dependents:   TORTUGA_BLE

Fork of BLE_API by aapje monkey

Revision:
1203:3bc52a68543f
Parent:
1202:a31d51d9fba8
diff -r a31d51d9fba8 -r 3bc52a68543f ble/services/BikeService.h
--- a/ble/services/BikeService.h	Sat Apr 30 13:29:42 2016 +0000
+++ b/ble/services/BikeService.h	Sat Apr 30 23:10:50 2016 +0000
@@ -1,67 +1,163 @@
 #ifndef __BLE_BIKE_SERVICE_H__
 #define __BLE_BIKE_SERVICE_H__
- 
+
+#include "BikeData.h"
 class BikeService {
 public:
+/*
+    UUID LIST:
+    0x6969: SERVICE
+        0x696A: Trip Distance
+        0x696B: Trip Average
+        0x696C: Trip Time (in seconds)
+        
+        0x696D: Overall Distance
+        0x696E: Overall Average
+        0x696F: Overall Time (in seconds)
+        
+        0x6970: Since Battery Change Distance
+        0x6971: Since Battery Change Average
+        0x6972: Since Battery Change Time (in seconds)
+        
+        0x6973: pulses in last second
+        0x6974: Speed
+        0x6975: Control
+    */
+
     const static uint16_t BIKE_SERVICE_UUID              = 0x6969;
-    const static uint16_t BIKE_DISTANCE_CHARACTERISTIC_UUID = 0x696A;
-    const static uint16_t BIKE_AVERAGE_CHARACTERISTIC_UUID = 0x696B;
-    const static uint16_t BIKE_PULSE_CHARACTERISTIC_UUID = 0x696C;
-    const static uint16_t BIKE_START_CHARACTERISTIC_UUID = 0x696D;
+    
+    const static uint16_t TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x696A;
+    const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x696B;
+    const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x696C;
     
-    uint8_t *start;
+    const static uint16_t OVERALL_DISTANCE_CHARACTERISTIC_UUID = 0x696D;
+    const static uint16_t OVERALL_AVERAGE_CHARACTERISTIC_UUID = 0x696E;
+    const static uint16_t OVERALL_TIME_CHARACTERISTIC_UUID = 0x696F;
+    
+    const static uint16_t BATCHANGE_DISTANCE_CHARACTERISTIC_UUID = 0x6970;
+    const static uint16_t BATCHANGE_AVERAGE_CHARACTERISTIC_UUID = 0x6971;
+    const static uint16_t BATCHANGE_TIME_CHARACTERISTIC_UUID = 0x6972;
+    
+    const static uint16_t PULSE_CHARACTERISTIC_UUID = 0x6973;
+    const static uint16_t SPEED_CHARACTERISTIC_UUID = 0x6974;
+    const static uint16_t CONTROL_CHARACTERISTIC_UUID = 0x6975;
     
     //CONSTRUCTOR
-    BikeService(BLE &_ble,
-                uint8_t *startBool,
-                uint32_t initialDistance = 0,
-                uint32_t initialAverage = 0) : 
-        ble(_ble), 
-        bikeDistance(initialDistance),
-        bikeAverage(initialAverage),
-        bikeDistanceChar(BIKE_DISTANCE_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        bikeAverageChar(BIKE_AVERAGE_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        bikePulseChar(BIKE_PULSE_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
-        bikeStartChar(BIKE_START_CHARACTERISTIC_UUID, startBool, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+    BikeService(BLE &_ble, BikeData *bikeData) :
+                ble(_ble), 
+                bd(bikeData),
+                tripDistanceChar(TRIP_DISTANCE_CHARACTERISTIC_UUID,&tripDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                tripAverageChar(TRIP_AVERAGE_CHARACTERISTIC_UUID,&tripAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                tripTimeChar(TRIP_TIME_CHARACTERISTIC_UUID,&tripTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                
+                overallDistanceChar(OVERALL_DISTANCE_CHARACTERISTIC_UUID,&overallDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                overallAverageChar(OVERALL_AVERAGE_CHARACTERISTIC_UUID,&overallAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                overallTimeChar(OVERALL_TIME_CHARACTERISTIC_UUID,&overallTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                
+                batChangeDistanceChar(BATCHANGE_DISTANCE_CHARACTERISTIC_UUID,&batChangeDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                batChangeAverageChar(BATCHANGE_AVERAGE_CHARACTERISTIC_UUID,&batChangeAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                batChangeTimeChar(BATCHANGE_TIME_CHARACTERISTIC_UUID,&batChangeTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        
+                pulseChar(PULSE_CHARACTERISTIC_UUID,&pulse, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                speedChar(SPEED_CHARACTERISTIC_UUID,&speed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+                controlChar(CONTROL_CHARACTERISTIC_UUID,&control, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
     {
-        GattCharacteristic *charTable[] = {&bikeDistanceChar, &bikeAverageChar, &bikePulseChar, &bikeStartChar};
+        tripDistance = (bd->getDataSet(BikeData::TRIP))->getDistance() *100;
+        tripAverage = (bd->getDataSet(BikeData::TRIP))->getAverage() *100;
+        tripTime = (bd->getDataSet(BikeData::TRIP))->getTime();
+        
+        overallDistance = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100;
+        overallAverage = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100;
+        overallTime = (bd->getDataSet(BikeData::OVERALL))->getTime();
+        
+        batChangeDistance = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100;
+        batChangeAverage = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100;
+        batChangeTime = (bd->getDataSet(BikeData::BATCHANGE))->getTime();
+        
+        pulse = bd->getLastCount();
+        speed = bd->getSpeed()*100;
+        control = bd->isLogging()*5;
+        
+        GattCharacteristic *charTable[] = {&tripDistanceChar, &tripAverageChar, &tripTimeChar,
+                                           &overallDistanceChar, &overallAverageChar, &overallTimeChar,
+                                           &batChangeDistanceChar, &batChangeAverageChar, &batChangeTimeChar,
+                                           &pulseChar, &speedChar, &controlChar};
         GattService         bikeService(BikeService::BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
         ble.gattServer().addService(bikeService);
         ble.onDataWritten(this, &BikeService::onDataWrittenCallback);
-        start = startBool;
     }
     
     void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) {
-        if (writeParams->handle == bikeStartChar.getValueHandle()) {
-                *start = *(writeParams->data);
+        if (writeParams->handle == controlChar.getValueHandle()) {
+                bd->startTrip();
             }
     }
     
  
-    void updateBikeDistance(float newDistance) {
-        bikeDistance = newDistance*100;
-        ble.gattServer().write(bikeDistanceChar.getValueHandle(), (uint8_t *)&bikeDistance, sizeof(uint32_t));
-    }
-    
-    void updateBikeAverage(float newAverage) {
-        bikeAverage = newAverage*100;
-        ble.gattServer().write(bikeAverageChar.getValueHandle(), (uint8_t *)&bikeAverage, sizeof(uint32_t));
-    }
+    void update(){
+        /*
+        *
+        *TODO: update optimaliseren zodat niet alles moet geupdate worden om de seconde (enkel de waardes die veranderen)
+        *
+        */
+        
+        tripDistance = ((bd->getDataSet(BikeData::TRIP))->getDistance()) * 100;
+        tripAverage = ((bd->getDataSet(BikeData::TRIP))->getAverage()) * 100;
+        tripTime = ((bd->getDataSet(BikeData::TRIP))->getTime());
+        
+        overallDistance = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100;
+        overallAverage = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100;
+        overallTime = (bd->getDataSet(BikeData::OVERALL))->getTime();
+        
+        batChangeDistance = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100;
+        batChangeAverage = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100;
+        batChangeTime = (bd->getDataSet(BikeData::BATCHANGE))->getTime();
+        
+        pulse = bd->getLastCount();
+        speed = bd->getSpeed()*100;
+        control = bd->isLogging()*5;
+        
+        ble.gattServer().write(tripDistanceChar.getValueHandle(),(uint8_t *)&tripDistance,sizeof(uint32_t));
+        ble.gattServer().write(tripAverageChar.getValueHandle(),(uint8_t *)&tripAverage,sizeof(uint32_t));
+        ble.gattServer().write(tripTimeChar.getValueHandle(),(uint8_t *)&tripTime,sizeof(uint32_t));
+        
+        ble.gattServer().write(overallDistanceChar.getValueHandle(),(uint8_t *)&overallDistance,sizeof(uint32_t));
+        ble.gattServer().write(overallAverageChar.getValueHandle(),(uint8_t *)&overallAverage,sizeof(uint32_t));
+        ble.gattServer().write(overallTimeChar.getValueHandle(),(uint8_t *)&overallTime,sizeof(uint32_t));
+        
+        ble.gattServer().write(batChangeDistanceChar.getValueHandle(),(uint8_t *)&batChangeDistance,sizeof(uint32_t));
+        ble.gattServer().write(batChangeAverageChar.getValueHandle(),(uint8_t *)&batChangeAverage,sizeof(uint32_t));
+        ble.gattServer().write(batChangeTimeChar.getValueHandle(),(uint8_t *)&batChangeTime,sizeof(uint32_t));
+        
+        ble.gattServer().write(pulseChar.getValueHandle(),&pulse,sizeof(uint8_t));
+        ble.gattServer().write(speedChar.getValueHandle(),(uint8_t *)&speed,sizeof(uint32_t));
+        ble.gattServer().write(controlChar.getValueHandle(),&control,sizeof(uint8_t));
+        }
+ 
+public:
+    BLE                              &ble;
+    BikeData                         *bd;
+    uint32_t tripDistance, tripAverage, tripTime,
+             overallDistance, overallAverage, overallTime,
+             batChangeDistance, batChangeAverage, batChangeTime,
+             speed;
+    uint8_t pulse, control;
+             
+    ReadOnlyGattCharacteristic<uint32_t>  tripDistanceChar;
+    ReadOnlyGattCharacteristic<uint32_t>  tripAverageChar;
+    ReadOnlyGattCharacteristic<uint32_t>  tripTimeChar;
     
-    void updateBikePulse(uint32_t newPulse) {
-        bikePulse = newPulse;
-        ble.gattServer().write(bikePulseChar.getValueHandle(), (uint8_t *)&bikePulse, sizeof(uint32_t));
-    }
- 
-private:
-    BLE                              &ble;
-    uint32_t                         bikeDistance;
-    uint32_t                         bikeAverage;
-    uint32_t                         bikePulse;
-    ReadOnlyGattCharacteristic<uint32_t>  bikePulseChar;
-    ReadOnlyGattCharacteristic<uint32_t>  bikeDistanceChar;
-    ReadOnlyGattCharacteristic<uint32_t>  bikeAverageChar;
-    ReadWriteGattCharacteristic<uint8_t>  bikeStartChar;
+    ReadOnlyGattCharacteristic<uint32_t>  overallDistanceChar;
+    ReadOnlyGattCharacteristic<uint32_t>  overallAverageChar;
+    ReadOnlyGattCharacteristic<uint32_t>  overallTimeChar;
+    
+    ReadOnlyGattCharacteristic<uint32_t>  batChangeDistanceChar;
+    ReadOnlyGattCharacteristic<uint32_t>  batChangeAverageChar;
+    ReadOnlyGattCharacteristic<uint32_t>  batChangeTimeChar;
+    
+    ReadOnlyGattCharacteristic<uint8_t>  pulseChar;
+    ReadOnlyGattCharacteristic<uint32_t>  speedChar;
+    ReadWriteGattCharacteristic<uint8_t>  controlChar;
 };
  
 #endif
\ No newline at end of file