-
Notifications
You must be signed in to change notification settings - Fork 3
/
DelcomIndicator.js
157 lines (131 loc) · 4.69 KB
/
DelcomIndicator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
var hid = require('node-hid');
class DelcomIndicator {
constructor() {
this.vendorId = 0xFC5;
this.productId = 0xB080;
this.device = undefined;
this.green = 0xFE;
this.red = 0xFD;
this.blue = 0xFB;
this.off = 0xFF;
this.buzzerOn = 1;
this.buzzerOff = 0;
this.write8bytes = 101;
this.write16bytes = 102;
this.solidCommand = 2;
this.flashCommand = 20;
this.buzzCommand = 70;
this.readButtonCommand = 8;
this.readButtonCommandLength = 8;
this.device = this.findDevice();
if (this.device) {
this.deviceConnection = new hid.HID(this.device.path);
}
}
padValues(values) {
var desiredLength = 8;
if (values.length > 0 && values[0] === this.write16bytes) {
desiredLength = 16;
}
while (values.length < desiredLength) {
values.push(0);
}
return values;
}
writeToDevice(values) {
if (!this.isOpen()) {
throw "Device is not open";
}
if (process.platform === 'win32') {
this.deviceConnection.sendFeatureReport(this.padValues(values));
} else {
this.deviceConnection.write(values);
}
}
findDevice() {
var devices = hid.devices(this.vendorId, this.productId);
if (devices !== undefined) {
return devices[0];
}
}
isConnected() {
return this.device !== undefined;
}
isOpen() {
return this.deviceConnection;
}
close() {
if (this.deviceConnection) {
this.deviceConnection.close();
this.deviceConnection = undefined;
}
}
solidColor(colorBits) {
this.writeToDevice([this.write8bytes, this.solidCommand, colorBits]);
}
solidGreen() {
this.solidColor(this.green);
}
solidRed() {
this.solidColor(this.red);
}
solidBlue() {
this.solidColor(this.blue);
}
solidYellow() {
this.solidBlue();
}
flashGreen() {
this.writeToDevice([this.write8bytes, this.solidCommand, this.green]);
this.writeToDevice([this.write8bytes, this.flashCommand, 0, 1]);
}
flashRed() {
this.writeToDevice([this.write8bytes, this.flashCommand, this.red]);
this.writeToDevice([this.write8bytes, this.flashCommand, 0, 2]);
}
flashBlue() {
this.writeToDevice([this.write8bytes, this.flashCommand, this.blue]);
this.writeToDevice([this.write8bytes, this.flashCommand, 0, 4]);
}
flashYellow() {
this.flashBlue();
}
turnOff() {
this.writeToDevice([this.write8bytes, this.solidCommand, this.off]);
this.writeToDevice([this.write8bytes, this.flashCommand, this.off]);
}
/*
https://www.delcomproducts.com/downloads/USBIOHID.pdf
"The frequency is programmed by setting the buzzer’s frequency time variable, the units are in 256us. For
example a desired buzzer frequency of 1KHz would yield a frequency value of around 4. The
buzzer‘s on time and off time variables are used to program the duty cycle of the buzzer.
These units are in 50ms. If you want the buzzer to turn on and off every second you would
program 10 for the on time and off time. The repeat value dictates what mode the buzzer will
be in. If a value of zero is used for the repeat value then the buzzer will sound continuously at
the frequency specified until the user turns it off. If a value of 255 is used then the buzzer will
sound at the frequency and duty cycle specified until the user turns it off. If any other value is
used the buzzer will sound at the frequency and duty cycle specified and repeat for that many
times. The DataLSB turns this feature on (1) or off (0). The DataMSB sets the frequency. The
DataExt[0] sets the repeat value. The Data Ext[1] sets the on time. And the Data Ext[2] sets
the off time."
+-----------+----------+-----------+----------+-----------+----------+
| FreqValue | Freq(Hz) | FreqValue | Freq(Hz) | FreqValue | Freq(Hz) |
+-----------+----------+-----------+----------+-----------+----------+
| 1 | 3906 | 5 | 781 | 9 | 434 |
| 2 | 1953 | 6 | 651 | 10 | 390 |
| 3 | 1302 | 7 | 558 | 11 | 355 |
| 4 | 976 | 8 | 488 | 12 | 325 |
+-----------+----------+-----------+----------+-----------+----------+
*/
buzz(frequency, repeatCount, onTime, offTime) {
this.writeToDevice([this.write16bytes, this.buzzCommand, this.buzzerOn, frequency, 0, 0, 0, 0, repeatCount, onTime, offTime]);
}
turnOffBuzzer() {
this.writeToDevice([this.write16bytes, this.buzzCommand, this.buzzerOff]);
}
readButtonState() {
return this.deviceConnection.getFeatureReport(this.readButtonCommand, this.readButtonCommandLength);
}
}
module.exports = DelcomIndicator