File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ function BigRedButton(index)
3232 if ( index > bigRedButton . length || index < 0 ) {
3333 throw new Error ( "Index " + index + " out of range, only " + bigRedButton . length + " BigRedButton found" ) ;
3434 }
35+ this . button = bigRedButton [ index ] ;
3536 this . hid = new HID . HID ( bigRedButton [ index ] . path ) ;
3637
3738 this . hid . write ( cmd_status ) ;
@@ -41,7 +42,14 @@ function BigRedButton(index)
4142 last_state = data [ 0 ] ;
4243 that . hid . read ( that . interpretData . bind ( that ) ) ;
4344 } ) ;
44- setInterval ( this . askForStatus . bind ( this ) , 100 ) ;
45+ this . interval = setInterval ( this . askForStatus . bind ( this ) , 100 ) ;
46+ this . close = function ( ) {
47+ clearInterval ( this . interval ) ;
48+ this . interval = false ;
49+ setTimeout ( function ( ) {
50+ this . hid . close ( ) ;
51+ } . bind ( this ) , 100 ) ;
52+ } ;
4553}
4654
4755util . inherits ( BigRedButton , events . EventEmitter ) ;
@@ -51,6 +59,10 @@ BigRedButton.prototype.askForStatus = function() {
5159} ;
5260
5361BigRedButton . prototype . interpretData = function ( error , data ) {
62+ if ( ! this . interval || error || ! data ) {
63+ this . close ( ) ;
64+ return ;
65+ }
5466 var n_state = data [ 0 ] ;
5567
5668 if ( last_state != n_state ) {
You can’t perform that action at this time.
0 commit comments