diff --git a/src/app/simulation.js b/src/app/simulation.js index cf368c2..25697e4 100644 --- a/src/app/simulation.js +++ b/src/app/simulation.js @@ -39,8 +39,8 @@ export class Simulation extends EventEmitter { * @emits Simulation#pedal * @private */ - onPedal() { - this._lastPedalTime = Date.now(); + onPedal(timestamp) { + this._lastPedalTime = Number.isFinite(timestamp) ? timestamp : Date.now(); /** * Pedal event. * @event Simulation#pedal @@ -56,10 +56,12 @@ export class Simulation extends EventEmitter { schedulePedal() { if (this._interval === Infinity) return; - let timeSinceLast = Date.now() - this._lastPedalTime; + let now = Date.now(); + let timeSinceLast = now - this._lastPedalTime; let timeUntilNext = Math.max(0, this._interval - timeSinceLast); + let nextPedalTime = now - timeSinceLast + this._interval; this._timeoutId = setTimeout(() => { - this.onPedal(); + this.onPedal(nextPedalTime); this.schedulePedal(); }, timeUntilNext); } diff --git a/src/server/services/cycling-power/characteristics/cycling-power-measurement.js b/src/server/services/cycling-power/characteristics/cycling-power-measurement.js index ae74d3b..3c4a9ac 100644 --- a/src/server/services/cycling-power/characteristics/cycling-power-measurement.js +++ b/src/server/services/cycling-power/characteristics/cycling-power-measurement.js @@ -1,7 +1,7 @@ import {Characteristic, Descriptor} from '@abandonware/bleno'; const FLAG_HASCRANKDATA = (1<<5); -const CRANK_TIMESTAMP_SCALE = 1000 / 1024; // timestamp resolution is 1/1024 sec +const CRANK_TIMESTAMP_SCALE = 1024 / 1000; // timestamp resolution is 1/1024 sec /** * Bluetooth LE GATT Cycling Power Measurement Characteristic implementation. @@ -41,7 +41,7 @@ export class CyclingPowerMeasurementCharacteristic extends Characteristic { // include crank data if provided if (crank) { const revolutions16bit = crank.revolutions & 0xffff; - const timestamp16bit = Math.floor(crank.timestamp * CRANK_TIMESTAMP_SCALE) & 0xffff; + const timestamp16bit = Math.round(crank.timestamp * CRANK_TIMESTAMP_SCALE) & 0xffff; value.writeUInt16LE(revolutions16bit, 4); value.writeUInt16LE(timestamp16bit, 6); flags |= FLAG_HASCRANKDATA;