Skip to content

Instantly share code, notes, and snippets.

@sgallese
Created November 6, 2009 04:43
Show Gist options
  • Save sgallese/227714 to your computer and use it in GitHub Desktop.
Save sgallese/227714 to your computer and use it in GitHub Desktop.
import processing.core.*;
import processing.video.*;
import oscP5.*;
import netP5.*;
public class Showlights extends PApplet {
Squares[] towers;
int currClick = 0;
int currMouseX = 0;
int currMouseY = 0;
String coords = "";
Movie video;
OscP5 oscP5;
NetAddress myRemoteLocation;
public void setup(){
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
frameRate(3);
noFill();
fill(255);
video = new Movie(this, "radiolightsnosound.mov");
video.loop();
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,5555);
myRemoteLocation = new NetAddress("127.0.0.1",5555);
noStroke();
smooth();
towers = new Squares[7];
towers[0] = new Squares(64,166,23,93, 640, 480);
towers[1] = new Squares(309,154,28,122, 640, 480);
towers[2] = new Squares(375,149,19,127, 640, 480);
towers[3] = new Squares(409,185,18,88, 640, 480);
towers[4] = new Squares(440,171,18,104, 640, 480);
towers[5] = new Squares(459,151,31,123, 640, 480);
towers[6] = new Squares(518,131,28,130, 640, 480);
}
public void draw(){
if (video.available()) {
video.read();
image(video, 0, 0, width, height); // Draw the webcam video onto the screen
stroke(255,255,255);
noFill();
for(int i = 0; i < towers.length; i++){
rect(towers[i].get_x(), towers[i].get_y(), towers[i].get_width(), towers[i].get_height());
}
for(int i = 0; i < towers.length; i++){
searchForBright(towers[i]);
}
}
}
public void searchForBright(Squares tower) {
int[] towerPixels = tower.get_pixels();
boolean trippedSignal = false;
searchFor: for (int i = 0; i < towerPixels.length; i++) {
int pixelValue = video.pixels[towerPixels[i]];
//System.out.println(red(pixelValue));
if (red(pixelValue) > 0) {
fill(255, 204, 0, 175);
rect(tower.get_x(), tower.get_y(), tower.get_width(), tower.get_height());
tower.set_volume(150);
trippedSignal=true;
break searchFor;
}
}
if (!trippedSignal){
tower.set_volume(0);
}
OscMessage myMessage = new OscMessage( "processing" );
int maxVolume = 150;
// System.out.println("Life: "+closestDistanceLife+", Heart: "+closestDistanceHeart+", Head: "+closestDistanceHead+", Fate: "+closestDistanceFate);
myMessage.add(new int[] { 1, towers[0].get_volume(), 1, towers[1].get_volume(),1, towers[2].get_volume(), 1, towers[3].get_volume(), 2, towers[4].get_volume(), 2, towers[5].get_volume(), 2, towers[6].get_volume()});
/* send the message */
oscP5.send(myMessage, myRemoteLocation);
}
void movieEvent(Movie m) {
m.read();
}
public void mouseClicked(){
if (currClick == 0){
currMouseX = mouseX;
currMouseY = mouseY;
coords = "rect("+mouseX+","+mouseY+",";
currClick++;
}
else{
coords = coords + (mouseX-currMouseX)+","+(mouseY-currMouseY)+");";
System.out.println(coords);
currClick = 0;
}
}
}
public class Squares {
int _x;
int _y;
int _width;
int _height;
int[] _pixels;
int _imageHeight;
int _imageWidth;
int _startPixel;
int _volume;
public int get_volume() {
return _volume;
}
public void set_volume(int volume) {
_volume = volume;
}
public int get_x() {
return _x;
}
public int get_y() {
return _y;
}
public int get_width() {
return _width;
}
public int get_height() {
return _height;
}
public int[] get_pixels() {
return _pixels;
}
public int get_imageHeight() {
return _imageHeight;
}
public int get_imageWidth() {
return _imageWidth;
}
public int get_startPixel() {
return _startPixel;
}
public void print_pixels(){
for(int i = 0; i< _pixels.length; i++){
System.out.println(_pixels[i]);
}
}
public Squares(int x, int y, int width, int height, int imageHeight, int imageWidth){
_x = x;
_y = y;
_width = width;
_height = height;
_imageHeight = imageHeight;
_imageWidth = imageWidth;
_pixels = new int[_width*_height];
//Calculate start of square
_startPixel = (_x+((_y-1)*_imageWidth))-1;
//Calculate pixels of square
int currentPixel = 0;
for(int m = 0; m < _height; m++){
for(int i = 0; i < _width; i++){
_pixels[currentPixel] = (_startPixel+i)+(m*_imageWidth);
currentPixel++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment