forked from meg768/rpi-ws281x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (74 loc) · 2.8 KB
/
index.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
var path = require("path");
var addon = require(path.join(__dirname, "build", "Release", "rpi-ws281x.node"));
class Module {
constructor() {
this.map = undefined;
this.leds = undefined;
}
configure(options) {
var {width, height, map, leds, ...options} = options;
if (width != undefined || height != undefined) {
if (width == undefined) {
throw new Error('Must specify width if height is specified.');
}
if (height == undefined) {
throw new Error('Must specify height if width is specified.');
}
if (leds != undefined) {
throw new Error('Please do not specify leds when both width and height are specified.');
}
leds = width * height;
if (typeof map == 'string') {
if (map == 'matrix') {
map = new Uint32Array(width * height);
for (var i = 0; i < map.length; i++)
map[i] = i;
}
else if (map == 'alternating-matrix') {
map = new Uint32Array(width * height);
for (var i = 0; i < map.length; i++) {
var row = Math.floor(i / width), col = i % width;
if ((row % 2) === 0) {
map[i] = i;
}
else {
map[i] = (row+1) * width - (col+1);
}
}
}
}
}
// Make sure the number of leds are specified
if (leds == undefined) {
throw new Error('Number of leds must be defined. Either by leds or by width and height.');
}
// If no map specified, create a default one...
if (map == undefined) {
map = new Uint32Array(leds);
for (var i = 0; i < leds; i++)
map[i] = i;
}
// Make sure we have a correct instance of pixel mapping
if (!(map instanceof Uint32Array))
throw new Error('Pixel mapping must be an Uint32Array.');
if (map.length != leds)
throw new Error('Pixel mapping array must be of the same size as the number of leds.');
this.map = map;
this.leds = leds;
addon.configure({...options, leds:leds});
}
reset() {
if (this.leds != undefined) {
this.render(new Uint32Array(this.leds));
addon.reset();
}
}
sleep(ms) {
addon.sleep(ms);
}
render(pixels) {
if (this.map != undefined)
addon.render(pixels, this.map);
}
}
module.exports = new Module();