BLE Library with custom services for the tortuga bike

Dependents:   TORTUGA_BLE

Fork of BLE_API by aapje monkey

Committer:
ptuytsch
Date:
Tue Jul 05 12:17:18 2016 +0000
Revision:
1204:c87f50212c84
Parent:
1202:a31d51d9fba8
Child:
1205:86f0783a5420
working with 1 battery read (in Device information)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptuytsch 1131:5cc818415e2b 1 #ifndef __BLE_BIKE_SERVICE_H__
ptuytsch 1131:5cc818415e2b 2 #define __BLE_BIKE_SERVICE_H__
ptuytsch 1204:c87f50212c84 3
ptuytsch 1204:c87f50212c84 4 #include "BikeData.h"
ptuytsch 1131:5cc818415e2b 5 class BikeService {
ptuytsch 1131:5cc818415e2b 6 public:
ptuytsch 1204:c87f50212c84 7 /*
ptuytsch 1204:c87f50212c84 8 UUID LIST:
ptuytsch 1204:c87f50212c84 9 0x6969: SERVICE
ptuytsch 1204:c87f50212c84 10 0x696A: Trip Distance
ptuytsch 1204:c87f50212c84 11 0x696B: Trip Average
ptuytsch 1204:c87f50212c84 12 0x696C: Trip Time (in seconds)
ptuytsch 1204:c87f50212c84 13
ptuytsch 1204:c87f50212c84 14 0x696D: Overall Distance
ptuytsch 1204:c87f50212c84 15 0x696E: Overall Average
ptuytsch 1204:c87f50212c84 16 0x696F: Overall Time (in seconds)
ptuytsch 1204:c87f50212c84 17
ptuytsch 1204:c87f50212c84 18 0x6970: Since Battery Change Distance
ptuytsch 1204:c87f50212c84 19 0x6971: Since Battery Change Average
ptuytsch 1204:c87f50212c84 20 0x6972: Since Battery Change Time (in seconds)
ptuytsch 1204:c87f50212c84 21
ptuytsch 1204:c87f50212c84 22 0x6973: pulses in last second
ptuytsch 1204:c87f50212c84 23 0x6974: Speed
ptuytsch 1204:c87f50212c84 24 0x6975: Control
ptuytsch 1204:c87f50212c84 25 */
ptuytsch 1204:c87f50212c84 26
ptuytsch 1204:c87f50212c84 27
ptuytsch 1204:c87f50212c84 28 /*defining the UUID's for the service and characters*/
ptuytsch 1204:c87f50212c84 29 const static uint16_t BIKE_SERVICE_UUID = 0x3969;
ptuytsch 1202:a31d51d9fba8 30
ptuytsch 1204:c87f50212c84 31 const static uint16_t TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x396A;
ptuytsch 1204:c87f50212c84 32 const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x396B;
ptuytsch 1204:c87f50212c84 33 const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x396C;
ptuytsch 1204:c87f50212c84 34
ptuytsch 1204:c87f50212c84 35 const static uint16_t OVERALL_DISTANCE_CHARACTERISTIC_UUID = 0x396D;
ptuytsch 1204:c87f50212c84 36 const static uint16_t OVERALL_AVERAGE_CHARACTERISTIC_UUID = 0x396E;
ptuytsch 1204:c87f50212c84 37 const static uint16_t OVERALL_TIME_CHARACTERISTIC_UUID = 0x396F;
ptuytsch 1204:c87f50212c84 38
ptuytsch 1204:c87f50212c84 39 const static uint16_t BATCHANGE_DISTANCE_CHARACTERISTIC_UUID = 0x3970;
ptuytsch 1204:c87f50212c84 40 const static uint16_t BATCHANGE_AVERAGE_CHARACTERISTIC_UUID = 0x3971;
ptuytsch 1204:c87f50212c84 41 const static uint16_t BATCHANGE_TIME_CHARACTERISTIC_UUID = 0x3972;
ptuytsch 1204:c87f50212c84 42
ptuytsch 1204:c87f50212c84 43 const static uint16_t PULSE_CHARACTERISTIC_UUID = 0x3973;
ptuytsch 1204:c87f50212c84 44 const static uint16_t SPEED_CHARACTERISTIC_UUID = 0x3974;
ptuytsch 1204:c87f50212c84 45 const static uint16_t CONTROL_CHARACTERISTIC_UUID = 0x3975;
ptuytsch 1202:a31d51d9fba8 46
ptuytsch 1202:a31d51d9fba8 47 //CONSTRUCTOR
ptuytsch 1204:c87f50212c84 48 BikeService(BLE &_ble, BikeData *bikeData) :
ptuytsch 1204:c87f50212c84 49 /*define standard/initial value*/
ptuytsch 1204:c87f50212c84 50 ble(_ble),
ptuytsch 1204:c87f50212c84 51 bd(bikeData),
ptuytsch 1204:c87f50212c84 52 tripDistanceChar(TRIP_DISTANCE_CHARACTERISTIC_UUID,&tripDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 53 tripAverageChar(TRIP_AVERAGE_CHARACTERISTIC_UUID,&tripAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 54 tripTimeChar(TRIP_TIME_CHARACTERISTIC_UUID,&tripTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 55
ptuytsch 1204:c87f50212c84 56 overallDistanceChar(OVERALL_DISTANCE_CHARACTERISTIC_UUID,&overallDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 57 overallAverageChar(OVERALL_AVERAGE_CHARACTERISTIC_UUID,&overallAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 58 overallTimeChar(OVERALL_TIME_CHARACTERISTIC_UUID,&overallTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 59
ptuytsch 1204:c87f50212c84 60 batChangeDistanceChar(BATCHANGE_DISTANCE_CHARACTERISTIC_UUID,&batChangeDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 61 batChangeAverageChar(BATCHANGE_AVERAGE_CHARACTERISTIC_UUID,&batChangeAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 62 batChangeTimeChar(BATCHANGE_TIME_CHARACTERISTIC_UUID,&batChangeTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 63
ptuytsch 1204:c87f50212c84 64 pulseChar(PULSE_CHARACTERISTIC_UUID,&pulse, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 65 speedChar(SPEED_CHARACTERISTIC_UUID,&speed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
ptuytsch 1204:c87f50212c84 66 controlChar(CONTROL_CHARACTERISTIC_UUID,&control, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
ptuytsch 1131:5cc818415e2b 67 {
ptuytsch 1204:c87f50212c84 68 //read al the values out of the BikeData object
ptuytsch 1204:c87f50212c84 69 tripDistance = (bd->getDataSet(BikeData::TRIP))->getDistance() *100;
ptuytsch 1204:c87f50212c84 70 tripAverage = (bd->getDataSet(BikeData::TRIP))->getAverage() *100;
ptuytsch 1204:c87f50212c84 71 tripTime = (bd->getDataSet(BikeData::TRIP))->getTime();
ptuytsch 1204:c87f50212c84 72
ptuytsch 1204:c87f50212c84 73 overallDistance = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100;
ptuytsch 1204:c87f50212c84 74 overallAverage = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100;
ptuytsch 1204:c87f50212c84 75 overallTime = (bd->getDataSet(BikeData::OVERALL))->getTime();
ptuytsch 1204:c87f50212c84 76
ptuytsch 1204:c87f50212c84 77 batChangeDistance = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100;
ptuytsch 1204:c87f50212c84 78 batChangeAverage = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100;
ptuytsch 1204:c87f50212c84 79 batChangeTime = (bd->getDataSet(BikeData::BATCHANGE))->getTime();
ptuytsch 1204:c87f50212c84 80
ptuytsch 1204:c87f50212c84 81 pulse = bd->getLastCount();
ptuytsch 1204:c87f50212c84 82 speed = bd->getSpeed()*100;
ptuytsch 1204:c87f50212c84 83 control = bd->isLogging()*5;
ptuytsch 1204:c87f50212c84 84
ptuytsch 1204:c87f50212c84 85 //creating a pointer list of al the characteristics
ptuytsch 1204:c87f50212c84 86 GattCharacteristic *charTable[] = {&tripDistanceChar, &tripAverageChar, &tripTimeChar,
ptuytsch 1204:c87f50212c84 87 &overallDistanceChar, &overallAverageChar, &overallTimeChar,
ptuytsch 1204:c87f50212c84 88 &batChangeDistanceChar, &batChangeAverageChar, &batChangeTimeChar,
ptuytsch 1204:c87f50212c84 89 &pulseChar, &speedChar, &controlChar};
ptuytsch 1204:c87f50212c84 90 //creating the gattService
ptuytsch 1131:5cc818415e2b 91 GattService bikeService(BikeService::BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
ptuytsch 1131:5cc818415e2b 92 ble.gattServer().addService(bikeService);
ptuytsch 1204:c87f50212c84 93 //define the function to call when data is written
ptuytsch 1202:a31d51d9fba8 94 ble.onDataWritten(this, &BikeService::onDataWrittenCallback);
ptuytsch 1202:a31d51d9fba8 95 }
ptuytsch 1202:a31d51d9fba8 96
ptuytsch 1202:a31d51d9fba8 97 void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) {
ptuytsch 1204:c87f50212c84 98 //when data is written in the controlCharacteristic
ptuytsch 1204:c87f50212c84 99 printf("data");
ptuytsch 1204:c87f50212c84 100 if (writeParams->handle == controlChar.getValueHandle()) {
ptuytsch 1204:c87f50212c84 101 //performing different actions according to the written value
ptuytsch 1204:c87f50212c84 102 if (*(writeParams->data) == '1'){
ptuytsch 1204:c87f50212c84 103 bd->startTrip();
ptuytsch 1204:c87f50212c84 104 }
ptuytsch 1204:c87f50212c84 105 else if (*(writeParams->data) == '2'){
ptuytsch 1204:c87f50212c84 106 bd->pauzeTrip();
ptuytsch 1204:c87f50212c84 107 }
ptuytsch 1204:c87f50212c84 108 else if (*(writeParams->data) == '3'){
ptuytsch 1204:c87f50212c84 109 bd->stopTrip();
ptuytsch 1204:c87f50212c84 110 }
ptuytsch 1202:a31d51d9fba8 111 }
ptuytsch 1131:5cc818415e2b 112 }
ptuytsch 1131:5cc818415e2b 113
ptuytsch 1131:5cc818415e2b 114
ptuytsch 1204:c87f50212c84 115 void update(){
ptuytsch 1204:c87f50212c84 116 /*updating the different characteristics when the value has changed*/
ptuytsch 1204:c87f50212c84 117
ptuytsch 1204:c87f50212c84 118 uint32_t tmp;
ptuytsch 1204:c87f50212c84 119 //TripData
ptuytsch 1204:c87f50212c84 120 tmp = ((bd->getDataSet(BikeData::TRIP))->getDistance()) * 100;
ptuytsch 1204:c87f50212c84 121 if (tmp != tripDistance){
ptuytsch 1204:c87f50212c84 122 tripDistance = tmp;
ptuytsch 1204:c87f50212c84 123 ble.gattServer().write(tripDistanceChar.getValueHandle(),(uint8_t *)&tripDistance,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 124 }
ptuytsch 1204:c87f50212c84 125 tmp = ((bd->getDataSet(BikeData::TRIP))->getAverage()) * 100;
ptuytsch 1204:c87f50212c84 126 if (tmp != tripAverage){
ptuytsch 1204:c87f50212c84 127 tripAverage = tmp;
ptuytsch 1204:c87f50212c84 128 ble.gattServer().write(tripAverageChar.getValueHandle(),(uint8_t *)&tripAverage,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 129 }
ptuytsch 1204:c87f50212c84 130
ptuytsch 1204:c87f50212c84 131 tmp = ((bd->getDataSet(BikeData::TRIP))->getTime());
ptuytsch 1204:c87f50212c84 132 if (tmp != tripTime){
ptuytsch 1204:c87f50212c84 133 tripTime = tmp;
ptuytsch 1204:c87f50212c84 134 ble.gattServer().write(tripTimeChar.getValueHandle(),(uint8_t *)&tripTime,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 135 }
ptuytsch 1204:c87f50212c84 136 //OverallData
ptuytsch 1204:c87f50212c84 137 tmp = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100;
ptuytsch 1204:c87f50212c84 138 if (tmp != overallDistance){
ptuytsch 1204:c87f50212c84 139 overallDistance = tmp;
ptuytsch 1204:c87f50212c84 140 ble.gattServer().write(overallDistanceChar.getValueHandle(),(uint8_t *)&overallDistance,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 141 }
ptuytsch 1204:c87f50212c84 142 tmp = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100;
ptuytsch 1204:c87f50212c84 143 if (tmp != overallAverage){
ptuytsch 1204:c87f50212c84 144 overallAverage = tmp;
ptuytsch 1204:c87f50212c84 145 ble.gattServer().write(overallAverageChar.getValueHandle(),(uint8_t *)&overallAverage,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 146 }
ptuytsch 1204:c87f50212c84 147 tmp = (bd->getDataSet(BikeData::OVERALL))->getTime();
ptuytsch 1204:c87f50212c84 148 if (tmp != overallTime){
ptuytsch 1204:c87f50212c84 149 overallTime = tmp;
ptuytsch 1204:c87f50212c84 150 ble.gattServer().write(overallTimeChar.getValueHandle(),(uint8_t *)&overallTime,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 151 }
ptuytsch 1204:c87f50212c84 152 //Since Battery Change
ptuytsch 1204:c87f50212c84 153 tmp = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100;
ptuytsch 1204:c87f50212c84 154 if (tmp != batChangeDistance){
ptuytsch 1204:c87f50212c84 155 batChangeDistance = tmp;
ptuytsch 1204:c87f50212c84 156 ble.gattServer().write(batChangeDistanceChar.getValueHandle(),(uint8_t *)&batChangeDistance,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 157 }
ptuytsch 1204:c87f50212c84 158 tmp = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100;
ptuytsch 1204:c87f50212c84 159 if (tmp != batChangeAverage){
ptuytsch 1204:c87f50212c84 160 batChangeAverage = tmp;
ptuytsch 1204:c87f50212c84 161 ble.gattServer().write(batChangeAverageChar.getValueHandle(),(uint8_t *)&batChangeAverage,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 162 }
ptuytsch 1204:c87f50212c84 163 tmp = (bd->getDataSet(BikeData::BATCHANGE))->getTime();
ptuytsch 1204:c87f50212c84 164 if (tmp != batChangeTime){
ptuytsch 1204:c87f50212c84 165 batChangeTime = tmp;
ptuytsch 1204:c87f50212c84 166 ble.gattServer().write(batChangeTimeChar.getValueHandle(),(uint8_t *)&batChangeTime,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 167 }
ptuytsch 1204:c87f50212c84 168 // Continious Data
ptuytsch 1204:c87f50212c84 169 tmp = bd->getLastCount();
ptuytsch 1204:c87f50212c84 170 if (tmp != pulse){
ptuytsch 1204:c87f50212c84 171 pulse = tmp;
ptuytsch 1204:c87f50212c84 172 ble.gattServer().write(pulseChar.getValueHandle(),&pulse,sizeof(uint8_t));
ptuytsch 1204:c87f50212c84 173 }
ptuytsch 1204:c87f50212c84 174 tmp = bd->getSpeed()*100;
ptuytsch 1204:c87f50212c84 175 if (tmp != speed){
ptuytsch 1204:c87f50212c84 176 speed = tmp;
ptuytsch 1204:c87f50212c84 177 ble.gattServer().write(speedChar.getValueHandle(),(uint8_t *)&speed,sizeof(uint32_t));
ptuytsch 1204:c87f50212c84 178 }
ptuytsch 1204:c87f50212c84 179 tmp = bd->isLogging()*'5';
ptuytsch 1204:c87f50212c84 180 if (tmp != control){
ptuytsch 1204:c87f50212c84 181 control = tmp;
ptuytsch 1204:c87f50212c84 182 ble.gattServer().write(controlChar.getValueHandle(),&control,sizeof(uint8_t));
ptuytsch 1204:c87f50212c84 183 }
ptuytsch 1204:c87f50212c84 184 }
ptuytsch 1204:c87f50212c84 185
ptuytsch 1204:c87f50212c84 186 public:
ptuytsch 1204:c87f50212c84 187
ptuytsch 1204:c87f50212c84 188 //different variables for the service
ptuytsch 1204:c87f50212c84 189 BLE &ble;
ptuytsch 1204:c87f50212c84 190 BikeData *bd;
ptuytsch 1204:c87f50212c84 191 uint32_t tripDistance, tripAverage, tripTime,
ptuytsch 1204:c87f50212c84 192 overallDistance, overallAverage, overallTime,
ptuytsch 1204:c87f50212c84 193 batChangeDistance, batChangeAverage, batChangeTime,
ptuytsch 1204:c87f50212c84 194 speed;
ptuytsch 1204:c87f50212c84 195 uint8_t pulse, control;
ptuytsch 1204:c87f50212c84 196
ptuytsch 1204:c87f50212c84 197 //the different characteristics created of an template
ptuytsch 1204:c87f50212c84 198 ReadOnlyGattCharacteristic<uint32_t> tripDistanceChar;
ptuytsch 1204:c87f50212c84 199 ReadOnlyGattCharacteristic<uint32_t> tripAverageChar;
ptuytsch 1204:c87f50212c84 200 ReadOnlyGattCharacteristic<uint32_t> tripTimeChar;
ptuytsch 1131:5cc818415e2b 201
ptuytsch 1204:c87f50212c84 202 ReadOnlyGattCharacteristic<uint32_t> overallDistanceChar;
ptuytsch 1204:c87f50212c84 203 ReadOnlyGattCharacteristic<uint32_t> overallAverageChar;
ptuytsch 1204:c87f50212c84 204 ReadOnlyGattCharacteristic<uint32_t> overallTimeChar;
ptuytsch 1204:c87f50212c84 205
ptuytsch 1204:c87f50212c84 206 ReadOnlyGattCharacteristic<uint32_t> batChangeDistanceChar;
ptuytsch 1204:c87f50212c84 207 ReadOnlyGattCharacteristic<uint32_t> batChangeAverageChar;
ptuytsch 1204:c87f50212c84 208 ReadOnlyGattCharacteristic<uint32_t> batChangeTimeChar;
ptuytsch 1204:c87f50212c84 209
ptuytsch 1204:c87f50212c84 210 ReadOnlyGattCharacteristic<uint8_t> pulseChar;
ptuytsch 1204:c87f50212c84 211 ReadOnlyGattCharacteristic<uint32_t> speedChar;
ptuytsch 1204:c87f50212c84 212 ReadWriteGattCharacteristic<uint8_t> controlChar;
ptuytsch 1131:5cc818415e2b 213 };
ptuytsch 1131:5cc818415e2b 214
ptuytsch 1202:a31d51d9fba8 215 #endif