-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater_rower.js
271 lines (223 loc) · 7.78 KB
/
water_rower.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var serialport = require('serialport');
var util = require('util');
var EventEmitter = require('events');
var debug = function() {};
function WaterRower( opts ) {
var opts = opts || {};
EventEmitter.call(this);
this.comPort = opts.port || "";
this.baudRate = opts.baudRate || 19200;//115200;
this.pollRate = opts.pollRate || 800;
this.lastPing = null;
this.stateHandler = this.stateDisconnected;
this.readings = {
strokesPerMinute: 0, // "stroke_pull is first subtracted from stroke_average then a modifier of 1.25 multipled by the result to generate the ratio value for display"
strokeCount: 0, // number
totalSpeed: 0, // cm/s
averageSpeed: 0, // cm/s
distance: 0, // m
heartRate: 0, // bpm
displayTimeSeconds: 0,
displayTimeMinutes: 0,
displayTimeHours: 0
};
debug("Creating new water rower");
debug("\tlistening on port " + this.comPort);
debug('\tlistening at rate ' + this.rate);
debug('\tpolling at rate ' + this.pollRate);
this.serialPort = new serialport.SerialPort( this.comPort, {
baudrate: this.baudRate,
disconnectedCallback: function () { this.emit('disconnect'); }.bind(this),
parser: serialport.parsers.readline("\r\n")
});
this.serialPort.on("open", function () {
debug('port ' + this.comPort + ' open');
// tell the waterrower that we're wanting to talk to it.
this.serialPort.write('USB\r\n');
}.bind(this));
this.serialPort.on("data", function(data) {
var trimmedData = data.trim();
debug('port ' + this.comPort + ' read ' + trimmedData );
this.ingestMessage( data );
}.bind(this));
this.serialPort.on("closed", function () {
debug('port ' + this.comPort + ' closed');
this.emit('disconnect');
}.bind(this));
this.serialPort.on("error", function( err ) {
debug('port ' + this.comPort + ' error ' + err);
this.emit('error', err);
}.bind(this));
}
util.inherits(WaterRower, EventEmitter);
var msgOnline = /^_WR_$/;
var msgOkay = /^OK$/;
var msgInteractive = /^AIA$/;
var msgPing = /^PING$/;
var msgError = /^ERROR$/;
var msgStrokeStart = /^SS$/;
var msgStrokeEnd = /^SE$/;
var msgStrokePulse = /^P([\dA-Fa-f]{2})$/;
var msgStrokeCount = /^IDD140([\dA-Fa-f]{4})$/;
var msgTotalSpeed = /^IDD148([\dA-Fa-f]{4})$/;
var msgAverageSpeed = /^IDD14A([\dA-Fa-f]{4})$/;
var msgDistance = /^IDD057([\dA-Fa-f]{4})$/;
var msgHeartrate = /^IDD1A0([\dA-Fa-f]{4})$/;
var msgStrokeInfo = /^IDD142([\dA-Fa-f]{2})([\dA-Fa-f]{2})$/;
var msgStrokeRate = /^IDS1A9([\dA-Fa-f]{2})$/;
var msgKeypress = /^AK([123456789Rr]{1})$/;
var msgWorkoutTime = /^IDT1E1([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})$/;
WaterRower.prototype.ingestMessage = function( msg ) {
debug('port ' + this.comPort + ' dispatch ' + msg );
// we consider *any* message, not just PING, as a confirmation of the controllers existence
this.lastPing = Date.now();
// handle key presses
var keypress = msg.match(msgKeypress);
if (keypress) {
this.emit('keypad', keypress[1] );
} else {
// handle other messages
switch(true) {
case msgStrokeStart.test(msg): this.emit('stroke start');
break;
case msgStrokeEnd.test(msg): this.emit('stroke end');
break;
case msgPing.test(msg): // fallthrough
case msgError.test(msg): // fallthrough
default: this.stateHandler = this.stateHandler( msg );
break;
}
}
};
WaterRower.prototype.shutdown = function () {
debug('requesting shutdown');
this.stateHandler = this.stateShuttingDown;
}
var kMsgWriteDelay = 25; // wait at least 25 milleseconds as per manual
WaterRower.prototype.delayedWrite = function( msg ) {
setTimeout( function() {
this.serialPort.write(msg);
}.bind(this), kMsgWriteDelay);
}
WaterRower.prototype.stateShuttingDown = function () {
debug('in state shutdown');
this.delayedWrite('EXIT\r\n');
return this.stateShuttingDown;
}
WaterRower.prototype.stateDisconnected = function ( msg ) {
debug('in state disconnected ' + msg);
if ( msgOnline.test(msg) ) {
this.emit('connect');
this.delayedWrite('RESET\r\n');
return this.statePreconnect;
} else {
return this.stateDisconnected;
}
}
WaterRower.prototype.statePreconnect = function ( msg ) {
debug('in state preconnect' + msg);
if ( msgOkay.test(msg) ) {
this.emit('connect');
this.serialPort.emit('data', "let's start this party");
return this.stateConnected;
} else {
return this.statePreconnect;
}
}
WaterRower.prototype.stateConnected = function ( msg ) {
debug('in state connected');
// in the conected state, we only care about starting the stroke_count chain
this.emit('readings', this.readings);
this.delayedWrite('IRD1400\r\n');
return this.stateAwaitingStrokeCount;
}
WaterRower.prototype.stateAwaitingStrokeCount = function ( msg ) {
debug('in state awaiting stroke count');
var matches = msg.match(msgStrokeCount);
if (matches){
// parse out the stroke count in the 'IDD140??\r\n' message,
this.readings.strokeCount = Number.parseInt( matches[1], 16);
this.delayedWrite('IRD148\r\n');
return this.stateAwaitingTotalSpeed;
} else {
return this.stateAwaitingStrokeCount;
}
}
WaterRower.prototype.stateAwaitingTotalSpeed = function ( msg ) {
debug('in state awaiting total speed');
var matches = msg.match(msgTotalSpeed);
if (matches){
this.readings.totalSpeed = Number.parseInt( matches[1], 16);
this.delayedWrite('IRD14A\r\n');
return this.stateAwaitingAverageSpeed;
} else {
return this.stateAwaitingTotalSpeed;
}
}
WaterRower.prototype.stateAwaitingAverageSpeed = function ( msg ) {
debug('in state awaiting average speed');
var matches = msg.match(msgAverageSpeed);
if (matches) {
this.readings.averageSpeed = Number.parseInt( matches[1], 16);
this.delayedWrite('IRD057\r\n');
return this.stateAwaitingDistance;
} else {
return this.stateAwaitingAverageSpeed;
}
}
WaterRower.prototype.stateAwaitingDistance = function ( msg ) {
debug('in state awaiting distance');
var matches = msg.match(msgDistance);
if (matches){
this.readings.distance = Number.parseInt( matches[1], 16);
this.delayedWrite('IRD1A0\r\n');
return this.stateAwaitingHeartrate;
} else {
return this.stateAwaitingDistance;
}
}
WaterRower.prototype.stateAwaitingHeartrate = function ( msg ) {
debug('in state awaiting heart rate');
var matches = msg.match(msgHeartrate);
if (matches){
this.readings.heartRate = Number.parseInt( matches[1], 16);
//this.delayedWrite('IRD142\r\n');
this.delayedWrite('IRS1A9\r\n');
return this.stateAwaitingStrokeInfo;
} else {
return this.stateAwaitingHeartrate;
}
}
WaterRower.prototype.stateAwaitingStrokeInfo = function ( msg ) {
debug('in state awaiting stroke info');
var matches = msg.match(msgStrokeRate);
if (matches){
this.readings.strokesPerMinute = Number.parseInt( matches[1], 16);
this.delayedWrite('IRT1E1\r\n');
return this.stateAwaitingWorkoutTime;
} else {
return this.stateAwaitingStrokeInfo;
}
}
WaterRower.prototype.stateAwaitingWorkoutTime = function ( msg ) {
debug('in state awaiting total workout time');
var matches = msg.match(msgWorkoutTime);
if (matches){
this.readings.displayTimeSeconds = Number.parseInt( matches[3], 10);
this.readings.displayTimeMinutes = Number.parseInt( matches[2], 10);
this.readings.displayTimeHours = Number.parseInt( matches[1], 10);
this.delayedWrite('IRD057\r\n');
return this.stateConnected;
} else {
return this.stateAwaitingWorkoutTime;
}
}
module.exports = function( opts ){
var opts = opts || {};
if (opts.debug) {
debug = console.log;
}
return {
WaterRower: WaterRower
};
}