1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Add Gamma flag and Brightness control to node-red-node-pi-neopixel (#431)

This commit is contained in:
cymplecy 2018-04-14 23:17:12 +01:00 committed by Dave Conway-Jones
parent 025f97206f
commit 0f55fc160e
4 changed files with 63 additions and 18 deletions

View File

@ -36,9 +36,12 @@ LED_GAMMA = [
222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255] 222,224,227,229,231,233,235,237,239,241,244,246,248,250,252,255]
LED_COUNT = int(sys.argv[1]) LED_COUNT = max(0,int(sys.argv[1]))
WAIT_MS = int(sys.argv[2]) WAIT_MS = max(0,int(sys.argv[2]))
MODE = sys.argv[3] MODE = sys.argv[3]
LED_BRIGHTNESS = min(255,int(max(0,float(sys.argv[4])) * 255 / 100))
if (sys.argv[5].lower() != "true"):
LED_GAMMA = range(256)
def getRGBfromI(RGBint): def getRGBfromI(RGBint):
blue = RGBint & 255 blue = RGBint & 255
@ -59,6 +62,12 @@ def setPixels(strip, s, e, color, wait_ms=30):
strip.show() strip.show()
time.sleep(wait_ms/1000.0) time.sleep(wait_ms/1000.0)
def setBrightness(strip, brightness, wait_ms=30):
"""Set overall brighness"""
strip.setBrightness(brightness)
strip.show()
time.sleep(wait_ms/1000.0)
def colorWipe(strip, color, wait_ms=30): def colorWipe(strip, color, wait_ms=30):
"""Wipe color across display a pixel at a time.""" """Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()): for i in range(strip.numPixels()):
@ -145,6 +154,9 @@ if __name__ == '__main__':
try: try:
data = raw_input() data = raw_input()
bits = data.split(',') bits = data.split(',')
if len(bits) == 2:
if bits[0] == "brightness":
setBrightness(strip, min(255,max(0,int(bits[1]))), WAIT_MS)
if len(bits) == 3: if len(bits) == 3:
if MODE == "shiftu": if MODE == "shiftu":
shiftUp(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS) shiftUp(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS)

24
hardware/neopixel/neopixel.html Normal file → Executable file
View File

@ -31,6 +31,16 @@
<option value="grb">GRB</option> <option value="grb">GRB</option>
</select> </select>
</div> </div>
<div class="form-row">
<label for="node-input-brightness"><i class="fa fa-sun-o"></i> Brightness</label>
<input type="text" id="node-input-brightness" placeholder="number" style="width:60px;"> &nbsp;(0-100)
</div>
<div class="form-row">
<label for="node-input-gamma"> </label>
<label for="node-input-gamma" style="width:70%">
<input type="checkbox" id="node-input-gamma" style="display:inline-block; width:22px; vertical-align:baseline;"> Apply gamma correction
</label>
</div>
<br/> <br/>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
@ -53,6 +63,8 @@
<!-- <p>The whole strip is set by <code>msg.payload</code> with a CSV string <i>r,g,b</i> --> <!-- <p>The whole strip is set by <code>msg.payload</code> with a CSV string <i>r,g,b</i> -->
<p>A range of pixels from <i>x</i> to <i>y</i> can be set by <code>msg.payload</code> <p>A range of pixels from <i>x</i> to <i>y</i> can be set by <code>msg.payload</code>
with a CSV string <i>x,y,r,g,b</i> with a CSV string <i>x,y,r,g,b</i>
<p>By default, gamma correction is enabled but it can disabled which can be useful for working with low brightness levels</p>
<p><code>msg.brightness</code> can be used to dynamically set brightness level</p>
<p>The pixels data line should be connected to Pi physical pin 12 - GPIO 18. <i>Note:</i> <p>The pixels data line should be connected to Pi physical pin 12 - GPIO 18. <i>Note:</i>
this may conflict with audio playback.</p> this may conflict with audio playback.</p>
<p align="right"><a href="http://flows.nodered.org/node/node-red-node-pi-neopixel#usage">More info&nbsp;&nbsp;</a></p> <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 @@
fgnd: { value:"" }, fgnd: { value:"" },
wipe: { value:"40", required:true, validate:RED.validators.number() }, wipe: { value:"40", required:true, validate:RED.validators.number() },
mode: { value:"pcent" }, mode: { value:"pcent" },
rgb: { value:"rgb" } rgb: { value:"rgb" },
brightness: { value:"100", required:true, validate:RED.validators.number() },
gamma: { value: true }
}, },
inputs:1, inputs:1,
outputs:0, outputs:0,
@ -82,6 +96,14 @@
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
}, },
oneditprepare: function() { oneditprepare: function() {
if (this.gamma === undefined) {
this.gamma = true;
$("#node-input-gamma").prop('checked', true);
}
if (this.brightness === undefined) {
this.brighness = "100";
$("#node-input-brightness").val("100");
}
var setstate = function () { var setstate = function () {
if ($('#node-input-mode').val().indexOf("shift") !== -1) { if ($('#node-input-mode').val().indexOf("shift") !== -1) {
$("#bgcol").hide(); $("#bgcol").hide();

12
hardware/neopixel/neopixel.js Normal file → Executable file
View File

@ -35,8 +35,13 @@ module.exports = function(RED) {
this.fgnd = n.fgnd || "128,128,128"; this.fgnd = n.fgnd || "128,128,128";
this.mode = n.mode || "pcent"; this.mode = n.mode || "pcent";
this.rgb = n.rgb || "rgb"; this.rgb = n.rgb || "rgb";
this.gamma = n.gamma;
if (this.gamma === undefined) { this.gamma = true; }
this.brightness = Number(n.brightness || 100);
this.wipe = Number(n.wipe || 40); this.wipe = Number(n.wipe || 40);
if (this.wipe < 0) { this.wipe = 0; } if (this.wipe < 0) { this.wipe = 0; }
if (this.brightness < 0) { this.brightness = 0; }
if (this.brightness > 100) { this.brightness = 100; }
var node = this; var node = this;
var needle = "255,255,255"; var needle = "255,255,255";
//var p1 = /^\#[A-Fa-f0-9]{6}$/ //var p1 = /^\#[A-Fa-f0-9]{6}$/
@ -45,6 +50,9 @@ module.exports = function(RED) {
var p4 = /^[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+$/ var p4 = /^[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+$/
function inputlistener(msg) { function inputlistener(msg) {
if (msg.hasOwnProperty("brightness")) {
node.child.stdin.write("brightness,"+msg.brightness.toString()+"\n");
}
if (msg.hasOwnProperty("payload")) { if (msg.hasOwnProperty("payload")) {
var pay = msg.payload.toString().toUpperCase(); var pay = msg.payload.toString().toUpperCase();
var parts = pay.split(","); var parts = pay.split(",");
@ -101,8 +109,8 @@ module.exports = function(RED) {
else { node.warn("Invalid payload : "+pay); } else { node.warn("Invalid payload : "+pay); }
} }
} }
node.warn("GAMMA: "+node.gamma);
node.child = spawn(piCommand, [node.pixels, node.wipe, node.mode]); node.child = spawn(piCommand, [node.pixels, node.wipe, node.mode, node.brightness, node.gamma]);
node.status({fill:"green",shape:"dot",text:"ok"}); node.status({fill:"green",shape:"dot",text:"ok"});
node.on("input", inputlistener); node.on("input", inputlistener);

29
hardware/neopixel/package.json Normal file → Executable file
View File

@ -1,22 +1,25 @@
{ {
"name" : "node-red-node-pi-neopixel", "name": "node-red-node-pi-neopixel",
"version" : "0.0.16", "version": "0.0.17",
"description" : "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.", "description": "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.",
"dependencies" : { "dependencies": {},
}, "repository": {
"repository" : { "type": "git",
"type":"git", "url": "https://github.com/node-red/node-red-nodes/tree/master/hardware/neopixel"
"url":"https://github.com/node-red/node-red-nodes/tree/master/hardware/neopixel"
}, },
"license": "Apache-2.0", "license": "Apache-2.0",
"keywords": [ "node-red", "ws2812", "neopixel" ], "keywords": [
"node-red" : { "node-red",
"nodes" : { "ws2812",
"neopixel"
],
"node-red": {
"nodes": {
"rpi-neopixels": "neopixel.js" "rpi-neopixels": "neopixel.js"
} }
}, },
"scripts" : { "scripts": {
"postinstall" : "scripts/checklib.js" "postinstall": "scripts/checklib.js"
}, },
"author": { "author": {
"name": "Dave Conway-Jones", "name": "Dave Conway-Jones",