node-piglow
v2 does not contain the commandline interface and the animations interface anymore. They are moved to two seperate modules. (node-piglow-cli and node-piglow-animations)
v3 removes node 0.10 and 0.12 support and only works for node versions >4
The piGlow is a little LED-Board for the Raspberry Pi sold by Pimoroni. It features 18 LEDs in six different colors (white, blue, green, yellow, orange, red) arranged like a vortex:
This module offers an interface to control the individual LEDs.
In action video: http://www.youtube.com/watch?v=s-rD8PfAke8
TOC
Installation
$ npm install piglow
Setup
$ sudo vi /etc/modules
Add these two lines
i2c-bcm2708 i2c-dev
$ sudo vi /etc/modprobe.d/raspi-blacklist.conf
Comment out blacklist i2c-bcm2708
#blacklist i2c-bcm2708
Invocation
var piGlow = ; //callback fires when board is initialized;
Adressing
To each LED a brightness value between 0 (off) and 255 (freakin' bright) can be assigned. If one preferrs percentage values, as a convenience function all values smaller than 1 are treated as percentage values. Note that the value of '1' is not treated as 100% but as the brightness value of 1!
Individual LEDs
//parameter sets the brightness:pil_0_0 = 100; //sets LED 1 of leg 1 to a brightness of 100 (of 255)pil_0_1 = 10; //sets LED 2 of leg 1 to a brightness of 10pil_0_1 = 05; //sets LED 2 of leg 1 to a brightness of 50% (=brightness of 127)...pil_2_5 = 200; //sets LED 6 of leg 3 to a brightness of 200 //shorthand form:pil_0_0; //sets LED 1 of leg 1 to a brightness of 255
Legs
pileg_0 = 100; //sets all LEDs of leg 1 to a brightness of 100 //shorthandpileg_0; //sets all LEDs of leg 1 to 255
Rings
piring_0 = 100; //sets LED 1 of leg 1, LED 1 of leg 2 and LED 1 of leg 3 to 100 //shorthandpiring_0; //sets LED 1 of leg 1, LED 1 of leg 1 and LED 1 of leg 2 to 255
As the rings are distinguishable by color (order from outer ring to the inner: red, orange, yellow, green, blue, white), they can be adressed via the ring's color:
pired = 100; //sets the first ring to a brightness of 100 //shorthandpired; //sets the first ring to maximum brightness
All LEDs
piall = 100; //set all LEDs to 100 //shorthandpiall; //set all LEDs to 255 (watch your eyes) pireset; //set all LEDs to 0
Random
pirandom = 05; //shorthandpirandom;
The propbability of lighting up can be defined (pi.random = 0.1;
) and is otherwise calculated via this formula: (0.4 + Math.random() * 0.2);
.
The brightness is calculated via this formula: parseInt(MAX_VALUE / 2 + (MAX_VALUE / 2 * Math.random()), 10)
Transactions
Each parameter that is set causes the backend to transfer the complete set of values to the piglow board. Thus the following operation would cause three write operations:
pil_0_1 = 100;pil_0_2 = 100;pil_0_3 = 100;
The piglow-interface offers the possibility to open up a transaction and to commit it when all changes have been made. So the following code will cause only one write to the hardware board:
pi;pil_0_1 = 100;pil_0_2 = 100;pil_0_3 = 100;pi;
This benefits performance especially when the LEDs are changed in high frequency.
Animations
Do you like your piglow animated? Checkout piglow-animations!
Command-Line-Interface
node-piglow-cli wraps piglow and offers a command line interface. You can than invoke the piglow like this (lights up the red LEDs):
$ piglow --red
Possible use cases:
- use it in your Makefile to indicate a sucessfull built
- use it in your CI server to indicate failed (or sucessfull) built
- ...
Mocking
This module also exposes its internal structure, with the possibility to invoke the piGlow interface with a injected mocking backend.
There are two backends, BackendMock
prints the piglow data as JSON, BackendMockPrettyPrint
structures the data in a readable way.
var piGlow = ;var PiGlowBackendMock = piGlowBackendMock;var piGlowInterface = piGLowpiGlowInterface; var myMock = ;var myInterface = ; //lets hackmyInterfacering_0 = 255;
This way the module can be used in a non raspi environment for development or with a testing mock for unit tests. To implement your own mocks follow this interface:
{} PiGlowMockprototype { /* piGlowConfiguration is a object in the following form: { "l_0_0":0, "l_0_1":0, "l_0_2":0, "l_0_3":0, "l_0_4":0, "l_0_5":0, "l_1_0":0, "l_1_1":0, "l_1_2":0, "l_1_3":0, "l_1_4":0, "l_1_5":0, "l_2_0":0, "l_2_1":0, "l_2_2":0, "l_2_3":0, "l_2_4":0, "l_2_5":0 } */};
Used in
- piglow-system a system utlity tool that shows metrics about your system
- piglow-clock a binary watch implemented via the piglow