Skip to content

Commit d3761a7

Browse files
committed
Modifications to support plugging and unplugging plus new test
1 parent c1d192d commit d3761a7

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

BigRedButton.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ var LID_DOWN=0x15, LID_UP=0x17, BUTTON_DOWN=0x16;
1313

1414
function getAllDevices()
1515
{
16-
if (!allDevices) {
17-
allDevices = HID.devices(7476,13);
18-
}
19-
return allDevices;
16+
allDevices = HID.devices(7476,13);
17+
return allDevices;
2018
}
2119

2220
function BigRedButton(index)
@@ -29,6 +27,7 @@ function BigRedButton(index)
2927
if (!bigRedButton.length) {
3028
throw new Error("No BigRedButton could be found");
3129
}
30+
3231
if (index > bigRedButton.length || index < 0) {
3332
throw new Error("Index " + index + " out of range, only " + bigRedButton.length + " BigRedButton found");
3433
}
@@ -49,6 +48,7 @@ function BigRedButton(index)
4948
setTimeout(function() {
5049
this.hid.close();
5150
}.bind(this), 100);
51+
this.emit("buttonGone");
5252
};
5353
}
5454

BigRedButtonTest.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,42 @@
22

33
var 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

Comments
 (0)