BLE Library with custom services for the tortuga bike
Fork of BLE_API by
ble/services/BikeService.h
- Committer:
- ptuytsch
- Date:
- 2016-04-30
- Revision:
- 1203:3bc52a68543f
- Parent:
- 1202:a31d51d9fba8
File content as of revision 1203:3bc52a68543f:
#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 TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x696A; const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x696B; const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x696C; 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, 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) { 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); } void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) { if (writeParams->handle == controlChar.getValueHandle()) { bd->startTrip(); } } 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; 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