Skip to content

Commit 0f55fc1

Browse files
cymplecydceejay
authored andcommitted
Add Gamma flag and Brightness control to node-red-node-pi-neopixel (#431)
1 parent 025f972 commit 0f55fc1

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

hardware/neopixel/neopix.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@
3636
222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255]
3737

3838

39-
LED_COUNT = int(sys.argv[1])
40-
WAIT_MS = int(sys.argv[2])
39+
LED_COUNT = max(0,int(sys.argv[1]))
40+
WAIT_MS = max(0,int(sys.argv[2]))
4141
MODE = sys.argv[3]
42+
LED_BRIGHTNESS = min(255,int(max(0,float(sys.argv[4])) * 255 / 100))
43+
if (sys.argv[5].lower() != "true"):
44+
LED_GAMMA = range(256)
4245

4346
def getRGBfromI(RGBint):
4447
blue = RGBint & 255
@@ -59,6 +62,12 @@ def setPixels(strip, s, e, color, wait_ms=30):
5962
strip.show()
6063
time.sleep(wait_ms/1000.0)
6164

65+
def setBrightness(strip, brightness, wait_ms=30):
66+
"""Set overall brighness"""
67+
strip.setBrightness(brightness)
68+
strip.show()
69+
time.sleep(wait_ms/1000.0)
70+
6271
def colorWipe(strip, color, wait_ms=30):
6372
"""Wipe color across display a pixel at a time."""
6473
for i in range(strip.numPixels()):
@@ -145,6 +154,9 @@ def rainbowCycle(strip, wait_ms=20, iterations=2):
145154
try:
146155
data = raw_input()
147156
bits = data.split(',')
157+
if len(bits) == 2:
158+
if bits[0] == "brightness":
159+
setBrightness(strip, min(255,max(0,int(bits[1]))), WAIT_MS)
148160
if len(bits) == 3:
149161
if MODE == "shiftu":
150162
shiftUp(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)

hardware/neopixel/neopixel.html

100644100755
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
<option value="grb">GRB</option>
3232
</select>
3333
</div>
34+
<div class="form-row">
35+
<label for="node-input-brightness"><i class="fa fa-sun-o"></i> Brightness</label>
36+
<input type="text" id="node-input-brightness" placeholder="number" style="width:60px;"> &nbsp;(0-100)
37+
</div>
38+
<div class="form-row">
39+
<label for="node-input-gamma"> </label>
40+
<label for="node-input-gamma" style="width:70%">
41+
<input type="checkbox" id="node-input-gamma" style="display:inline-block; width:22px; vertical-align:baseline;"> Apply gamma correction
42+
</label>
43+
</div>
3444
<br/>
3545
<div class="form-row">
3646
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
@@ -53,6 +63,8 @@
5363
<!-- <p>The whole strip is set by <code>msg.payload</code> with a CSV string <i>r,g,b</i> -->
5464
<p>A range of pixels from <i>x</i> to <i>y</i> can be set by <code>msg.payload</code>
5565
with a CSV string <i>x,y,r,g,b</i>
66+
<p>By default, gamma correction is enabled but it can disabled which can be useful for working with low brightness levels</p>
67+
<p><code>msg.brightness</code> can be used to dynamically set brightness level</p>
5668
<p>The pixels data line should be connected to Pi physical pin 12 - GPIO 18. <i>Note:</i>
5769
this may conflict with audio playback.</p>
5870
<p align="right"><a href="http://flows.nodered.org/node/node-red-node-pi-neopixel#usage">More info&nbsp;&nbsp;</a></p>
@@ -69,7 +81,9 @@
6981
fgnd: { value:"" },
7082
wipe: { value:"40", required:true, validate:RED.validators.number() },
7183
mode: { value:"pcent" },
72-
rgb: { value:"rgb" }
84+
rgb: { value:"rgb" },
85+
brightness: { value:"100", required:true, validate:RED.validators.number() },
86+
gamma: { value: true }
7387
},
7488
inputs:1,
7589
outputs:0,
@@ -82,6 +96,14 @@
8296
return this.name?"node_label_italic":"";
8397
},
8498
oneditprepare: function() {
99+
if (this.gamma === undefined) {
100+
this.gamma = true;
101+
$("#node-input-gamma").prop('checked', true);
102+
}
103+
if (this.brightness === undefined) {
104+
this.brighness = "100";
105+
$("#node-input-brightness").val("100");
106+
}
85107
var setstate = function () {
86108
if ($('#node-input-mode').val().indexOf("shift") !== -1) {
87109
$("#bgcol").hide();

hardware/neopixel/neopixel.js

100644100755
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ module.exports = function(RED) {
3535
this.fgnd = n.fgnd || "128,128,128";
3636
this.mode = n.mode || "pcent";
3737
this.rgb = n.rgb || "rgb";
38+
this.gamma = n.gamma;
39+
if (this.gamma === undefined) { this.gamma = true; }
40+
this.brightness = Number(n.brightness || 100);
3841
this.wipe = Number(n.wipe || 40);
3942
if (this.wipe < 0) { this.wipe = 0; }
43+
if (this.brightness < 0) { this.brightness = 0; }
44+
if (this.brightness > 100) { this.brightness = 100; }
4045
var node = this;
4146
var needle = "255,255,255";
4247
//var p1 = /^\#[A-Fa-f0-9]{6}$/
@@ -45,6 +50,9 @@ module.exports = function(RED) {
4550
var p4 = /^[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+$/
4651

4752
function inputlistener(msg) {
53+
if (msg.hasOwnProperty("brightness")) {
54+
node.child.stdin.write("brightness,"+msg.brightness.toString()+"\n");
55+
}
4856
if (msg.hasOwnProperty("payload")) {
4957
var pay = msg.payload.toString().toUpperCase();
5058
var parts = pay.split(",");
@@ -101,8 +109,8 @@ module.exports = function(RED) {
101109
else { node.warn("Invalid payload : "+pay); }
102110
}
103111
}
104-
105-
node.child = spawn(piCommand, [node.pixels, node.wipe, node.mode]);
112+
node.warn("GAMMA: "+node.gamma);
113+
node.child = spawn(piCommand, [node.pixels, node.wipe, node.mode, node.brightness, node.gamma]);
106114
node.status({fill:"green",shape:"dot",text:"ok"});
107115

108116
node.on("input", inputlistener);

hardware/neopixel/package.json

100644100755
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
{
2-
"name" : "node-red-node-pi-neopixel",
3-
"version" : "0.0.16",
4-
"description" : "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.",
5-
"dependencies" : {
6-
},
7-
"repository" : {
8-
"type":"git",
9-
"url":"https://github.com/node-red/node-red-nodes/tree/master/hardware/neopixel"
2+
"name": "node-red-node-pi-neopixel",
3+
"version": "0.0.17",
4+
"description": "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.",
5+
"dependencies": {},
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/node-red/node-red-nodes/tree/master/hardware/neopixel"
109
},
1110
"license": "Apache-2.0",
12-
"keywords": [ "node-red", "ws2812", "neopixel" ],
13-
"node-red" : {
14-
"nodes" : {
11+
"keywords": [
12+
"node-red",
13+
"ws2812",
14+
"neopixel"
15+
],
16+
"node-red": {
17+
"nodes": {
1518
"rpi-neopixels": "neopixel.js"
1619
}
1720
},
18-
"scripts" : {
19-
"postinstall" : "scripts/checklib.js"
21+
"scripts": {
22+
"postinstall": "scripts/checklib.js"
2023
},
2124
"author": {
2225
"name": "Dave Conway-Jones",

0 commit comments

Comments
 (0)