Fadecandy, Neopixels and ofxOPC

Overview
ofxOPC is a openFrameworks addon that gives users the ability to send data to LEDs simply by drawing graphics to a screen. The addon uses @scanlime’s amazing Fadecandy unit.

For those who may be unfamiliar with the Fadecandy, its a small USB powered LED driver that is able to control up to 512 individual LEDs through its 8 output pins or 64 channels per pin. Check out @scanlime’s github for more on how the unit itself works.

Fadecandy Unit

How the addon works?
The addon pulls colour information from the screen, which is defined by the user. The addon then sends this data in TCP packets to the Fadecandy server which is decoded by the Fadecandy device and passed to the LEDs.

How to use the addon?
Within the addon repository there are multiple examples and explanations on how the addon interacts with openFrameworks. But for the purpose of this post I’ll give a really quick breakdown of how to use the addon. Before starting you will need to either download or clone both the addon https://github.com/DHaylock/ofxOPC and the Fadecandy server https://github.com/scanlime/fadecandy.

Step 1 – Include the addon
Place the addon in the addons folder of your openFrameworks folder. Make a new project with the project generator adding both ofxOPC and ofxNetwork.

Next declare both an ofxOPC and Neopixel object.

ofxOPC opcClient;
NeoPixelRing12px ring12px;

There are numerous Neopixel objects within the src folder.

Step 2 – Make the Connection
The Fadecandy server as default runs locally at 127.0.0.1 on port 7890. You can alter these but for now we will leave them.
In the .cpp file setup the ofxOPC giving it both variables.

opcClient.setup("127.0.0.1", 7890);
ring12px.setupLedRing();

Step 3 – Generate what you want the LEDs to display.
For this example we will draw four of these images rotating with different colors.dot

// ofApp.h file 
ofImage dot;

// ofApp.cpp
//Setup 
dot.loadImage("dot.png");

//Draw
int size = 70;
ofPushMatrix();
ofTranslate(0, 0);
ofPushMatrix();
ofTranslate(ofGetWidth()/2,ofGetHeight()/2);
ofRotateZ(ofGetElapsedTimeMillis()/10);
ofPushMatrix();
ofTranslate(-size,-size);
ofEnableBlendMode(OF_BLENDMODE_ADD);
ofSetColor(0, 255,20);
dot.draw(size/4, size/4, size,size);
ofSetColor(255, 0,20);
dot.draw((size/4*3), size/4, size,size);
ofSetColor(0, 0,255);
dot.draw(size/4, (size/4*3), size,size);
ofSetColor(255, 0,255);
dot.draw((size/4*3),(size/4*3), size,size);
ofDisableBlendMode();
ofPopMatrix();
ofPopMatrix();
ofPopMatrix();

Step 4 – Grab the Data
When you are ready to send stuff to the LEDs add the following lines to the update function.
Its important to ensure the Fadecandy is connected and the server is running before running the application, otherwise it will almost certainly crash.

Ensure you have a black background otherwise the addon will pull the background colour.

    // Grab data from the centre of the window
    ring12px.grabImageData(ofPoint(ofGetWidth()/2,ofGetHeight()/2));
    ring12px.update();

    // If the client is not connected do not try and send information
    if (!opcClient.isConnected())
    {
        // Will continue to try and reconnect to the Pixel Server
        opcClient.tryConnecting();
    }
    else
    {
        // Write out the first set of data
        opcClient.writeChannelOne(ring12px.colorData());
    }

Step 5 – Draw and Enjoy
Add the following lines to the draw function to visualise the output of the LEDs.

 // Visual Representation of the Grab Area
    ring12px.drawGrabRegion(hide);
    
    ring12px.drawRing(50, 50);

Screen Shot 2014-10-06 at 11.15.20

Fire up the Server by running the fcserver script, which is found in the bin/ directory of fadecandy folder. The server should open a terminal window and display all connected units.
Screen Shot 2014-10-06 at 11.34.17

Run the oF application and hey presto your LEDs should alight.

Check out the other examples in the addon folder.

Maybe try multiple strips to make a video curtain:

Or include Syphon feeds: