-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cycling Speed and Cadence to server. (#39)
This enables cadence broadcasting via the BLE CSC service. e.g. for apps like Peloton on iOS/Android.
- Loading branch information
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/servers/ble/services/cycling-speed-and-cadence/characteristics/csc-feature.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Characteristic, Descriptor} from '@abandonware/bleno'; | ||
|
||
/** | ||
* Bluetooth LE GATT CSC Feature Characteristic implementation. | ||
*/ | ||
export class CscFeatureCharacteristic extends Characteristic { | ||
constructor() { | ||
super({ | ||
uuid: '2a5c', | ||
properties: ['read'], | ||
descriptors: [ | ||
new Descriptor({ | ||
uuid: '2901', | ||
value: 'CSC Feature' | ||
}) | ||
], | ||
value: Buffer.from([2,0]) // crank revolution data | ||
}) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/servers/ble/services/cycling-speed-and-cadence/characteristics/csc-measurement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {Characteristic, Descriptor} from '@abandonware/bleno'; | ||
|
||
const FLAG_HASCRANKDATA = (1<<1); | ||
const CRANK_TIMESTAMP_SCALE = 1024 / 1000; // timestamp resolution is 1/1024 sec | ||
|
||
/** | ||
* Bluetooth LE GATT CSC Measurement Characteristic implementation. | ||
*/ | ||
export class CscMeasurementCharacteristic extends Characteristic { | ||
constructor() { | ||
super({ | ||
uuid: '2a5b', | ||
properties: ['notify'], | ||
descriptors: [ | ||
new Descriptor({ | ||
uuid: '2903', | ||
value: Buffer.alloc(2) | ||
}) | ||
] | ||
}) | ||
} | ||
|
||
/** | ||
* Notify subscriber (e.g. Zwift) of new CSC Measurement. | ||
* @param {object} measurement - new csc measurement. | ||
* @param {object} measurement.crank - last crank event. | ||
* @param {number} measurement.crank.revolutions - revolution count at last crank event. | ||
* @param {number} measurement.crank.timestamp - timestamp at last crank event. | ||
*/ | ||
updateMeasurement({ crank }) { | ||
let flags = 0; | ||
|
||
const value = Buffer.alloc(5); | ||
|
||
const revolutions16bit = crank.revolutions & 0xffff; | ||
const timestamp16bit = Math.round(crank.timestamp * CRANK_TIMESTAMP_SCALE) & 0xffff; | ||
value.writeUInt16LE(revolutions16bit, 1); | ||
value.writeUInt16LE(timestamp16bit, 3); | ||
flags |= FLAG_HASCRANKDATA; | ||
|
||
value.writeUInt8(flags, 0); | ||
|
||
if (this.updateValueCallback) { | ||
this.updateValueCallback(value) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/servers/ble/services/cycling-speed-and-cadence/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {PrimaryService} from '@abandonware/bleno'; | ||
import {CscMeasurementCharacteristic} from './characteristics/csc-measurement'; | ||
import {CscFeatureCharacteristic} from './characteristics/csc-feature'; | ||
|
||
/** | ||
* Bluetooth LE GATT Cycling Speed and Cadence Service implementation. | ||
*/ | ||
export class CyclingSpeedAndCadenceService extends PrimaryService { | ||
/** | ||
* Create a CyclingSpeedAndCadenceService instance. | ||
*/ | ||
constructor() { | ||
super({ | ||
uuid: '1816', | ||
characteristics: [ | ||
new CscMeasurementCharacteristic(), | ||
new CscFeatureCharacteristic(), | ||
] | ||
}) | ||
} | ||
|
||
/** | ||
* Notify subscriber (e.g. Zwift) of new CSC Measurement. | ||
* @param {object} measurement - new csc measurement. | ||
* @param {object} measurement.crank - last crank event. | ||
* @param {number} measurement.crank.revolutions - revolution count at last crank event. | ||
* @param {number} measurement.crank.timestamp - timestamp at last crank event. | ||
*/ | ||
updateMeasurement(measurement) { | ||
this.characteristics[0].updateMeasurement(measurement) | ||
} | ||
} |