From d38c152983fe8105c08ce3071f3cda9238d577b7 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 12 Jan 2016 22:53:43 +0000 Subject: [PATCH] Add Shift modes to neopixel node --- hardware/neopixel/README.md | 28 ++++++++++++++++--------- hardware/neopixel/neopix.py | 37 ++++++++++++++++++++++++++++++--- hardware/neopixel/neopixel.html | 3 +++ hardware/neopixel/neopixel.js | 2 +- hardware/neopixel/package.json | 2 +- 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/hardware/neopixel/README.md b/hardware/neopixel/README.md index d3d2fb0c..723ac84f 100644 --- a/hardware/neopixel/README.md +++ b/hardware/neopixel/README.md @@ -2,14 +2,15 @@ node-red-node-pi-neopixel ========================= A Node-RED node to drive a strip -of Neopixel or WS2812 LEDs. +of Neopixel or WS2812 LEDs from a Raspberry Pi. Pre-requisites -------------- -The Unicorn HAT python drivers need to be pre-installed... This is the easiest way to get the neopixel driver installed... see the - -Pimorini Getting Started with Unicorn HAT page. +The Neopixel python driver need to be pre-installed... The easiest way to get +the driver installed is to use the Unicorn HAT drivers install script... see the + +Pimoroni Getting Started with Unicorn HAT page. curl -sS get.pimoroni.com/unicornhat | bash @@ -21,22 +22,29 @@ Usually this is `~/.node-red` npm install node-red-node-pi-neopixel +The data pin of the pixels should be connected to physical pin 12 - GPIO 18 of the Pi. +*Note:* this may conflict with audio playback. + Usage ----- Defaults to a bar chart style mode using configured foreground and background colours. -It can accept a number in **msg.payload** that can be either the number of pixels, or a percentage of the total length. +It can accept a number in **msg.payload** that can be either the number of pixels, +or a percentage of the total length. -If you want to change the foregound colour, you can set **msg.payload** to a comma separated string of `html_colour,length`. +If you want to change the foreground colour, you can set **msg.payload** to a +comma separated string of `html_colour,length` or `length,html_colour` -To set the background just set **msg.payload** `html_colour` name. +To set the background just set **msg.payload** to an `html_colour` name. Here is a list of html_colour names. -The `nth` pixel is set by **msg.payload** with a CSV string `n,r,g,b` +You can also select shift modes where a single colour pixel is added to either +the start or the end of the strip, shifting all the others along by one. + +The `nth` pixel can be set by **msg.payload** with a CSV string `n,r,g,b` , +where r, g and b are 0-255. A range of pixels from `x` to `y` can be set by **msg.payload** with a CSV string `x,y,r,g,b` - -The pixels data line should be connected to Pi physical pin 12 - GPIO 18. *Note:* this may conflict with audio playback. diff --git a/hardware/neopixel/neopix.py b/hardware/neopixel/neopix.py index f114f34f..cad5a96a 100755 --- a/hardware/neopixel/neopix.py +++ b/hardware/neopixel/neopix.py @@ -32,6 +32,13 @@ if sys.version_info >= (3,0): LED_COUNT = int(sys.argv[1]) WAIT_MS = int(sys.argv[2]) +MODE = sys.argv[3] + +def getRGBfromI(RGBint): + blue = RGBint & 255 + green = (RGBint >> 8) & 255 + red = (RGBint >> 16) & 255 + return red, green, blue # Define functions which animate LEDs in various ways. def setPixel(strip, i, color): @@ -53,6 +60,24 @@ def colorWipe(strip, color, wait_ms=30): strip.show() time.sleep(wait_ms/1000.0) +def shiftUp(strip, color, wait_ms=30): + """Shift all pixels one way.""" + for i in range(strip.numPixels()): + strip.setPixelColor(LED_COUNT-i, strip.getPixelColor(LED_COUNT-i-1)) + strip.show() + time.sleep(wait_ms/1000.0) + strip.setPixelColor(0,color) + strip.show() + +def shiftDown(strip, color, wait_ms=30): + """Shift all pixels the other way.""" + for i in range(strip.numPixels()): + strip.setPixelColor(i, strip.getPixelColor(i+1)) + strip.show() + time.sleep(wait_ms/1000.0) + strip.setPixelColor(LED_COUNT-1,color) + strip.show() + def wheel(pos): """Generate rainbow colors across 0-255 positions.""" if pos < 85: @@ -103,12 +128,18 @@ if __name__ == '__main__': data = raw_input() bits = data.split(',') if len(bits) == 3: - colorWipe(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS) - if len(bits) == 4: + if MODE == "shiftu": + shiftUp(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS) + elif MODE == "shiftd": + shiftDown(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS) + else: + colorWipe(strip, Color(int(bits[0]), int(bits[1]), int(bits[2])), WAIT_MS) + if (MODE[0] == 'p' and len(bits) == 4): setPixel(strip, int(bits[0]), Color(int(bits[1]), int(bits[2]), int(bits[3]) )) - if len(bits) == 5: + if (MODE[0] == 'p' and len(bits) == 5): setPixels(strip, int(bits[0]), int(bits[1]), Color(int(bits[2]), int(bits[3]), int(bits[4]) ), WAIT_MS) except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program sys.exit(0) except Exception as ex: print "bad data: "+data + print ex diff --git a/hardware/neopixel/neopixel.html b/hardware/neopixel/neopixel.html index ac6b749d..2e5beb65 100644 --- a/hardware/neopixel/neopixel.html +++ b/hardware/neopixel/neopixel.html @@ -32,6 +32,8 @@
@@ -53,6 +55,7 @@

To set the background just send an html colour name. Here is a list of html colour names.

+

You can also select shift modes where a single colour pixel is added to either the start or the end of the strip.

The nth pixel is set by msg.payload with a CSV string n,r,g,b

A range of pixels from x to y can be set by msg.payload diff --git a/hardware/neopixel/neopixel.js b/hardware/neopixel/neopixel.js index bf5740b0..ba9a5189 100644 --- a/hardware/neopixel/neopixel.js +++ b/hardware/neopixel/neopixel.js @@ -94,7 +94,7 @@ module.exports = function(RED) { } } - node.child = spawn(piCommand, [node.pixels, node.wipe]); + node.child = spawn(piCommand, [node.pixels, node.wipe, node.mode]); node.status({fill:"green",shape:"dot",text:"ok"}); node.on("input", inputlistener); diff --git a/hardware/neopixel/package.json b/hardware/neopixel/package.json index e1e25013..47abd421 100644 --- a/hardware/neopixel/package.json +++ b/hardware/neopixel/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pi-neopixel", - "version" : "0.0.1", + "version" : "0.0.2", "description" : "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.", "dependencies" : { },