Skip to content

Commit c1d192d

Browse files
committed
Merge pull request #3 from blainesch/feature/workups
Feature/workups - all looks good from here.
2 parents 3c89917 + 0b1f73a commit c1d192d

File tree

1 file changed

+59
-50
lines changed

1 file changed

+59
-50
lines changed

BigRedButton.js

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,95 @@ var util = require('util');
66
var events = require('events');
77

88
var allDevices;
9-
var cmd_status=new Buffer([ 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 ]);
10-
var last_state;
9+
var cmdStatus=new Buffer([ 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 ]);
10+
var lastState;
1111

1212
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+
if (!allDevices) {
17+
allDevices = HID.devices(7476,13);
18+
}
19+
return allDevices;
2020
}
2121

2222
function 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.hid = new HID.HID(bigRedButton[index].path);
36-
37-
this.hid.write(cmd_status);
38-
39-
var that=this;
40-
this.hid.read(function(error,data) {
41-
last_state=data[0];
42-
that.hid.read(that.interpretData.bind(that));
43-
});
44-
setInterval(this.askForStatus.bind(this),100);
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(cmdStatus);
39+
40+
var that=this;
41+
this.hid.read(function(error,data) {
42+
lastState=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+
};
4553
}
4654

4755
util.inherits(BigRedButton, events.EventEmitter);
4856

4957
BigRedButton.prototype.askForStatus = function() {
50-
this.hid.write(cmd_status);
58+
this.hid.write(cmdStatus);
5159
};
5260

5361
BigRedButton.prototype.interpretData = function(error, data) {
54-
var n_state=data[0];
55-
56-
if(last_state!=n_state) {
57-
if(last_state==LID_DOWN && n_state==LID_UP) {
58-
this.emit("lidRaised");
59-
} else if (last_state==LID_UP && n_state==BUTTON_DOWN) {
60-
this.emit("buttonPressed");
61-
} else if(last_state==BUTTON_DOWN && n_state==LID_UP) {
62-
this.emit("buttonReleased");
63-
} else if(last_state==BUTTON_DOWN && n_state==LID_DOWN) {
64-
this.emit("buttonReleased");
65-
this.emit("lidClosed");
66-
} else if(last_state==LID_UP && n_state==LID_DOWN) {
67-
this.emit("lidClosed");
68-
}
69-
last_state=n_state;
62+
if (!this.interval || error || !data) {
63+
this.close();
64+
return;
65+
}
66+
var newState=data[0];
67+
68+
if (lastState!=newState) {
69+
if (lastState==LID_DOWN && newState==LID_UP) {
70+
this.emit("lidRaised");
71+
} else if (lastState==LID_UP && newState==BUTTON_DOWN) {
72+
this.emit("buttonPressed");
73+
} else if (lastState==BUTTON_DOWN && newState==LID_UP) {
74+
this.emit("buttonReleased");
75+
} else if (lastState==BUTTON_DOWN && newState==LID_DOWN) {
76+
this.emit("buttonReleased");
77+
this.emit("lidClosed");
78+
} else if (lastState==LID_UP && newState==LID_DOWN) {
79+
this.emit("lidClosed");
80+
}
81+
lastState=newState;
7082
}
7183

7284
this.hid.read(this.interpretData.bind(this));
7385
}
7486

7587
BigRedButton.prototype.isLidUp = function() {
76-
return last_state==LID_UP || last_state==BUTTON_DOWN;
88+
return lastState==LID_UP || lastState==BUTTON_DOWN;
7789
}
7890

7991
BigRedButton.prototype.isButtonPressed = function() {
80-
return last_state==BUTTON_DOWN;
92+
return lastState==BUTTON_DOWN;
8193
}
8294

8395
BigRedButton.prototype.isLidDown = function() {
84-
return last_state==LID_DOWN;
96+
return lastState==LID_DOWN;
8597
}
8698

8799
exports.BigRedButton = BigRedButton;
88100
exports.deviceCount = function () { return getAllDevices().length; }
89-
90-
91-

0 commit comments

Comments
 (0)