Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cadence accuracy. #15

Merged
merged 2 commits into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/app/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down