Skip to content

Instantly share code, notes, and snippets.

@heuermh
Created June 10, 2010 22:15
Show Gist options
  • Save heuermh/433718 to your computer and use it in GitHub Desktop.
Save heuermh/433718 to your computer and use it in GitHub Desktop.
import oscP5.*;
import netP5.*;
// monome emulator size
static final int W = 32;
static final int H = 16;
// button size
static final int S = 16;
// led color
static final int LED[][] = new int[W][H];
// default colors, greyscale 128 style
//final int frame = color(24, 27, 30);
//final int steel = color(185, 193, 196);
//final int ledOff = color(129, 137, 137);
//final int ledOn = color(253, 252, 252);
//final int highlight = color(0, 0, 80);
// alternate colors, 512 style
final int frame = color(56, 36, 29);
final int steel = color(126, 122, 121);
final int ledOff = color(100, 94, 98);
final int ledOn = color(245, 124, 77);
final int highlight = color(80, 0, 80);
// cursor
int cursorX = 0;
int cursorY = 0;
// open sound control (OSC) communication
OscP5 oscP5;
String host = "127.0.0.1";
int sendPort = 9090;
int receivePort = 9091;
void setup()
{
size(W * S + 5*S + 2*W, H * S + 5*S + 2*H);
smooth();
background(0);
clear();
oscP5 = new OscP5(this, host, sendPort, receivePort, "oscEvent");
oscP5.plug(this, "led", "/40h/led");
}
void draw()
{
fill(0);
noStroke();
rect(0, 0, width, height);
fill(steel);
stroke(frame);
strokeWeight(S);
roundRect(S, S, width - 2*S, height - 2*S, S*2);
noStroke();
for (int x = 0; x < W; x++)
{
for (int y = 0; y < H; y++)
{
fill(LED[x][y]);
roundRect(2.5*S + x * (S + 2), 2.5*S + y * (S + 2), S, S, 2);
}
}
noFill();
stroke(highlight);
strokeWeight(1);
roundRect(2.5*S + cursorX * (S + 2), 2.5*S + cursorY * (S + 2), S, S, 2);
}
void clear()
{
for (int x = 0; x < W; x++)
{
for (int y = 0; y < H; y++)
{
ledOff(x, y);
}
}
}
void led(final int x, final int y, final int state)
{
if (state == 1)
{
ledOn(x, y);
}
else
{
ledOff(x, y);
}
}
void ledOn(final int x, final int y)
{
ledOn(x, y, ledOn);
}
void ledOff(final int x, final int y)
{
ledOff(x, y, ledOff);
}
void ledOn(final int x, final int y, final int c)
{
LED[x][y] = c;
}
void ledOff(final int x, final int y, final int c)
{
LED[x][y] = c;
}
void buttonPressed(final int x, final int y)
{
OscMessage message = new OscMessage("/40h/press");
message.add(x);
message.add(y);
message.add(1);
oscP5.send(message);
}
void buttonReleased(final int x, final int y)
{
OscMessage message = new OscMessage("/40h/press");
message.add(x);
message.add(y);
message.add(0);
oscP5.send(message);
}
void keyPressed()
{
switch (keyCode)
{
case UP:
cursorY--;
break;
case DOWN:
cursorY++;
break;
case LEFT:
cursorX--;
break;
case RIGHT:
cursorX++;
break;
case CONTROL:
case KeyEvent.VK_SPACE:
buttonPressed(cursorX, cursorY);
buttonReleased(cursorX, cursorY);
break;
default:
break;
}
cursorX = constrain(cursorX, 0, W - 1);
cursorY = constrain(cursorY, 0, H - 1);
}
void mouseMoved()
{
int i = 0;
float x = 2.5*S;
while (x < mouseX)
{
x += S + 2;
i++;
}
int j = 0;
float y = 2.5*S;
while (y < mouseY)
{
y += S + 2;
j++;
}
cursorX = constrain(i - 1, 0, W - 1);
cursorY = constrain(j - 1, 0, H - 1);
}
void mousePressed()
{
buttonPressed(cursorX, cursorY);
}
void mouseReleased()
{
buttonReleased(cursorX, cursorY);
}
void oscEvent(final OscMessage message)
{
print("### received an osc message.");
print(" addrpattern: "+message.addrPattern());
println(" typetag: "+message.typetag());
}
// see http://code.google.com/p/processing/issues/detail?id=265
private float prevX;
private float prevY;
private void roundRect(final int x, final int y, final int w, final int h, final int r)
{
beginShape();
vertex(x+r, y);
vertex(x+w-r, y);
prevX = x+w-r;
prevY = y;
quadraticBezierVertex(x+w, y, x+w, y+r);
vertex(x+w, y+h-r);
prevX = x+w;
prevY = y+h-r;
quadraticBezierVertex(x+w, y+h, x+w-r, y+h);
vertex(x+r, y+h);
prevX = x+r;
prevY = y+h;
quadraticBezierVertex(x, y+h, x, y+h-r);
vertex(x, y+r);
prevX = x;
prevY = y+r;
quadraticBezierVertex(x, y, x+r, y);
endShape();
}
private void quadraticBezierVertex(final int cpx, final int cpy, final int x, final int y)
{
float cp1x = prevX + 2.0/3.0*(cpx - prevX);
float cp1y = prevY + 2.0/3.0*(cpy - prevY);
float cp2x = cp1x + (x - prevX)/3.0;
float cp2y = cp1y + (y - prevY)/3.0;
bezierVertex(cp1x, cp1y, cp2x, cp2y, x, y);
}
private void roundRect(final float x, final float y, final float w, final float h, final float r)
{
beginShape();
vertex(x+r, y);
vertex(x+w-r, y);
prevX = x+w-r;
prevY = y;
quadraticBezierVertex(x+w, y, x+w, y+r);
vertex(x+w, y+h-r);
prevX = x+w;
prevY = y+h-r;
quadraticBezierVertex(x+w, y+h, x+w-r, y+h);
vertex(x+r, y+h);
prevX = x+r;
prevY = y+h;
quadraticBezierVertex(x, y+h, x, y+h-r);
vertex(x, y+r);
prevX = x;
prevY = y+r;
quadraticBezierVertex(x, y, x+r, y);
endShape();
}
private void quadraticBezierVertex(final float cpx, final float cpy, final float x, final float y)
{
float cp1x = prevX + 2.0/3.0*(cpx - prevX);
float cp1y = prevY + 2.0/3.0*(cpy - prevY);
float cp2x = cp1x + (x - prevX)/3.0;
float cp2y = cp1y + (y - prevY)/3.0;
bezierVertex(cp1x, cp1y, cp2x, cp2y, x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment