@@ -13,49 +13,49 @@ var LID_DOWN=0x15, LID_UP=0x17, BUTTON_DOWN=0x16;
1313
1414function getAllDevices ( )
1515{
16- if ( ! allDevices ) {
17- allDevices = HID . devices ( 7476 , 13 ) ;
18- }
19- return allDevices ;
16+ if ( ! allDevices ) {
17+ allDevices = HID . devices ( 7476 , 13 ) ;
18+ }
19+ return allDevices ;
2020}
2121
2222function BigRedButton ( index )
2323{
24- if ( ! arguments . length ) {
25- index = 0 ;
26- }
27-
28- var bigRedButton = getAllDevices ( ) ;
29- if ( ! bigRedButton . length ) {
30- throw new Error ( "No BigRedButton could be found" ) ;
31- }
32- if ( index > bigRedButton . length || index < 0 ) {
33- throw new Error ( "Index " + index + " out of range, only " + bigRedButton . length + " BigRedButton found" ) ;
34- }
35- this . button = bigRedButton [ index ] ;
36- this . hid = new HID . HID ( bigRedButton [ index ] . path ) ;
37-
38- this . hid . write ( cmd_status ) ;
39-
40- var that = this ;
41- this . hid . read ( function ( error , data ) {
42- last_state = data [ 0 ] ;
43- that . hid . read ( that . interpretData . bind ( that ) ) ;
44- } ) ;
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- } ;
24+ if ( ! arguments . length ) {
25+ index = 0 ;
26+ }
27+
28+ var bigRedButton = getAllDevices ( ) ;
29+ if ( ! bigRedButton . length ) {
30+ throw new Error ( "No BigRedButton could be found" ) ;
31+ }
32+ if ( index > bigRedButton . length || index < 0 ) {
33+ throw new Error ( "Index " + index + " out of range, only " + bigRedButton . length + " BigRedButton found" ) ;
34+ }
35+ this . button = bigRedButton [ index ] ;
36+ this . hid = new HID . HID ( bigRedButton [ index ] . path ) ;
37+
38+ this . hid . write ( cmd_status ) ;
39+
40+ var that = this ;
41+ this . hid . read ( function ( error , data ) {
42+ last_state = data [ 0 ] ;
43+ that . hid . read ( that . interpretData . bind ( that ) ) ;
44+ } ) ;
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+ } ;
5353}
5454
5555util . inherits ( BigRedButton , events . EventEmitter ) ;
5656
5757BigRedButton . prototype . askForStatus = function ( ) {
58- this . hid . write ( cmd_status ) ;
58+ this . hid . write ( cmd_status ) ;
5959} ;
6060
6161BigRedButton . prototype . interpretData = function ( error , data ) {
@@ -65,39 +65,36 @@ BigRedButton.prototype.interpretData = function(error, data) {
6565 }
6666 var n_state = data [ 0 ] ;
6767
68- if ( last_state != n_state ) {
69- if ( last_state == LID_DOWN && n_state == LID_UP ) {
70- this . emit ( "lidRaised" ) ;
71- } else if ( last_state == LID_UP && n_state == BUTTON_DOWN ) {
72- this . emit ( "buttonPressed" ) ;
73- } else if ( last_state == BUTTON_DOWN && n_state == LID_UP ) {
74- this . emit ( "buttonReleased" ) ;
75- } else if ( last_state == BUTTON_DOWN && n_state == LID_DOWN ) {
76- this . emit ( "buttonReleased" ) ;
77- this . emit ( "lidClosed" ) ;
78- } else if ( last_state == LID_UP && n_state == LID_DOWN ) {
79- this . emit ( "lidClosed" ) ;
80- }
81- last_state = n_state ;
68+ if ( last_state != n_state ) {
69+ if ( last_state == LID_DOWN && n_state == LID_UP ) {
70+ this . emit ( "lidRaised" ) ;
71+ } else if ( last_state == LID_UP && n_state == BUTTON_DOWN ) {
72+ this . emit ( "buttonPressed" ) ;
73+ } else if ( last_state == BUTTON_DOWN && n_state == LID_UP ) {
74+ this . emit ( "buttonReleased" ) ;
75+ } else if ( last_state == BUTTON_DOWN && n_state == LID_DOWN ) {
76+ this . emit ( "buttonReleased" ) ;
77+ this . emit ( "lidClosed" ) ;
78+ } else if ( last_state == LID_UP && n_state == LID_DOWN ) {
79+ this . emit ( "lidClosed" ) ;
80+ }
81+ last_state = n_state ;
8282 }
8383
8484 this . hid . read ( this . interpretData . bind ( this ) ) ;
8585}
8686
8787BigRedButton . prototype . isLidUp = function ( ) {
88- return last_state == LID_UP || last_state == BUTTON_DOWN ;
88+ return last_state == LID_UP || last_state == BUTTON_DOWN ;
8989}
9090
9191BigRedButton . prototype . isButtonPressed = function ( ) {
92- return last_state == BUTTON_DOWN ;
92+ return last_state == BUTTON_DOWN ;
9393}
9494
9595BigRedButton . prototype . isLidDown = function ( ) {
96- return last_state == LID_DOWN ;
96+ return last_state == LID_DOWN ;
9797}
9898
9999exports . BigRedButton = BigRedButton ;
100100exports . deviceCount = function ( ) { return getAllDevices ( ) . length ; }
101-
102-
103-
0 commit comments