BLE Library with custom services for the tortuga bike

Dependents:   TORTUGA_BLE

Fork of BLE_API by aapje monkey

ble/services/BikeService.h

Committer:
ptuytsch
Date:
2016-04-16
Revision:
1131:5cc818415e2b
Child:
1202:a31d51d9fba8

File content as of revision 1131:5cc818415e2b:

#ifndef __BLE_BIKE_SERVICE_H__
#define __BLE_BIKE_SERVICE_H__
 
class BikeService {
public:
    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;
 
    BikeService(BLE &_ble,
                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)
    {
        GattCharacteristic *charTable[] = {&bikeDistanceChar, &bikeAverageChar, &bikePulseChar};
        GattService         bikeService(BikeService::BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
        ble.gattServer().addService(bikeService);
    }
    
 
    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 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;
};
 
#endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */