BLE Library with custom services for the tortuga bike
Fork of BLE_API by
ble/services/BikeService.h@1203:3bc52a68543f, 2016-04-30 (annotated)
- Committer:
- ptuytsch
- Date:
- Sat Apr 30 23:10:50 2016 +0000
- Revision:
- 1203:3bc52a68543f
- Parent:
- 1202:a31d51d9fba8
Working, all send by bluetooth and adding Device information.; TODO: optimize update of BikeService
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 1131:5cc818415e2b | 1 | #ifndef __BLE_BIKE_SERVICE_H__ |
ptuytsch | 1131:5cc818415e2b | 2 | #define __BLE_BIKE_SERVICE_H__ |
ptuytsch | 1203:3bc52a68543f | 3 | |
ptuytsch | 1203:3bc52a68543f | 4 | #include "BikeData.h" |
ptuytsch | 1131:5cc818415e2b | 5 | class BikeService { |
ptuytsch | 1131:5cc818415e2b | 6 | public: |
ptuytsch | 1203:3bc52a68543f | 7 | /* |
ptuytsch | 1203:3bc52a68543f | 8 | UUID LIST: |
ptuytsch | 1203:3bc52a68543f | 9 | 0x6969: SERVICE |
ptuytsch | 1203:3bc52a68543f | 10 | 0x696A: Trip Distance |
ptuytsch | 1203:3bc52a68543f | 11 | 0x696B: Trip Average |
ptuytsch | 1203:3bc52a68543f | 12 | 0x696C: Trip Time (in seconds) |
ptuytsch | 1203:3bc52a68543f | 13 | |
ptuytsch | 1203:3bc52a68543f | 14 | 0x696D: Overall Distance |
ptuytsch | 1203:3bc52a68543f | 15 | 0x696E: Overall Average |
ptuytsch | 1203:3bc52a68543f | 16 | 0x696F: Overall Time (in seconds) |
ptuytsch | 1203:3bc52a68543f | 17 | |
ptuytsch | 1203:3bc52a68543f | 18 | 0x6970: Since Battery Change Distance |
ptuytsch | 1203:3bc52a68543f | 19 | 0x6971: Since Battery Change Average |
ptuytsch | 1203:3bc52a68543f | 20 | 0x6972: Since Battery Change Time (in seconds) |
ptuytsch | 1203:3bc52a68543f | 21 | |
ptuytsch | 1203:3bc52a68543f | 22 | 0x6973: pulses in last second |
ptuytsch | 1203:3bc52a68543f | 23 | 0x6974: Speed |
ptuytsch | 1203:3bc52a68543f | 24 | 0x6975: Control |
ptuytsch | 1203:3bc52a68543f | 25 | */ |
ptuytsch | 1203:3bc52a68543f | 26 | |
ptuytsch | 1131:5cc818415e2b | 27 | const static uint16_t BIKE_SERVICE_UUID = 0x6969; |
ptuytsch | 1203:3bc52a68543f | 28 | |
ptuytsch | 1203:3bc52a68543f | 29 | const static uint16_t TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x696A; |
ptuytsch | 1203:3bc52a68543f | 30 | const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x696B; |
ptuytsch | 1203:3bc52a68543f | 31 | const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x696C; |
ptuytsch | 1202:a31d51d9fba8 | 32 | |
ptuytsch | 1203:3bc52a68543f | 33 | const static uint16_t OVERALL_DISTANCE_CHARACTERISTIC_UUID = 0x696D; |
ptuytsch | 1203:3bc52a68543f | 34 | const static uint16_t OVERALL_AVERAGE_CHARACTERISTIC_UUID = 0x696E; |
ptuytsch | 1203:3bc52a68543f | 35 | const static uint16_t OVERALL_TIME_CHARACTERISTIC_UUID = 0x696F; |
ptuytsch | 1203:3bc52a68543f | 36 | |
ptuytsch | 1203:3bc52a68543f | 37 | const static uint16_t BATCHANGE_DISTANCE_CHARACTERISTIC_UUID = 0x6970; |
ptuytsch | 1203:3bc52a68543f | 38 | const static uint16_t BATCHANGE_AVERAGE_CHARACTERISTIC_UUID = 0x6971; |
ptuytsch | 1203:3bc52a68543f | 39 | const static uint16_t BATCHANGE_TIME_CHARACTERISTIC_UUID = 0x6972; |
ptuytsch | 1203:3bc52a68543f | 40 | |
ptuytsch | 1203:3bc52a68543f | 41 | const static uint16_t PULSE_CHARACTERISTIC_UUID = 0x6973; |
ptuytsch | 1203:3bc52a68543f | 42 | const static uint16_t SPEED_CHARACTERISTIC_UUID = 0x6974; |
ptuytsch | 1203:3bc52a68543f | 43 | const static uint16_t CONTROL_CHARACTERISTIC_UUID = 0x6975; |
ptuytsch | 1202:a31d51d9fba8 | 44 | |
ptuytsch | 1202:a31d51d9fba8 | 45 | //CONSTRUCTOR |
ptuytsch | 1203:3bc52a68543f | 46 | BikeService(BLE &_ble, BikeData *bikeData) : |
ptuytsch | 1203:3bc52a68543f | 47 | ble(_ble), |
ptuytsch | 1203:3bc52a68543f | 48 | bd(bikeData), |
ptuytsch | 1203:3bc52a68543f | 49 | tripDistanceChar(TRIP_DISTANCE_CHARACTERISTIC_UUID,&tripDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 50 | tripAverageChar(TRIP_AVERAGE_CHARACTERISTIC_UUID,&tripAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 51 | tripTimeChar(TRIP_TIME_CHARACTERISTIC_UUID,&tripTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 52 | |
ptuytsch | 1203:3bc52a68543f | 53 | overallDistanceChar(OVERALL_DISTANCE_CHARACTERISTIC_UUID,&overallDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 54 | overallAverageChar(OVERALL_AVERAGE_CHARACTERISTIC_UUID,&overallAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 55 | overallTimeChar(OVERALL_TIME_CHARACTERISTIC_UUID,&overallTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 56 | |
ptuytsch | 1203:3bc52a68543f | 57 | batChangeDistanceChar(BATCHANGE_DISTANCE_CHARACTERISTIC_UUID,&batChangeDistance, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 58 | batChangeAverageChar(BATCHANGE_AVERAGE_CHARACTERISTIC_UUID,&batChangeAverage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 59 | batChangeTimeChar(BATCHANGE_TIME_CHARACTERISTIC_UUID,&batChangeTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 60 | |
ptuytsch | 1203:3bc52a68543f | 61 | pulseChar(PULSE_CHARACTERISTIC_UUID,&pulse, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 62 | speedChar(SPEED_CHARACTERISTIC_UUID,&speed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1203:3bc52a68543f | 63 | controlChar(CONTROL_CHARACTERISTIC_UUID,&control, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
ptuytsch | 1131:5cc818415e2b | 64 | { |
ptuytsch | 1203:3bc52a68543f | 65 | tripDistance = (bd->getDataSet(BikeData::TRIP))->getDistance() *100; |
ptuytsch | 1203:3bc52a68543f | 66 | tripAverage = (bd->getDataSet(BikeData::TRIP))->getAverage() *100; |
ptuytsch | 1203:3bc52a68543f | 67 | tripTime = (bd->getDataSet(BikeData::TRIP))->getTime(); |
ptuytsch | 1203:3bc52a68543f | 68 | |
ptuytsch | 1203:3bc52a68543f | 69 | overallDistance = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100; |
ptuytsch | 1203:3bc52a68543f | 70 | overallAverage = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100; |
ptuytsch | 1203:3bc52a68543f | 71 | overallTime = (bd->getDataSet(BikeData::OVERALL))->getTime(); |
ptuytsch | 1203:3bc52a68543f | 72 | |
ptuytsch | 1203:3bc52a68543f | 73 | batChangeDistance = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100; |
ptuytsch | 1203:3bc52a68543f | 74 | batChangeAverage = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100; |
ptuytsch | 1203:3bc52a68543f | 75 | batChangeTime = (bd->getDataSet(BikeData::BATCHANGE))->getTime(); |
ptuytsch | 1203:3bc52a68543f | 76 | |
ptuytsch | 1203:3bc52a68543f | 77 | pulse = bd->getLastCount(); |
ptuytsch | 1203:3bc52a68543f | 78 | speed = bd->getSpeed()*100; |
ptuytsch | 1203:3bc52a68543f | 79 | control = bd->isLogging()*5; |
ptuytsch | 1203:3bc52a68543f | 80 | |
ptuytsch | 1203:3bc52a68543f | 81 | GattCharacteristic *charTable[] = {&tripDistanceChar, &tripAverageChar, &tripTimeChar, |
ptuytsch | 1203:3bc52a68543f | 82 | &overallDistanceChar, &overallAverageChar, &overallTimeChar, |
ptuytsch | 1203:3bc52a68543f | 83 | &batChangeDistanceChar, &batChangeAverageChar, &batChangeTimeChar, |
ptuytsch | 1203:3bc52a68543f | 84 | &pulseChar, &speedChar, &controlChar}; |
ptuytsch | 1131:5cc818415e2b | 85 | GattService bikeService(BikeService::BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
ptuytsch | 1131:5cc818415e2b | 86 | ble.gattServer().addService(bikeService); |
ptuytsch | 1202:a31d51d9fba8 | 87 | ble.onDataWritten(this, &BikeService::onDataWrittenCallback); |
ptuytsch | 1202:a31d51d9fba8 | 88 | } |
ptuytsch | 1202:a31d51d9fba8 | 89 | |
ptuytsch | 1202:a31d51d9fba8 | 90 | void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) { |
ptuytsch | 1203:3bc52a68543f | 91 | if (writeParams->handle == controlChar.getValueHandle()) { |
ptuytsch | 1203:3bc52a68543f | 92 | bd->startTrip(); |
ptuytsch | 1202:a31d51d9fba8 | 93 | } |
ptuytsch | 1131:5cc818415e2b | 94 | } |
ptuytsch | 1131:5cc818415e2b | 95 | |
ptuytsch | 1131:5cc818415e2b | 96 | |
ptuytsch | 1203:3bc52a68543f | 97 | void update(){ |
ptuytsch | 1203:3bc52a68543f | 98 | /* |
ptuytsch | 1203:3bc52a68543f | 99 | * |
ptuytsch | 1203:3bc52a68543f | 100 | *TODO: update optimaliseren zodat niet alles moet geupdate worden om de seconde (enkel de waardes die veranderen) |
ptuytsch | 1203:3bc52a68543f | 101 | * |
ptuytsch | 1203:3bc52a68543f | 102 | */ |
ptuytsch | 1203:3bc52a68543f | 103 | |
ptuytsch | 1203:3bc52a68543f | 104 | tripDistance = ((bd->getDataSet(BikeData::TRIP))->getDistance()) * 100; |
ptuytsch | 1203:3bc52a68543f | 105 | tripAverage = ((bd->getDataSet(BikeData::TRIP))->getAverage()) * 100; |
ptuytsch | 1203:3bc52a68543f | 106 | tripTime = ((bd->getDataSet(BikeData::TRIP))->getTime()); |
ptuytsch | 1203:3bc52a68543f | 107 | |
ptuytsch | 1203:3bc52a68543f | 108 | overallDistance = (bd->getDataSet(BikeData::OVERALL))->getDistance()*100; |
ptuytsch | 1203:3bc52a68543f | 109 | overallAverage = (bd->getDataSet(BikeData::OVERALL))->getAverage()*100; |
ptuytsch | 1203:3bc52a68543f | 110 | overallTime = (bd->getDataSet(BikeData::OVERALL))->getTime(); |
ptuytsch | 1203:3bc52a68543f | 111 | |
ptuytsch | 1203:3bc52a68543f | 112 | batChangeDistance = (bd->getDataSet(BikeData::BATCHANGE))->getDistance() *100; |
ptuytsch | 1203:3bc52a68543f | 113 | batChangeAverage = (bd->getDataSet(BikeData::BATCHANGE))->getAverage() *100; |
ptuytsch | 1203:3bc52a68543f | 114 | batChangeTime = (bd->getDataSet(BikeData::BATCHANGE))->getTime(); |
ptuytsch | 1203:3bc52a68543f | 115 | |
ptuytsch | 1203:3bc52a68543f | 116 | pulse = bd->getLastCount(); |
ptuytsch | 1203:3bc52a68543f | 117 | speed = bd->getSpeed()*100; |
ptuytsch | 1203:3bc52a68543f | 118 | control = bd->isLogging()*5; |
ptuytsch | 1203:3bc52a68543f | 119 | |
ptuytsch | 1203:3bc52a68543f | 120 | ble.gattServer().write(tripDistanceChar.getValueHandle(),(uint8_t *)&tripDistance,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 121 | ble.gattServer().write(tripAverageChar.getValueHandle(),(uint8_t *)&tripAverage,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 122 | ble.gattServer().write(tripTimeChar.getValueHandle(),(uint8_t *)&tripTime,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 123 | |
ptuytsch | 1203:3bc52a68543f | 124 | ble.gattServer().write(overallDistanceChar.getValueHandle(),(uint8_t *)&overallDistance,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 125 | ble.gattServer().write(overallAverageChar.getValueHandle(),(uint8_t *)&overallAverage,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 126 | ble.gattServer().write(overallTimeChar.getValueHandle(),(uint8_t *)&overallTime,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 127 | |
ptuytsch | 1203:3bc52a68543f | 128 | ble.gattServer().write(batChangeDistanceChar.getValueHandle(),(uint8_t *)&batChangeDistance,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 129 | ble.gattServer().write(batChangeAverageChar.getValueHandle(),(uint8_t *)&batChangeAverage,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 130 | ble.gattServer().write(batChangeTimeChar.getValueHandle(),(uint8_t *)&batChangeTime,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 131 | |
ptuytsch | 1203:3bc52a68543f | 132 | ble.gattServer().write(pulseChar.getValueHandle(),&pulse,sizeof(uint8_t)); |
ptuytsch | 1203:3bc52a68543f | 133 | ble.gattServer().write(speedChar.getValueHandle(),(uint8_t *)&speed,sizeof(uint32_t)); |
ptuytsch | 1203:3bc52a68543f | 134 | ble.gattServer().write(controlChar.getValueHandle(),&control,sizeof(uint8_t)); |
ptuytsch | 1203:3bc52a68543f | 135 | } |
ptuytsch | 1203:3bc52a68543f | 136 | |
ptuytsch | 1203:3bc52a68543f | 137 | public: |
ptuytsch | 1203:3bc52a68543f | 138 | BLE &ble; |
ptuytsch | 1203:3bc52a68543f | 139 | BikeData *bd; |
ptuytsch | 1203:3bc52a68543f | 140 | uint32_t tripDistance, tripAverage, tripTime, |
ptuytsch | 1203:3bc52a68543f | 141 | overallDistance, overallAverage, overallTime, |
ptuytsch | 1203:3bc52a68543f | 142 | batChangeDistance, batChangeAverage, batChangeTime, |
ptuytsch | 1203:3bc52a68543f | 143 | speed; |
ptuytsch | 1203:3bc52a68543f | 144 | uint8_t pulse, control; |
ptuytsch | 1203:3bc52a68543f | 145 | |
ptuytsch | 1203:3bc52a68543f | 146 | ReadOnlyGattCharacteristic<uint32_t> tripDistanceChar; |
ptuytsch | 1203:3bc52a68543f | 147 | ReadOnlyGattCharacteristic<uint32_t> tripAverageChar; |
ptuytsch | 1203:3bc52a68543f | 148 | ReadOnlyGattCharacteristic<uint32_t> tripTimeChar; |
ptuytsch | 1131:5cc818415e2b | 149 | |
ptuytsch | 1203:3bc52a68543f | 150 | ReadOnlyGattCharacteristic<uint32_t> overallDistanceChar; |
ptuytsch | 1203:3bc52a68543f | 151 | ReadOnlyGattCharacteristic<uint32_t> overallAverageChar; |
ptuytsch | 1203:3bc52a68543f | 152 | ReadOnlyGattCharacteristic<uint32_t> overallTimeChar; |
ptuytsch | 1203:3bc52a68543f | 153 | |
ptuytsch | 1203:3bc52a68543f | 154 | ReadOnlyGattCharacteristic<uint32_t> batChangeDistanceChar; |
ptuytsch | 1203:3bc52a68543f | 155 | ReadOnlyGattCharacteristic<uint32_t> batChangeAverageChar; |
ptuytsch | 1203:3bc52a68543f | 156 | ReadOnlyGattCharacteristic<uint32_t> batChangeTimeChar; |
ptuytsch | 1203:3bc52a68543f | 157 | |
ptuytsch | 1203:3bc52a68543f | 158 | ReadOnlyGattCharacteristic<uint8_t> pulseChar; |
ptuytsch | 1203:3bc52a68543f | 159 | ReadOnlyGattCharacteristic<uint32_t> speedChar; |
ptuytsch | 1203:3bc52a68543f | 160 | ReadWriteGattCharacteristic<uint8_t> controlChar; |
ptuytsch | 1131:5cc818415e2b | 161 | }; |
ptuytsch | 1131:5cc818415e2b | 162 | |
ptuytsch | 1202:a31d51d9fba8 | 163 | #endif |