Skip to content

Commit f27718d

Browse files
author
Blaine Schmeisser
committed
Give the user a method that allows you to close the HID device.
1 parent 3c89917 commit f27718d

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

BigRedButton.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff 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

4755
util.inherits(BigRedButton, events.EventEmitter);
@@ -51,6 +59,10 @@ BigRedButton.prototype.askForStatus = function() {
5159
};
5260

5361
BigRedButton.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) {

0 commit comments

Comments
 (0)