rpi-ws281x
This is a npm module for connecting a Raspbery Pi to Neopixel strips. It uses the library from jgarff https://github.com/jgarff/rpi_ws281x.
Installation
$ npm install rpi-ws281x --save
Usage
var ws281x = ; // One time initializationws281x; // Create my pixelsvar pixels = 16; // Render pixels to the Neopixel stripws281x;
Methods
This module is simple and only has three methods configure(), render() and reset().
- configure(options) - Configures the ws281x strip. Must be called once and before anything else. See examples below.
- render(pixels) - Renders the pixels specified to the strip. The pixels parameter must be an Uint32Array representing the color values of all pixels and the same size as the number of leds specified when configuring.
- reset() - Resets configuration.
Examples
Filling the Neopixel strip with a color
var ws281x = ; { thisconfig = {}; // Number of leds in my strip thisconfigleds = 169; // Use DMA 10 (default 10) thisconfigdma = 10; // Set full brightness, a value from 0 to 255 (default 255) thisconfigbrightness = 255; // Set the GPIO number to communicate with the Neopixel strip (default 18) thisconfiggpio = 18; // The RGB sequence may vary on some strips. Valid values // are "rgb", "rbg", "grb", "gbr", "bgr", "brg". // Default is "rgb". // RGBW strips are not currently supported. thisconfigstrip = 'grb'; // Configure ws281x ws281x; } { // Create a pixel array matching the number of leds. // This must be an instance of Uint32Array. var pixels = thisconfigleds; // Create a fill color with red/green/blue. var red = 255 green = 0 blue = 0; var color = red << 16 | green << 8| blue; for var i = 0; i < thisconfigleds; i++ pixelsi = color; // Render to strip ws281x; } ; var example = ;example;
Walking a pixel through the strip
var ws281x = ; { // Current pixel position thisoffset = 0; // Set my Neopixel configuration thisconfig = leds:169; // Configure ws281x ws281x; } { var pixels = thisconfigleds; // Set a specific pixel pixelsthisoffset = 0xFF0000; // Move on to next thisoffset = thisoffset + 1 % thisconfigleds; // Render to strip ws281x; } { // Loop every 100 ms ; }; var example = ;example;
Walking a pixel with pixel mapping
var ws281x = ; { // Current pixel position thisoffset = 0; // Set my Neopixel configuration thisconfig = {}; // By setting width and height instead of number of leds // you may use named pixel mappings. // Currently "matrix" and "alternating-matrix" are // supported. You may also set the "map" property // to a custom Uint32Array to define your own map. thisconfigwidth = 13; thisconfigheight = 13; thisconfigmap = 'alternating-matrix'; // Configure ws281x ws281x; } { var leds = thisconfigwidth * thisconfigheight; var pixels = leds; // Set a specific pixel pixelsthisoffset = 0xFF0000; // Move on to next thisoffset = thisoffset + 1 % leds; // Render to strip ws281x; } { // Loop every 100 ms ; }; var example = ;example;