Skip to content

Commit

Permalink
Peloton responsiveness improvements. (#20)
Browse files Browse the repository at this point in the history
Emit power and cadence as soon as they are received.

Co-authored-by: ptx2 <[email protected]>
  • Loading branch information
jeremydk and ptx2 authored Oct 10, 2020
1 parent 8b86526 commit aef3838
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/bikes/peloton.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {once, EventEmitter} from 'events';
import {Timer} from '../util/timer';
import util from 'util';

const SerialPort = require('serialport')
Expand All @@ -25,9 +24,6 @@ export class PelotonBikeClient extends EventEmitter {
// initial stats
this.power = 0;
this.cadence = 0;

this._timer = new Timer(1);
this._timer.on('timeout', this.onStatsUpdate);
}

async connect() {
Expand All @@ -40,7 +36,6 @@ export class PelotonBikeClient extends EventEmitter {
this._parser = this._port.pipe(new Delimiter({ delimiter: PACKET_DELIMITER }));
this._parser.on('data', this.onSerialMessage);

this._timer.reset();
this.state = 'connected';
}

Expand All @@ -62,12 +57,14 @@ export class PelotonBikeClient extends EventEmitter {

onSerialMessage(data) {
switch(data[1]) {
case 65:
this.cadence = decodePeloton(data, data[2], false);
return;
case 68:
this.power = decodePeloton(data, data[2], true);
return;
case 65:
this.cadence = decodePeloton(data, data[2], false);
this.onStatsUpdate();
return;
case 68:
this.power = decodePeloton(data, data[2], true);
this.onStatsUpdate();
return;
}
}

Expand Down

0 comments on commit aef3838

Please sign in to comment.