Skip to content

Prismatik Node.js API

Matthew Ng edited this page Jul 1, 2014 · 15 revisions

This is not a full feature API that Prismatik provides from their Github.

The file is located here: prismatik-client.js

This should be cross-platform (using only Node.js methods).

Add to Project

To add this to your project, you need prismatik-client.js to your project and you need to reference it as usual with require.

var prismatik = require("./prismatik-client");

API Reference

Overview

You can interface with Prismatik (when it is running). This API wraps a socket client to their application.

var prismatik = require("./prismatik-client");

// Connect to Prismatik
var options = {
   host: "127.0.0.1",  // Localhost
   port: 3636          // Default Prismatik port
};
prismatik.connect(function(isConnected) {
   if (isConnected) {
      prismatik.lock(function(success) {
         if (success) {
            prismatik.setBrightness(100);
            prismatik.setColorToAll(255, 255, 0);    // Set all LEDs' colors to yellow
         } else {
            console.log("Could not lock, something else has already connected.");
            prismatik.disconnect();
         }
      });
   } else {
      console.log("Failed to connect to Prismatik");
   }
}, options);

API List

prismatik.connect(fn, opts)

  • Params: fn - callback function
  • Params: [optional] opts - object with any of the following keys
    • host: specifies which host to connect to Prismatik (default is localhost, 127.0.0.1)
    • port: port specified in Prismatik application (default is 3636)
    • apikey: if Prismatik specifies for an API key, then you have to match it
  • Callback: fn - first argument is if socket has successfully connected to Prismatik

Connects to Prismatik with the options specified. If no options specified, it will use the defaults. The callback is triggered after connection or timeout with the first argument as a boolean to see if it connected or not. This does not mean that Lightpack has connected. Normally you lock after calling this or else no other commands will work.

prismatik.disconnect(fn)

  • Callback: fn - first argument is either true or false if it worked or not

Disconnects the socket from Prismatik. This will also run unlock.

prismatik.getCountLeds(fn)

  • Params: fn - callback function
  • Callback: fn - first argument is a number multiples of 10 if connected, 0 if not

Returns the number of LEDs this API is connected to.

prismatik.getProfiles(fn)

  • Callback: fn - first argument is an array of profile names (strings) made using Prismatik

prismatik.getProfile(fn)

  • Callback: fn - first argument is the current Prismatik's profile name (a string)

prismatik.getStatus(fn)

  • Callback: fn - first argument is one of the following
    • "status:on"
    • "status:off"
    • "status:device error"
    • "status:unknown"

Gets the status of what Prismatik is currently doing with this socket.

prismatik.getAPIStatus(fn)

  • Callback: first argument is one of the following
    • "statusapi:busy"
    • "statusapi:idle"

Gets the status of the API.

prismatik.setProfile(name, fn)

  • Params: name - string name of an existing profile from Prismatik
  • Callback: fn - first argument is either true or false if it worked or not

Changes the current profile set in Prismatik.

prismatik.setGamma(n, fn)

  • Params: n - set the gamma value from 0.01 to 10.0 (default is 2.2)
  • Callback: fn - first argument is either true or false if it worked or not

prismatik.setSmooth(n, fn)

  • Params: n - set the smooth value from 0 to 255
  • Callback: fn - first argument is either true or false if it worked or not

prismatik.setBrightness(n, fn)

  • Params: n - set the brightness value from 0 to 100
  • Callback: fn - first argument is either true or false if it worked or not

prismatik.setColorToAll(r, g, b, fn)

  • Params: r - set red part of color
  • Params: g - set green part of color
  • Params: b - set blue part of color
  • Callback: fn - first argument is either true or false if it worked or not

Sets the colors to all LEDs.

prismatik.setColors(rgbArray, fn)

  • Params: rgbArray - an array of colors with the following format
    • rgb array: [r,b,g]
    • single rgb value: rgb
    • skip led: -1
  • Callback: fn - true or false if it worked (or is connected)

This will set colors to multiple LEDs given a list of colors. Since this is slightly complicated to use, view the example below:

Example

prismatik.setColors([
   [255, 0, 0],    // LED 0 - Red
   -1,             // LED 1 - Skip
   [0, 255, 0],    // LED 2 - Green
   (255 | 255 & 0xFF00 | 0 & 0xFF0000) // LED 3 - Yellow
]);

prismatik.setColor(n, r, g, b, fn)

  • Params: n - which LED (0->10 or more) to set the color
  • Params: r - red color (0->255)
  • Params: g - red color (0->255)
  • Params: b - red color (0->255)
  • Callback: fn - true or false if it worked (or is connected)

This will set a color to one LED.

prismatik.lock(fn)

  • Callback: fn - true or false if it worked (or is connected)

Locks this application to Prismatik and nothing else can use it. If the argument in the callback is false, then it failed to lock and you cannot use any of the functions in this API except disconnect.

prismatik.unlock(fn)

  • Callback: fn - true or false if it worked (or is connected)

Unlocks this application from Prismatik. You do not need to use this when disconnecting because disconnect will unlock as well.

prismatik.on(evtName, fn)

  • Params: evtName - Event name, only one of the following:
    • error: when there is a socket error, this callback will run
  • Callback: fn - this function will run when an event occurs