forked from meg768/rpi-ws281x
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
103 lines (82 loc) · 3.16 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
99
100
101
102
103
const Matrix = require('./leds/matrix');
const AlternatingMatrix = require('./leds/alternating-matrix');
const SerpentineMatrix = require('./leds/serpentine-matrix');
const CanvasMatrix = require('./leds/canvas-matrix');
let path = require("path");
let addon = require(path.join(__dirname, "build", "Release", "rpi-ws281x.node"));
class Module {
constructor() {
this.map = undefined;
this.leds = undefined;
this.matrix = null;
}
configure(options) {
const width = options.width || undefined;
const height = options.height || undefined;
let map = options.map || undefined;
let leds = options.leds || undefined;
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') {
this.matrix = new Matrix(width, height);
map = this.matrix.getMap();
} else if (map === 'alternating-matrix') {
this.matrix = new AlternatingMatrix(width, height);
map = this.matrix.getMap();
} else if (map === 'serpentine-matrix') {
this.matrix = new SerpentineMatrix(width, height);
map = this.matrix.getMap();
} else if (map === 'canvas-matrix') {
this.matrix = new CanvasMatrix(width, height);
map = this.matrix.getMap();
}
}
}
// 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 (let 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});
}
getPixel(x, y) {
return this.matrix.getByCoordinate(x, y);
}
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();