Open Pixel Control
Control lights using the Open Pixel Control protocol.
Module Scope
This module handles binary encoding and decoding of the Open Pixel Control protocol and provides a virtual address space for pixels.
This module was created to control Fadecandy devices, but it should work as a generic tool to create Open Pixel Control messages.
Usage
This module provides three components:
- Encoder: Write binary messages
- Decoder: Parse binary messages
- Strand: Model of a strand of pixels and virtual address space
Example
// Create TCP connection to Open Pixel Control servervar Socket = Socket;var socket = ;socket;socket; // Create an Open Pixel Control stream and pipe it to the servervar createOPCStream = ;var stream = ;stream; // Create a strand representing connected lightsvar createStrand = ;var strand = ; // Fadecandy has 512 addressesvar left = strand; // Fadecandy pin 0var right = strand; // Fadecand pin 1// Set all left pixels to red and right to bluefor var i = 0; i < 64; i++ left; right; // Write the pixel colors to the device on channel 0stream;
Stream
var createStream = ;
var stream = createStream()
Creates a stream that emits Open Pixel Control protocol messages.
stream.writePixels(channel, pixels)
Emits a set pixel colors command message with the color data in the pixels buffer.
stream.writeColorCorrection(config)
Emits a Fadecandy set global color correction command message with the given config object.
stream.writeMessage(channel, command, data)
Emits a generic Open Pixel Control message. Data should be a buffer.
Strand
var createStrand = ;
var strand = createStrand(length)
Create a strand object representing a series of length pixels. The
strand provides a strand.setPixel()
function to set the color of each
pixel.
Pixel state is stored in buffer (strand.buffer)
in Open Pixel
Control format (i.e., used in the data block of a set pixel color
command).
strand.setPixel(index, r, g, b)
Set color at index to the given rgb value.
strand.getPixel(index)
Return an array representing the rgb color value at index (e.g.,
[255, 0, 105]
).
strand.slice(start, end)
Returns a new strand which references the same state as the old, but offset and cropped by the start and end indexes.
Modifying the new strand slice will modify the original strand!
strand.buffer
Binary data representing the strand's pixel colors. The size of the
buffer will be strand.length * 3
bytes.
strand.length
The number of pixels in the strand.
Parser
var createParser = ;var createStrand = ; ;
var parser = createParser()
Create a transform stream that parses binary data written to it an emits Open Pixel Control messages. Message objects have the following properties:
channel
: The channel idcommand
: The command iddata
: A buffer containing the message data
Installation
npm install opc