forked from luxdvie/WS2812Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrip.js
97 lines (84 loc) · 1.67 KB
/
strip.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
/**
* Put your number of LED's here
*/
var NUM_LEDS = 152;
var ws281x = require("rpi-ws281x-native");
var pixelData = new Uint32Array(NUM_LEDS);
var Lights = [];
ws281x.init(NUM_LEDS);
function strip() {
this.NUM_LEDS = NUM_LEDS;
this.Mode = "";
this.Lights = [];
this.brightness = 255
this.color = "0xffffff"
this.Clear = function () {
ws281x.reset();
};
/**
* Clear all LED's back to 0x00000 and render
*/
this.Off = function () {
this.SetBrightness(0);
this.Mode = "STOP";
};
/**
* Get Stirp Status for Homekit
*/
this.GetStripStatus = function () {
if (this.Mode === "STOP") {
return 0
} else {
return 1
}
}
/**
* Turn Strip on with last value.
*/
this.On = function () {
this.Mode = "On"
if (this.brightness === 0) {
this.brightness = 100;
}
// ws281x.setBrightness(this.brightness)
this.SetStripColor(this.color)
this.SetBrightness(this.brightness)
};
/**
* Assign the brightness of the whole strip.
*/
this.SetBrightness = function (brightness) {
if (brightness || brightness === 0) {
this.brightness = brightness
}
ws281x.setBrightness(this.brightness);
};
/**
* get brightness.
*/
this.GetBrightness = function () {
return this.brightness
};
/**
* Set a single color for all LED's
*/
this.SetStripColor = function (color) {
this.color = color
for (var i = 0; i < NUM_LEDS; i++) {
this.Lights[i] = color;
}
this.Render();
};
/**
* Render the current state of the LED strip
*/
this.Render = function () {
var tmp = [];
for (var i = 0; i < NUM_LEDS; i++) {
if (i > NUM_LEDS) break;
tmp[i] = this.Lights[i];
}
ws281x.render(tmp);
};
}
module.exports = new strip();