-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerServer.cpp
74 lines (59 loc) · 3.42 KB
/
PowerServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Arduino.h>
#include "config.h"
#include "PowerServer.h"
#include <string.h>
// Define Speed and Power Properties
// https://www.bluetooth.com/xml-viewer/?src=https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Services/org.bluetooth.service.cycling_power.xml
#define powerService BLEUUID((uint16_t)0x1818)
PowerServer::PowerServer() : cpMeasurementCharacteristics(BLEUUID((uint16_t)0x2A63), BLECharacteristic::PROPERTY_NOTIFY),
cpFeatureCharacteristics(BLEUUID((uint16_t)0x2A65), BLECharacteristic::PROPERTY_READ),
sensorLocationCharacteristics(BLEUUID((uint16_t)0x2A5D), BLECharacteristic::PROPERTY_READ)
{
// https://www.bluetooth.com/xml-viewer/?src=https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.cp_measurement.xml
cpMeasurementDescriptor.setValue("Exercise Bike Cycling Power Measurement");
cpMeasurementCharacteristics.addDescriptor(&cpMeasurementDescriptor);
// https://www.bluetooth.com/xml-viewer/?src=https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.cycling_power_feature.xml
cpFeatureDescriptor.setValue("Exercise Bike Cycling Power Feature");
cpFeatureCharacteristics.addDescriptor(&cpFeatureDescriptor);
// https://www.bluetooth.com/xml-viewer/?src=https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.sensor_location.xml
sensorLocationDescriptor.setValue("Exercise Bike Sensor Location");
sensorLocationCharacteristics.addDescriptor(&sensorLocationDescriptor);
}
void PowerServer::init(BLEServer *pServer)
{
// Create Speed and Power Configuration
BLEService *pPower = pServer->createService(powerService); // Create Speed and Power Service
pPower->addCharacteristic(&cpMeasurementCharacteristics);
pPower->addCharacteristic(&cpFeatureCharacteristics);
pPower->addCharacteristic(&sensorLocationCharacteristics);
uint32_t cpfeature = (1 << 3); // Bit index 3 - Crank rev present
cpFeatureCharacteristics.setValue(cpfeature);
uint8_t sensorlocation = 6; // Right crank -
sensorLocationCharacteristics.setValue(&sensorlocation, 1);
// Add UUIDs for Services to BLE Service Advertising
pServer->getAdvertising()->addServiceUUID(powerService);
// Start service
pPower->start();
}
void PowerServer::update(short watts, ulong crankCount, ulong msCrankEventTime)
{
// Least significant byte first
std::string measure; // flags (byte), revCount (uint32), eventTime (uint16)
measure.reserve(8);
// 16bit - Flags
uint16_t flags = (1 << 5); // Crank Measurement present
measure += (byte)flags;
measure += (byte)(flags >> 8) & 0xFF;
// sint16 - Instantaneous Power
measure += (byte)watts & 0xFF;
measure += (byte)(watts >> 8) & 0xFF;
// uint16 - Crank Revolution Data- Cumulative Crank Revolutions
measure += (byte)crankCount & 0xFF;
measure += (byte)(crankCount >> 8) & 0xFF;
// uint16 - Crank Revolution Data- Last Crank Event Time
uint16_t lastCrankTimeInTicks = (((ulong)(msCrankEventTime * (1024.0 / 1000))) & 0xFFFF);
measure += (byte)lastCrankTimeInTicks & 0xFF;
measure += (byte)(lastCrankTimeInTicks >> 8) & 0xFF;
cpMeasurementCharacteristics.setValue(measure);
cpMeasurementCharacteristics.notify();
}