Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
BikeService.h
00001 #ifndef __BLE_BIKE_SERVICE_H__ 00002 #define __BLE_BIKE_SERVICE_H__ 00003 00004 #include "BikeData.h" 00005 #include "ble/BLE.h" 00006 class BikeService { 00007 public: 00008 /* 00009 UUID LIST: 00010 0x6969: SERVICE 00011 0x696A: Trip Distance 00012 0x696B: Trip Average 00013 0x696C: Trip Time (in seconds) 00014 00015 0x696D: Overall Distance 00016 0x696E: Overall Average 00017 0x696F: Overall Time (in seconds) 00018 00019 0x6970: Since Battery Change Distance 00020 0x6971: Since Battery Change Average 00021 0x6972: Since Battery Change Time (in seconds) 00022 00023 0x6973: pulses in last second 00024 0x6974: Speed 00025 0x6975: Control 00026 0x6976: Device Name 00027 */ 00028 00029 00030 /*defining the UUID's for the service and characters*/ 00031 const static uint16_t BIKE_SERVICE_UUID = 0x6969; 00032 00033 const static uint16_t TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x696A; 00034 const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x696B; 00035 const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x696C; 00036 00037 const static uint16_t OVERALL_DISTANCE_CHARACTERISTIC_UUID = 0x696D; 00038 const static uint16_t OVERALL_AVERAGE_CHARACTERISTIC_UUID = 0x696E; 00039 const static uint16_t OVERALL_TIME_CHARACTERISTIC_UUID = 0x696F; 00040 00041 const static uint16_t BATCHANGE_DISTANCE_CHARACTERISTIC_UUID = 0x6970; 00042 const static uint16_t BATCHANGE_AVERAGE_CHARACTERISTIC_UUID = 0x6971; 00043 const static uint16_t BATCHANGE_TIME_CHARACTERISTIC_UUID = 0x6972; 00044 00045 const static uint16_t PULSE_CHARACTERISTIC_UUID = 0x6973; 00046 const static uint16_t SPEED_CHARACTERISTIC_UUID = 0x6974; 00047 const static uint16_t CONTROL_CHARACTERISTIC_UUID = 0x6975; 00048 const static uint16_t DEVICE_NAME_CHARACTERISTIC_UUID = 0x6976; 00049 00050 //CONSTRUCTOR 00051 BikeService(BLE &_ble, BikeData *bikeData) : 00052 /*define standard/initial value*/ 00053 ble(_ble), 00054 bd(bikeData), 00055 tripDistanceChar(TRIP_DISTANCE_CHARACTERISTIC_UUID,&tripDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00056 tripAverageChar(TRIP_AVERAGE_CHARACTERISTIC_UUID,&tripAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00057 tripTimeChar(TRIP_TIME_CHARACTERISTIC_UUID,&tripTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00058 00059 overallDistanceChar(OVERALL_DISTANCE_CHARACTERISTIC_UUID,&overallDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00060 overallAverageChar(OVERALL_AVERAGE_CHARACTERISTIC_UUID,&overallAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00061 overallTimeChar(OVERALL_TIME_CHARACTERISTIC_UUID,&overallTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00062 00063 batChangeDistanceChar(BATCHANGE_DISTANCE_CHARACTERISTIC_UUID,&batChangeDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00064 batChangeAverageChar(BATCHANGE_AVERAGE_CHARACTERISTIC_UUID,&batChangeAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00065 batChangeTimeChar(BATCHANGE_TIME_CHARACTERISTIC_UUID,&batChangeTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00066 00067 pulseChar(PULSE_CHARACTERISTIC_UUID,&pulse, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00068 speedChar(SPEED_CHARACTERISTIC_UUID,&speed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00069 controlChar(CONTROL_CHARACTERISTIC_UUID,&control, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) 00070 /*deviceNameChar(DEVICE_NAME_CHARACTERISTIC_UUID, 00071 (uint8_t *)bd->getBikeName(), 00072 bd->getBikeNameSize(), 00073 bd->getBikeNameSize(), 00074 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)*/ 00075 { 00076 static char devName[] = "tmp"; 00077 bd->getBikeName(devName); 00078 deviceNameChar = new GattCharacteristic(DEVICE_NAME_CHARACTERISTIC_UUID, 00079 (uint8_t *)devName, 00080 bd->getBikeNameSize(), 00081 bd->getBikeNameSize(), 00082 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00083 /*static char devName[] = "tmp"; 00084 bd->getBikeName(devName); 00085 ble.gattServer().write(deviceNameChar.getValueHandle(),(uint8_t *)devName,bd->getBikeNameSize());*/ 00086 //read al the values out of the BikeData object 00087 tripDistance = (bd->getDataSet(BikeData::TRIP))->getDistance() *100; 00088 tripAverage = (bd->getDataSet(BikeData::TRIP))->getAverage() *100; 00089 tripTime = (bd->getDataSet(BikeData::TRIP))->getTime(); 00090 00091 overallDistance = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100; 00092 overallAverage = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100; 00093 overallTime = (bd->getDataSet(BikeData::OVERALL))->getTime(); 00094 00095 batChangeDistance = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100; 00096 batChangeAverage = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100; 00097 batChangeTime = (bd->getDataSet(BikeData::BATCHANGE))->getTime(); 00098 00099 pulse = bd->getLastCount(); 00100 speed = bd->getSpeed()*100; 00101 control = bd->isLogging()*5; 00102 00103 //creating a pointer list of al the characteristics 00104 GattCharacteristic *charTable[] = {&tripDistanceChar, &tripAverageChar, &tripTimeChar, 00105 &overallDistanceChar, &overallAverageChar, &overallTimeChar, 00106 &batChangeDistanceChar, &batChangeAverageChar, &batChangeTimeChar, 00107 &pulseChar, &speedChar, &controlChar, deviceNameChar}; 00108 //creating the gattService 00109 GattService bikeService(BikeService::BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00110 ble.gattServer().addService(bikeService); 00111 //define the function to call when data is written to service 00112 ble.onDataWritten(this, &BikeService::onDataWrittenCallback); 00113 } 00114 00115 void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) { 00116 //when data is written in the controlCharacteristic 00117 if (writeParams->handle == controlChar.getValueHandle()) { 00118 //performing different actions according to the written value 00119 if (*(writeParams->data) == '1'){ 00120 bd->startTrip(); 00121 } 00122 else if (*(writeParams->data) == '2'){ 00123 bd->pauzeTrip(); 00124 } 00125 else if (*(writeParams->data) == '3'){ 00126 bd->stopTrip(); 00127 } 00128 } 00129 00130 if (writeParams->handle == deviceNameChar->getValueHandle()) { 00131 printf("name changing, length: %i\n",(uint8_t)writeParams->len); 00132 //uint8_t *recievedName = writeParams->data; 00133 //writeParams->data->push_back(0x00); 00134 uint8_t recievedName[writeParams->len +1]; 00135 for (uint8_t i = 0 ; i < writeParams->len ; i++){ 00136 recievedName[i] = *(writeParams->data+i); 00137 } 00138 recievedName[writeParams->len] = 0x00; 00139 bd->setBikeName((char *)recievedName,((uint8_t)writeParams->len)+1); 00140 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)recievedName, (writeParams->len)+1);//passing the device name 00141 00142 printf("written length: %i\n", bd->getBikeNameSize()); 00143 static char devName[] = "tmp"; 00144 bd->getBikeName(devName); 00145 ble.gattServer().write(deviceNameChar->getValueHandle(),(uint8_t *)devName,bd->getBikeNameSize()); 00146 /*char name[] = "otherName"; 00147 bd->setBikeName(name,sizeof(name));*/ 00148 } 00149 } 00150 00151 00152 void update(){ 00153 /*updating the different characteristics when the value has changed*/ 00154 uint32_t tmp; 00155 //TripData 00156 tmp = ((bd->getDataSet(BikeData::TRIP))->getDistance()) * 100; 00157 if (tmp != tripDistance){ 00158 tripDistance = tmp; 00159 ble.gattServer().write(tripDistanceChar.getValueHandle(),(uint8_t *)&tripDistance,sizeof(uint32_t)); 00160 for (uint64_t i = 0; i<0x2760; i++); 00161 } 00162 tmp = ((bd->getDataSet(BikeData::TRIP))->getAverage()) * 100; 00163 if (tmp != tripAverage){ 00164 tripAverage = tmp; 00165 ble.gattServer().write(tripAverageChar.getValueHandle(),(uint8_t *)&tripAverage,sizeof(uint32_t)); 00166 for (uint64_t i = 0; i<0x2760; i++); 00167 } 00168 tmp = ((bd->getDataSet(BikeData::TRIP))->getTime()); 00169 if (tmp != tripTime){ 00170 tripTime = tmp; 00171 ble.gattServer().write(tripTimeChar.getValueHandle(),(uint8_t *)&tripTime,sizeof(uint32_t)); 00172 for (uint64_t i = 0; i<0x2760; i++); 00173 } 00174 //OverallData 00175 tmp = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100; 00176 if (tmp != overallDistance){ 00177 overallDistance = tmp; 00178 ble.gattServer().write(overallDistanceChar.getValueHandle(),(uint8_t *)&overallDistance,sizeof(uint32_t)); 00179 for (uint64_t i = 0; i<0x2760; i++); 00180 } 00181 tmp = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100; 00182 if (tmp != overallAverage){ 00183 overallAverage = tmp; 00184 ble.gattServer().write(overallAverageChar.getValueHandle(),(uint8_t *)&overallAverage,sizeof(uint32_t)); 00185 for (uint64_t i = 0; i<0x2760; i++); 00186 } 00187 tmp = (bd->getDataSet(BikeData::OVERALL))->getTime(); 00188 if (tmp != overallTime){ 00189 overallTime = tmp; 00190 ble.gattServer().write(overallTimeChar.getValueHandle(),(uint8_t *)&overallTime,sizeof(uint32_t)); 00191 for (uint64_t i = 0; i<0x2760; i++); 00192 } 00193 //Since Battery Change 00194 tmp = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100; 00195 if (tmp != batChangeDistance){ 00196 batChangeDistance = tmp; 00197 ble.gattServer().write(batChangeDistanceChar.getValueHandle(),(uint8_t *)&batChangeDistance,sizeof(uint32_t)); 00198 for (uint64_t i = 0; i<0x2760; i++); 00199 } 00200 tmp = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100; 00201 if (tmp != batChangeAverage){ 00202 batChangeAverage = tmp; 00203 ble.gattServer().write(batChangeAverageChar.getValueHandle(),(uint8_t *)&batChangeAverage,sizeof(uint32_t)); 00204 for (uint64_t i = 0; i<0x2760; i++); 00205 } 00206 tmp = (bd->getDataSet(BikeData::BATCHANGE))->getTime(); 00207 if (tmp != batChangeTime){ 00208 batChangeTime = tmp; 00209 ble.gattServer().write(batChangeTimeChar.getValueHandle(),(uint8_t *)&batChangeTime,sizeof(uint32_t)); 00210 for (uint64_t i = 0; i<0x2760; i++); 00211 } 00212 // Continious Data 00213 tmp = bd->getLastCount(); 00214 if (tmp != pulse){ 00215 pulse = tmp; 00216 ble.gattServer().write(pulseChar.getValueHandle(),&pulse,sizeof(uint8_t)); 00217 for (uint64_t i = 0; i<0x2760; i++); 00218 } 00219 tmp = bd->getSpeed()*100; 00220 if (tmp != speed){ 00221 speed = tmp; 00222 ble.gattServer().write(speedChar.getValueHandle(),(uint8_t *)&speed,sizeof(uint32_t)); 00223 for (uint64_t i = 0; i<0x2760; i++); 00224 } 00225 tmp = bd->isLogging()*'5'; 00226 if (tmp != control){ 00227 control = tmp; 00228 ble.gattServer().write(controlChar.getValueHandle(),&control,sizeof(uint8_t)); 00229 } 00230 } 00231 00232 public: 00233 00234 //different variables for the service 00235 BLE &ble; 00236 BikeData *bd; 00237 uint32_t tripDistance, tripAverage, tripTime, 00238 overallDistance, overallAverage, overallTime, 00239 batChangeDistance, batChangeAverage, batChangeTime, 00240 speed; 00241 uint8_t pulse, control; 00242 00243 //the different characteristics created of an template 00244 ReadOnlyGattCharacteristic<uint32_t> tripDistanceChar; 00245 ReadOnlyGattCharacteristic<uint32_t> tripAverageChar; 00246 ReadOnlyGattCharacteristic<uint32_t> tripTimeChar; 00247 00248 ReadOnlyGattCharacteristic<uint32_t> overallDistanceChar; 00249 ReadOnlyGattCharacteristic<uint32_t> overallAverageChar; 00250 ReadOnlyGattCharacteristic<uint32_t> overallTimeChar; 00251 00252 ReadOnlyGattCharacteristic<uint32_t> batChangeDistanceChar; 00253 ReadOnlyGattCharacteristic<uint32_t> batChangeAverageChar; 00254 ReadOnlyGattCharacteristic<uint32_t> batChangeTimeChar; 00255 00256 ReadOnlyGattCharacteristic<uint8_t> pulseChar; 00257 ReadOnlyGattCharacteristic<uint32_t> speedChar; 00258 ReadWriteGattCharacteristic<uint8_t> controlChar; 00259 00260 00261 GattCharacteristic *deviceNameChar; 00262 }; 00263 00264 #endif
Generated on Tue Jul 12 2022 21:14:55 by
1.7.2
