Skip to content

Commit

Permalink
Reset Peloton cadence/power when ride ends. (#37)
Browse files Browse the repository at this point in the history
Fixes #32.

Co-authored-by: Jeremy Klein <jeremydk@gmail.com>
  • Loading branch information
ptx2 and jeremydk authored Feb 2, 2021
1 parent d13bda5 commit 1dc4d0d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/bikes/peloton.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {once, EventEmitter} from 'events';
import {Timer} from '../util/timer';
import util from 'util';

const SerialPort = require('serialport')
const Delimiter = require('@serialport/parser-delimiter')

const PACKET_DELIMITER = Buffer.from('f6', 'hex');
const STATS_TIMEOUT = 1.0;

const debuglog = util.debuglog('gymnasticon:bikes:peloton');

Expand All @@ -24,6 +26,10 @@ export class PelotonBikeClient extends EventEmitter {
// initial stats
this.power = 0;
this.cadence = 0;

// reset stats to 0 when the user leaves the ride screen or turns the bike off
this.statsTimeout = new Timer(STATS_TIMEOUT, {repeats: false});
this.statsTimeout.on('timeout', this.onStatsTimeout.bind(this));
}

async connect() {
Expand Down Expand Up @@ -62,17 +68,25 @@ export class PelotonBikeClient extends EventEmitter {
case 65:
this.cadence = decodePeloton(data, data[2], false);
this.onStatsUpdate();
this.statsTimeout.reset();
return;
case 68:
this.power = decodePeloton(data, data[2], true);
this.onStatsUpdate();
this.statsTimeout.reset();
return;
}
}

onSerialClose() {
this.emit('disconnect', {address: this.address});
}

onStatsTimeout() {
this.power = 0;
this.cadence = 0;
this.onStatsUpdate();
}
}

export function decodePeloton(bufferArray, byteLength, isPower) {
Expand Down

0 comments on commit 1dc4d0d

Please sign in to comment.