opc
v1.1.3
Published
Open Pixel Control protocol
Downloads
20
Maintainers
Readme
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 server
var Socket = require("net").Socket;
var socket = new Socket();
socket.setNoDelay();
socket.connect(7890);
// Create an Open Pixel Control stream and pipe it to the server
var createOPCStream = require("opc");
var stream = createOPCStream();
stream.pipe(socket);
// Create a strand representing connected lights
var createStrand = require("opc/strand");
var strand = createStrand(512); // Fadecandy has 512 addresses
var left = strand.slice(0, 64); // Fadecandy pin 0
var right = strand.slice(64, 128); // Fadecand pin 1
// Set all left pixels to red and right to blue
for (var i = 0; i < 64; i++) {
left.setPixel(i, 255, 0, 0);
right.setPixel(i, 0, 0, 255);
}
// Write the pixel colors to the device on channel 0
stream.writePixels(0, strand.buffer);
Stream
var createStream = require("opc");
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 = require("opc/strand");
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 = require("opc/parser");
var createStrand = require("opc/strand");
require("net").createServer(function(connection) {
connection.pipe(createParser()).on("data", function(message) {
console.log("Message");
console.log(" Channel:", message.channel);
console.log(" Command:", message.command);
console.log(" Data length:", message.data.length);
// Read pixel colors
if (message.command === 0) {
var strand = createStrand(message.data);
for (var i = 0; strand.length < i; i++) {
console.log(" Pixel", i, strand.getPixel(i));
}
}
});
});
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