/
blink1-async.ts
352 lines (313 loc) · 10.2 KB
/
blink1-async.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const _blink1Lib = require('node-blink1');
export enum Blink1_LEDN {
ALL = 0,
LEDA = 1,
LEDB = 2
}
export type RGBData = {
r: number;
g: number;
b: number;
}
export type PatternLineData = {
r: number;
g: number;
b: number;
fadeMillis: number
}
export enum BlinkRate {
SLOW = 1000,
MED = 500,
FAST = 250,
VERY_FAST = 100
}
const MAX_BLINK_DURATION = 0x1 << 30;
console.log('MAX:', MAX_BLINK_DURATION);
/**
* An asynchronouse api for controlling blink(1) USB LED devices.
*/
export class Blink1 {
// The blink(1) instance from node-blink1 package that does all of the work.
private _blink1: any;
/**
* Find all connected blink(1) devices.
* @returns Array of found blink(1) serial numbers.
*/
static devices(): Array<string> {
return _blink1Lib.devices();
}
/**
* Utility a sender can await on from an async function or method
* to create a delay-like experience in execution.
* @param [millis=1000] - delay period in milliseconds, if millis <= 0 then 2^31
* @returns A Promise that the sender can await on to simulate a delay
*/
static delay(millis=1000): Promise<void> {
let duration = millis <= 0 || millis > MAX_BLINK_DURATION ? MAX_BLINK_DURATION : millis;
return new Promise(resolve => {
setTimeout(resolve, duration);
});
}
/**
* Create a high-level reference to a blink(1) HID device.
* @param [serialNumber] The serial number of the device to reference (see devices()).
* When undefined choose the first blink(1) device found.
* @param [enableDegamma=true] Enablement for degamma color correction (crappy name)
*/
constructor(serialNumber?: string, enableDegamma=true) {
this._blink1 = new _blink1Lib(serialNumber);
}
/**
* Get the degamma setting.
* Default value is true
* @returns degamma enabled flag
*/
get enableDegamma(): boolean {
return this._blink1.enableDegamma;
}
/**
* Update the degamma setting.
* @param enabled The new enabled or disabled value
*/
set enableDegamma(enabled: boolean) {
this._blink1.enableDegamma = enabled;
}
/**
* Return the version number of the blink(1) HID device.
* @returns Promise<number> with version string to await for completion
*/
version(): Promise<number> {
return new Promise(resolve => {
this._blink1.version(resolve);
});
}
// TODO - commented out getSerialNumber() and getId() as they are causing
// blink(1) device to crash. Investigate asap.
/**
* Access the blink(1) serial number
* @returns a Promise that returns the blink(1) device serialNumber
*/
// getSerialNumber(): Promise<string> {
// return new Promise((resolve) => {
// this._blink1.getId(resolve);
// });
// }
/**
* Access the blink(1) serial number
* @returns a Promise that returns the blink(1) device serialNumber
* @deprecated use getSerialNumber()
*/
// getId(): Promise<string> {
// return new Promise((resolve) => {
// this._blink1.getId(resolve);
// });
// }
/**
* Close the underlying HID device.
* @returns Promise<void> to await for completion
*/
close(): Promise<void> {
return new Promise(resolve => {
this._blink1.close(resolve);
});
}
/**
* Transition one or both LEDs to a new RGB color.
* @param fadeMillis The milliseconds for the transition.
* @param [red=0] The red color value [0-255].
* @param [green=0] The green color value [0-255].
* @param [blue=0] The blue color value [0-255].
* @param [led=Blink1_LEDN.ALL] Led(s) to update.
* @returns Promise<void> to await for completion
*/
fadeToRGB(fadeMillis: number, red=0, green=0, blue=0, led=Blink1_LEDN.ALL) {
return new Promise(resolve => {
fadeMillis > 0 ?
this._blink1.fadeToRGB(fadeMillis, red, green, blue, led, resolve) :
this._blink1.setRGB(red, green, blue);
});
}
/**
* Access the device current RGB value.
* @param led The led to read the RGB value from.
* @returns A promise that resolves to the current RGB values.
*/
rgb(led: Blink1_LEDN): Promise<RGBData> {
return new Promise(resolve => {
this._blink1.rgb(led, resolve);
});
}
/**
* Immediately output an RGB color. For mk2 devices both LEDs will
* present the RGB color.
* @param [red=0] The red color value [0-255].
* @param [green=0] The green color value [0-255].
* @param [blue=0] The blue color value [0-255].
* @returns Promise<void> to await for completion
*/
setRGB(red=0, green=0, blue=0): Promise<void> {
return new Promise(resolve => {
this._blink1.setRGB(red, green, blue, resolve);
});
}
/**
* Immediately disable output.
* @returns Promise<void> to await for completion
*/
off(): Promise<void> {
return new Promise(resolve => {
this._blink1.off(resolve);
});
}
/**
* Animate the sequence of color patterns in RAM beginning at a specific index in the sequence.
* @param startPosition The index [0-31] into the sequence of color patterns.
* @returns Promise<void> to await for completion
*/
play(startPosition=0): Promise<void> {
return new Promise(resolve => {
this._blink1.play(startPosition, resolve);
});
}
/**
* Repeatedly animate a range of color patterns in RAM
* beginning at start index in the sequence to an end index
* the sequence.
* @param [startIndex=0] The start index [0-31] in the sequence of color patterns
* @param [endIndex=0] The end index [0-31] in the sequence of color patterns
* @param [count=1] The number of times to loop, 0 = loop infinitely
* @returns Promise<void> to await for completion
*/
playLoop(startPosition=0, endPosition=0, count=0): Promise<void> {
return new Promise(resolve => {
this._blink1.playLoop(startPosition, endPosition, count, resolve);
});
}
/**
* Immediately stop presenting color patterns from RAM.
* @returns Promise<void> to await for completion
*/
stop(): Promise<void> {
return new Promise(resolve => {
this._blink1.pause(resolve);
});
}
/**
* Immediately stop presenting display patterns from RAM.
* @returns Promise<void> to await for completion
* @deprecated Use stop()
*/
pause(): Promise<void> {
return this.stop();
}
/**
* Return the color pattern data at position in RAM.
* @param [position=0] The index [0-31] into the sequence of color patterns
* @returns Promise<string> returnin the pattern line read
*/
readPatternLine(position=0): Promise<string> {
return new Promise(resolve => {
this._blink1.readPatternLine(position, resolve);
});
}
/**
* Insert a new color pattern into the sequence of color patterns in RAM.
* @param fadeMillis The duration in milliseconds for the transition from the current color to the new RGB colors of this pattern
* @param [red=0] The red color value [0-255].
* @param [green=0] The green color value [0-255].
* @param [blue=0] The blue color value [0-255].
* @param [position=0] The position [0-31] in the color sequence to insert this pattern.
* @returns Promise<void> to await for completion
*/
writePatternLine(fadeMillis: number, red=0, green=0, blue=0, position=0): Promise<void> {
return new Promise(resolve => {
this._blink1.writePatternLine(fadeMillis, red, green, blue, position, resolve);
});
}
/**
* Save all patterns in to Blink(1) non-volatile memory.
* @returns Promise<void> to await for completion
*/
savePattern(): Promise<void> {
return new Promise(resolve => {
this._blink1.savePattern(resolve);
});
}
/**
* Clear all pattern data from Blink(1) non-volatile memory.
* @returns Promise<void> to await for completion
*/
async clearPattern(): Promise<void> {
return new Promise(async resolve => {
for (let i=0; i< 32; i++) {
await this._blink1.writePatternLine(0, 0, 0, 0, i);
}
});
}
/**
* Set the led to which future writePattern() calls will apply.
* @param led The led to make default
* @returns Promise<void> to await for completion
*/
setLedN(led=Blink1_LEDN.ALL): Promise<void> {
return new Promise(resolve => {
this._blink1.setLedN(led, resolve);
});
}
/**
* Enable the ServerDown feature that will trigger a display pattern
* @param triggerMillis
* @returns Promise<void> to await for completion
*/
enableServerDown(triggerMillis=10000) {
return new Promise((resolve) => {
this._blink1.enableServerDown(triggerMillis, resolve);
});
}
/**
* Disable the ServerDown feature.
* @returns A promise to await on.
*/
disableServerDown() {
return new Promise(resolve => {
this._blink1.disableServerDown(resolve);
});
}
/**
* Play an alternating pattern of the rgb color parameter in an on/off sequence for
* the number cycles or infinitely.
*
* Note this method returns as soon as the play command is sent
* to the blink(1) device, not when the blinking command has completed.
*
* @param [red=0] The red color value [0-255].
* @param [green=0] The green color value [0-255].
* @param [blue=0] The blue color value [0-255].
* @param [duration=1000] Blink for duration milliseconds, 0 = blink infinitely
* @returns Promise<void> to await for completion
*/
async blink(red=0, green=0, blue=0, rate=BlinkRate.MED, duration=0): Promise<void> {
let cnt = 0;
// setup COLOR ON pattern
await this.writePatternLine(rate, red, green, blue, cnt++);
// setup COLOR OFF pattern
await this.writePatternLine(rate, 0, 0, 0, cnt++);
// start blink loop
await this.playLoop(0, cnt-1, 0); // play continuously
await Blink1.delay(duration);
}
/**
* Send an array of PatternLineData to a blink(1) device.
* @param patterns The array of PatternLineData
* @param [startPos=0] Start writing patternLineData at this line
* @returns Promise<void> to await for completion
*/
async writePattern(patterns: Array<PatternLineData>, startPos=0): Promise<void> {
let pos = startPos;
return new Promise(resolve => {
patterns.forEach(async pattern => {
await this.writePatternLine(pattern.fadeMillis, pattern.r, pattern.g, pattern.b, pos++);
});
})
}
}