-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathws2812b.js
76 lines (70 loc) · 1.76 KB
/
ws2812b.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
var rmt = require("rmt");
rmt.setup(0, 2, { clock_div: 8, idle_output_en: false });
rmt.setPattern(0, 4, 1, 9, 0);
rmt.setPattern(1, 9, 1, 4, 0);
var leds = 12;
var buffer = new Array(3 * leds);
function drawPixel(index, r, g, b) {
var i = index * 3;
buffer[i] = g;
buffer[i+1] = r;
buffer[i+2] = b;
}
function clear(r, g, b) {
for (var index = 0; index < leds; index++) {
buffer[index * 3] = g;
buffer[index * 3 + 1] = r;
buffer[index * 3 + 2] = b;
}
}
while(true) {
for (var index = 0; index < leds; index++) {
drawPixel(index, 0xff, 0, 0);
rmt.sendSync(0, buffer);
process.delay(300);
drawPixel(index, 0, 0xff, 0);
rmt.sendSync(0, buffer);
process.delay(300);
drawPixel(index, 0, 0, 0xff);
rmt.sendSync(0, buffer);
process.delay(300);
}
for (var delay = 0; delay < 300; delay += 20) {
for (var pos = 0; pos < leds; pos++) {
clear(0, 0, 0);
drawPixel(pos, 255, 255, 255);
rmt.sendSync(0, buffer);
process.delay(300 - delay);
}
}
for (var index = 0; index < 255; index++) {
clear(index, 0, 0);
rmt.sendSync(0, buffer);
process.delay(16);
}
for (var index = 254; index >= 0; index--) {
clear(index, 0, 0);
rmt.sendSync(0, buffer);
process.delay(16);
}
for (var index = 0; index < 255; index++) {
clear(0, index, 0);
rmt.sendSync(0, buffer);
process.delay(16);
}
for (var index = 254; index >= 0; index--) {
clear(0, index, 0);
rmt.sendSync(0, buffer);
process.delay(16);
}
for (var index = 0; index < 255; index++) {
clear(0, 0, index);
rmt.sendSync(0, buffer);
process.delay(16);
}
for (var index = 254; index >= 0; index--) {
clear(0, 0, index);
rmt.sendSync(0, buffer);
process.delay(16);
}
}