forked from ajones/node-pixelpusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelstrip.js
50 lines (38 loc) · 1.15 KB
/
pixelstrip.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
var Pixel = require('./pixel');
module.exports = function(stripId, numPixels) {
var that = this;
var pixels = [];
var STRIP_ID = stripId;
var NUM_PIXELS = numPixels;
// init strip
for (var i = 0; i < NUM_PIXELS; i ++){
pixels.push(new Pixel());
}
this.setStripColor = function(r, g, b, a){
for (var i = 0; i < NUM_PIXELS; i ++){
pixels[i].setColor(r, g, b, a);
}
};
this.getStripData = function(){
var strip = {
strip_id : STRIP_ID,
data : new Buffer(3 * NUM_PIXELS)
}
// fill the buffer with off pixels
strip.data.fill(0x00);
for (var i = 0, j = 0; i < NUM_PIXELS; i ++, j+=3){
var pixelData = pixels[i].toData3();
strip.data[j + 0] = pixelData[0];
strip.data[j + 1] = pixelData[1];
strip.data[j + 2] = pixelData[2];
}
return strip;
}
this.getRandomPixel = function(){
var randomIndex = Math.floor(Math.random() * NUM_PIXELS);
return pixels[randomIndex];
}
this.getPixel = function(idx){
return pixels[idx];
}
}