Skip to content
Snippets Groups Projects
Commit a42e87e7 authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

Add a cute example for Processing

parent 3baaf462
Branches
Tags
No related merge requests found
/*
* Simple Open Pixel Control client for Processing,
* designed to sample each LED's color from some point on the canvas.
*
* Micah Elizabeth Scott, 2013
* This file is released into the public domain.
*/
import processing.net.Client;
import java.util.Arrays;
public class OPC {
PApplet app;
int[] pixelLocations;
byte[] packetData;
Client client;
String host;
int port;
boolean enableShowLocations;
OPC(PApplet app, String host, int port)
{
this.app = app;
this.host = host;
this.port = port;
app.registerDraw(this);
frameRate(30);
}
// Should the pixel sampling locations be visible? This helps with debugging.
void showLocations(boolean enabled)
{
enableShowLocations = enabled;
}
// Set the location of a single LED
void led(int index, int x, int y)
{
// For convenience, automatically grow the pixelLocations array. We do want this to be an array,
// instead of a HashMap, to keep draw() as fast as it can be.
if (pixelLocations == null) {
pixelLocations = new int[index + 1];
} else if (index >= pixelLocations.length) {
pixelLocations = Arrays.copyOf(pixelLocations, index + 1);
}
pixelLocations[index] = x + width * y;
}
// Set the location of several LEDs arranged in a strip.
// Angle is in radians, measured clockwise from +X.
// (x,y) is the center of the strip.
void ledStrip(int index, int count, float x, float y, float spacing, float angle, boolean reversed)
{
float s = sin(angle);
float c = cos(angle);
for (int i = 0; i < count; i++) {
led(reversed ? (index + count - 1 - i) : (index + i),
(int)(x + (i - (count-1)/2.0) * spacing * c + 0.5),
(int)(y + (i - (count-1)/2.0) * spacing * s + 0.5));
}
}
// Set the location of several LEDs arranged in a grid. The first strip is
// at 'angle', measured in radians clockwise from +X.
// (x,y) is the center of the grid.
void ledGrid(int index, int stripLength, int numStrips, float x, float y,
float ledSpacing, float stripSpacing, float angle, boolean zigzag)
{
float s = sin(angle + HALF_PI);
float c = cos(angle + HALF_PI);
for (int i = 0; i < numStrips; i++) {
ledStrip(index + stripLength * i, stripLength,
x + (i - (numStrips-1)/2.0) * stripSpacing * c,
y + (i - (numStrips-1)/2.0) * stripSpacing * s, ledSpacing,
angle, zigzag && (i % 2) == 1);
}
}
// Set the location of 64 LEDs arranged in a uniform 8x8 zig-zag grid.
// (x,y) is the center of the grid.
void ledGrid8x8(int index, float x, float y, float spacing, float angle)
{
ledGrid(index, 8, 8, x, y, spacing, spacing, angle, true);
}
// Automatically called at the end of each draw()
void draw()
{
if (pixelLocations == null) {
// No pixels defined yet
return;
}
if (client == null || !client.active()) {
// Try to (re)connect
client = new Client(app, host, port);
return;
}
int numPixels = pixelLocations.length;
int numBytes = 3 * numPixels;
int packetLen = 4 + numBytes;
if (packetData == null || packetData.length != packetLen) {
// Set up our packet buffer
packetData = new byte[packetLen];
packetData[0] = 0; // Channel
packetData[1] = 0; // Command (Set pixel colors)
packetData[2] = (byte)(numBytes >> 8);
packetData[3] = (byte)(numBytes & 0xFF);
}
loadPixels();
int ledAddress = 4;
for (int i = 0; i < numPixels; i++) {
int pixelLocation = pixelLocations[i];
int pixel = pixels[pixelLocation];
packetData[ledAddress] = (byte)(pixel >> 16);
packetData[ledAddress + 1] = (byte)(pixel >> 8);
packetData[ledAddress + 2] = (byte)pixel;
ledAddress += 3;
if (enableShowLocations) {
pixels[pixelLocation] = 0xFFFFFF ^ pixel;
}
}
client.write(packetData);
if (enableShowLocations) {
updatePixels();
}
}
}
examples/Processing/dot/data/dot.png

4.41 KiB

OPC opc;
PImage dot;
void setup()
{
size(640, 360);
dot = loadImage("dot.png");
// Connect to the local instance of fcserver. You can change this line to connect to another computer's fcserver
opc = new OPC(this, "127.0.0.1", 7890);
// Map an 8x8 grid of LEDs to the center of the window, scaled to take up most of the space
opc.ledGrid8x8(0, width/2, height/2, height/14, 0);
// Put two more 8x8 grids to the left and to the right of that one.
opc.ledGrid8x8(64, width/2 - height/14 * 8, height/2, height/14, 0);
opc.ledGrid8x8(128, width/2 + height/14 * 8, height/2, height/14, 0);
// Make the LED grid visible on-screen. By default, the LED sampling locations
// are hidden and don't affect Processing's output.
opc.showLocations(true);
}
void draw()
{
background(0);
// Change the dot size as a function of time, to make it "throb"
float dotSize = height * 0.6 * (1.0 + 0.2 * sin(millis() * 0.01));
// Draw it centered at the mouse location
image(dot, mouseX - dotSize/2, mouseY - dotSize/2, dotSize, dotSize);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment