npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

opc

v1.1.3

Published

Open Pixel Control protocol

Downloads

48

Readme

Open Pixel Control

build status

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 id
  • command: The command id
  • data: A buffer containing the message data

Installation

npm install opc