22
33var BigRedButton = require ( './BigRedButton' ) ;
44
5- var bigRedButton ;
6-
7- for ( var i = 0 ; i < BigRedButton . deviceCount ( ) ; i ++ ) {
8-
9- console . log ( 'opening BigRedButton' , i ) ;
10-
11- bigRedButton = new BigRedButton . BigRedButton ( i ) ;
12-
13- bigRedButton . on ( 'buttonPressed' , function ( ) {
14- console . log ( 'button pressed' ) ;
15- } ) ;
16-
17- bigRedButton . on ( 'buttonReleased' , function ( ) {
18- console . log ( 'button released' ) ;
19- } ) ;
20-
21- bigRedButton . on ( 'lidRaised' , function ( ) {
22- console . log ( 'lid raised' ) ;
23- } ) ;
24- bigRedButton . on ( 'lidClosed' , function ( ) {
25- console . log ( 'lid closed' ) ;
26- } ) ;
27-
28- }
5+ function configureButton ( button ) {
6+ button . on ( 'buttonPressed' , function ( ) {
7+ console . log ( 'button pressed' ) ;
8+ } ) ;
9+
10+ button . on ( 'buttonReleased' , function ( ) {
11+ console . log ( 'button released' ) ;
12+ } ) ;
13+
14+ button . on ( 'lidRaised' , function ( ) {
15+ console . log ( 'lid raised' ) ;
16+ } ) ;
17+ button . on ( 'lidClosed' , function ( ) {
18+ console . log ( 'lid closed' ) ;
19+ } ) ;
20+
21+ button . on ( 'buttonGone' , function ( ) {
22+ console . log ( 'button gone' ) ;
23+ setTimeout ( newButton , 1000 ) ;
24+ } ) ;
25+ }
26+
27+ function newButton ( ) {
28+ console . log ( "Getting button 0" )
29+ try {
30+ bigRedButton = new BigRedButton . BigRedButton ( 0 ) ;
31+ }
32+ catch ( err ) {
33+ console . log ( "No button, waiting" ) ;
34+ setTimeout ( newButton , 1000 ) ;
35+ return ;
36+ }
37+
38+ configureButton ( bigRedButton ) ;
39+ console . log ( "Configured button 0" )
40+ return ;
41+ }
42+
43+ newButton ( ) ;
0 commit comments