@@ -25,7 +25,10 @@ const _options = {
25
25
verbose : false
26
26
} ;
27
27
28
- function OpenBCISimulator ( portName , options ) {
28
+ function Simulator ( portName , options ) {
29
+ if ( ! ( this instanceof Simulator ) ) {
30
+ return new Simulator ( portName , options ) ;
31
+ }
29
32
options = options || { } ;
30
33
var opts = { } ;
31
34
@@ -97,10 +100,9 @@ function OpenBCISimulator (portName, options) {
97
100
}
98
101
99
102
// This allows us to use the emitter class freely outside of the module
100
- // TODO: upgrade from old-style streams to stream.Duplex or stream.Transform
101
- util . inherits ( OpenBCISimulator , stream . Stream ) ;
103
+ util . inherits ( Simulator , EventEmitter ) ;
102
104
103
- OpenBCISimulator . prototype . flush = function ( callback ) {
105
+ Simulator . prototype . flush = function ( callback ) {
104
106
this . outputBuffered = 0 ;
105
107
106
108
clearTimeout ( this . outputLoopHandle ) ;
@@ -109,12 +111,12 @@ OpenBCISimulator.prototype.flush = function (callback) {
109
111
if ( callback ) callback ( ) ;
110
112
} ;
111
113
112
- OpenBCISimulator . prototype . isOpen = function ( ) {
114
+ Simulator . prototype . isOpen = function ( ) {
113
115
return this . connected ;
114
116
} ;
115
117
116
118
// output only size bytes of the output buffer
117
- OpenBCISimulator . prototype . _partialDrain = function ( size ) {
119
+ Simulator . prototype . _partialDrain = function ( size ) {
118
120
if ( ! this . connected ) throw new Error ( 'not connected' ) ;
119
121
120
122
if ( size > this . outputBuffered ) size = this . outputBuffered ;
@@ -129,7 +131,7 @@ OpenBCISimulator.prototype._partialDrain = function (size) {
129
131
} ;
130
132
131
133
// queue some data for output and send it out depending on options.fragmentation
132
- OpenBCISimulator . prototype . _output = function ( dataBuffer ) {
134
+ Simulator . prototype . _output = function ( dataBuffer ) {
133
135
// drain full buffers until there is no overflow
134
136
while ( this . outputBuffered + dataBuffer . length > this . outputBuffer . length ) {
135
137
var len = dataBuffer . copy ( this . outputBuffer , this . outputBuffered ) ;
@@ -185,7 +187,7 @@ OpenBCISimulator.prototype._output = function (dataBuffer) {
185
187
}
186
188
} ;
187
189
188
- OpenBCISimulator . prototype . write = function ( data , callback ) {
190
+ Simulator . prototype . write = function ( data , callback ) {
189
191
if ( ! this . connected ) {
190
192
/* istanbul ignore else */
191
193
if ( callback ) callback ( Error ( 'Not connected' ) ) ;
@@ -286,11 +288,11 @@ OpenBCISimulator.prototype.write = function (data, callback) {
286
288
}
287
289
} ;
288
290
289
- OpenBCISimulator . prototype . drain = function ( callback ) {
291
+ Simulator . prototype . drain = function ( callback ) {
290
292
if ( callback ) callback ( ) ;
291
293
} ;
292
294
293
- OpenBCISimulator . prototype . close = function ( callback ) {
295
+ Simulator . prototype . close = function ( callback ) {
294
296
if ( this . connected ) {
295
297
this . flush ( ) ;
296
298
@@ -304,7 +306,7 @@ OpenBCISimulator.prototype.close = function (callback) {
304
306
}
305
307
} ;
306
308
307
- OpenBCISimulator . prototype . _startStream = function ( ) {
309
+ Simulator . prototype . _startStream = function ( ) {
308
310
var intervalInMS = 1000 / this . options . sampleRate ;
309
311
310
312
if ( intervalInMS < 2 ) intervalInMS = 2 ;
@@ -341,31 +343,31 @@ OpenBCISimulator.prototype._startStream = function () {
341
343
} , intervalInMS ) ;
342
344
} ;
343
345
344
- OpenBCISimulator . prototype . _syncUp = function ( ) {
346
+ Simulator . prototype . _syncUp = function ( ) {
345
347
setTimeout ( ( ) => {
346
348
this . sendSyncSetPacket = true ;
347
349
} , 12 ) ; // 3 packets later
348
350
} ;
349
351
350
- OpenBCISimulator . prototype . _printEOT = function ( ) {
352
+ Simulator . prototype . _printEOT = function ( ) {
351
353
this . _output ( new Buffer ( '$$$' ) ) ;
352
354
} ;
353
355
354
- OpenBCISimulator . prototype . _printFailure = function ( ) {
356
+ Simulator . prototype . _printFailure = function ( ) {
355
357
this . _output ( new Buffer ( 'Failure: ' ) ) ;
356
358
} ;
357
359
358
- OpenBCISimulator . prototype . _printSuccess = function ( ) {
360
+ Simulator . prototype . _printSuccess = function ( ) {
359
361
this . _output ( new Buffer ( 'Success: ' ) ) ;
360
362
} ;
361
363
362
- OpenBCISimulator . prototype . _printValidatedCommsTimeout = function ( ) {
364
+ Simulator . prototype . _printValidatedCommsTimeout = function ( ) {
363
365
this . _printFailure ( ) ;
364
366
this . _output ( new Buffer ( 'Communications timeout - Device failed to poll Host' ) ) ;
365
367
this . _printEOT ( ) ;
366
368
} ;
367
369
368
- OpenBCISimulator . prototype . _processPrivateRadioMessage = function ( dataBuffer ) {
370
+ Simulator . prototype . _processPrivateRadioMessage = function ( dataBuffer ) {
369
371
switch ( dataBuffer [ 1 ] ) {
370
372
case k . OBCIRadioCmdChannelGet :
371
373
if ( this . options . firmwareVersion === k . OBCIFirmwareV2 ) {
@@ -479,6 +481,4 @@ OpenBCISimulator.prototype._processPrivateRadioMessage = function (dataBuffer) {
479
481
}
480
482
} ;
481
483
482
- util . inherits ( OpenBCISimulator , EventEmitter ) ;
483
-
484
- module . exports = OpenBCISimulator ;
484
+ module . exports = Simulator ;
0 commit comments