/*
Lab4D OSC Router - lab4d.com
Captures OSC Messages and sends them back to a new desired IP and port.
The messages are sent to various ports to help network speed.
You must have Processing 1.5.1 and the OSCP5 library.
Current setup is to work with MindYourOSC for the Emotiv EPOC.
For more information conact me at www.lab4d.com
*/


import oscP5.*;
import netP5.*;

String lastInput = new String();
String currentInput = new String();

public String OutIP = ("127.0.0.1");  // Default Out IP
public int InPort = 7400;             // Input Port

//Router Messages
public int Port1 = 10000;
String OSCIn1 = ("/EXP/CLENCH");
String OSCOut1 = ("/EXP/Clench");
String OSCIn2 = ("/EXP/SMILE");
String OSCOut2 = ("/EXP/Smile");
String OSCIn3 = ("/EXP/BLINK");
String OSCOut3 = ("/EXP/Blink");
String OSCIn4 = ("/EXP/WINK_LEFT");
String OSCOut4 = ("/EXP/Wink_Left");
String OSCIn5 = ("/EXP/WINK_RIGHT");
String OSCOut5 = ("/EXP/Wink_Right");
String OSCIn6 = ("/EXP/EYEBROW");
String OSCOut6 = ("/EXP/Eyebrow");

public int Port2 = 11000;
String OSCIn7 = ("/COG/LIFT");
String OSCOut7 = ("/COG/Lift");
String OSCIn8 = ("/COG/DROP");
String OSCOut8 = ("/COG/Drop");
String OSCIn9 = ("/COG/LEFT");
String OSCOut9 = ("/COG/Left");
String OSCIn10 = ("/COG/RIGHT");
String OSCOut10 = ("/COG/Right");
String OSCIn11 = ("/COG/ROTATE_LEFT");
String OSCOut11 = ("/COG/Rotate_Left");
String OSCIn12 = ("/COG/ROTATE_RIGHT");
String OSCOut12 = ("/COG/Rotate_Right");

public int Port3 = 12000;
String OSCIn13 = ("/AFF/Engaged/Bored");
String OSCOut13 = ("/AFF/Engaged/Bored");
String OSCIn14 = ("/AFF/Frustration");
String OSCOut14 = ("/AFF/Frustration");
String OSCIn15 = ("/AFF/Excitement");
String OSCOut15 = ("/AFF/Excitement");
String OSCIn16 = ("/AFF/Meditation");
String OSCOut16 = ("/AFF/Meditation");
String OSCIn17 = ("/AFF/Excitement Long Term");
String OSCOut17 = ("/AFF/Excitement Long Term");



public float M1 = 0;
public float M2 = 0;
public float M3 = 0;
public float M4 = 0;
public float M5 = 0;
public float M6 = 0;

public float M7 = 0;
public float M8 = 0;
public float M9 = 0;
public float M10 = 0;
public float M11 = 0;
public float M12 = 0;

public float M13 = 0;
public float M14 = 0;
public float M15 = 0;
public float M16 = 0;
public float M17 = 0;

int FP = 20;   // Font Position
int FS = 12;   // Font Size
int FF = 150;  // Font Fill Color
int GP = 170;  // Graph Start Position
int TP = 20;   // Title Position
int TFR = 150; // Title Fill Color RED
int TFG = 150; // Title Fill Color GREEN
int TFB = 0;   // Title Fill Color BLUE
int IFR = 200; // Information Fill Color RED
int IFG = 20;  // Information Fill Color GREEN
int IFB = 0;   // Inforation Fill Color BLUE

int OSCSend = 0;
PFont fontA;
PImage img;

OscP5 oscP5;


void setup() {
  size(540, 600);
  frameRate(30);
  smooth();
  
  oscP5 = new OscP5(this, InPort);

  
  fontA = loadFont("HelveticaNeueLTCom-Th.vlw");
  textFont(fontA, FS);
  
  img = loadImage("l4d.png");
// println("Sending to: "+OutIP); //Print on Console the Out IP
}

void draw() {
  background(0);
  

fill(255, 0, 0);
text(currentInput, width/4.1, height*.85);
  
 image(img, 410, 540, img.width/6, img.height/6);

  // draw First pack of graph ticks
  int i;
  for (i = 1; i <= 11; i++) {
    stroke(map(i, 1, 11, 0, 255));
    float tickX = map(i, 1, 11, 135, 493);
    line(tickX, 40, tickX, 170);
  }
  //draw Second pack of graph ticks   
  int b;
  for (b = 1; b <= 11; b++) {
    stroke(map(b, 1, 11, 0, 255));
    float tickX = map(b, 1, 11, 135, 493);
    line(tickX, 200, tickX, 330);
  }
  //draw Third pack of graph ticks
  int c;
  for (c = 1; c <= 11; c++) {
    stroke(map(c, 1, 11, 0, 255));
    float tickX = map(c, 1, 11, 135, 493);
    line(tickX, 360, tickX, 470);
  }
 
  noStroke();
  
  //Draw Titles
  fill(TFR, TFG, TFB);
  text("Messages to port "+Port1, TP, 30);
  fill(TFR, TFG, TFB);
  text("Messages to port "+Port2, TP, 190);
  fill(TFR, TFG, TFB);
  text("Messages to port "+Port3, TP, 350);
  
  
  // Draw Information
  fill(IFR, IFG, IFB);
  text("Receiving local on port "+InPort, 20, 540);
  fill(IFR, IFG, IFB);
  text("Sending to " +OutIP+" on ports "+Port1+" - "+Port2+" - "+Port3, 20, 560);
  fill(IFR, IFG, IFB);
  text("Type in this window and hit enter to change Out IP", 20, 580);
  
  // draw First pack of bar graphs
  fill(FF);
  text(OSCOut1, FP, 60);  
  drawEXPBarGraph(M1, 50);
  fill(FF);
  text(OSCOut2, FP, 80);
  drawEXPBarGraph(M2, 70);
  fill(FF);
  text(OSCOut3, FP, 100);
  drawEXPBarGraph(M3, 90);
  fill(FF);
  text(OSCOut4, FP, 120);
  drawEXPBarGraph(M4, 110);
  fill(FF);
  text(OSCOut5, FP, 140);
  drawEXPBarGraph(M5, 130);
  fill(FF);
  text(OSCOut6, FP, 160);
  drawEXPBarGraph(M6, 150);
  
  // draw Second pack of bar graphs
  fill(FF);
  text(OSCOut7, FP, 210);  
  drawEXPBarGraph(M7, 200);
  fill(FF);
  text(OSCOut8, FP, 230);
  drawEXPBarGraph(M8, 220);
  fill(FF);
  text(OSCOut9, FP, 250);
  drawEXPBarGraph(M9, 240);
  fill(FF);
  text(OSCOut10, FP, 270);
  drawEXPBarGraph(M10, 260);
  fill(FF);
  text(OSCOut11, FP, 290);
  drawEXPBarGraph(M11, 280);
  fill(FF);
  text(OSCOut12, FP, 310);
  drawEXPBarGraph(M12, 300);
  
  // draw Third pack of bar graphs
  fill(FF);
  text(OSCOut13, FP, 380);  
  drawEXPBarGraph(M13, 370);
  fill(FF);
  text(OSCOut14, FP, 400);
  drawEXPBarGraph(M14, 390);
  fill(FF);
  text(OSCOut15, FP, 420);
  drawEXPBarGraph(M15, 410);
  fill(FF);
  text(OSCOut16, FP, 440);
  drawEXPBarGraph(M16, 430);
  fill(FF);
  text(OSCOut17, FP, 460);
  drawEXPBarGraph(M17, 450);
  
}

//Input IP manually

void keyPressed() {
if(key == ENTER)
{
  OutIP = currentInput;
  lastInput = currentInput = currentInput + key;
  currentInput = "";
  
}
else if(key == BACKSPACE && currentInput.length() > 0)
{
  currentInput = currentInput.substring(0, currentInput.length() - 1);
}
else if(key == CODED) {  // ignore Special Keys...
}
else
{
  currentInput = currentInput + key;
}

}


// Draw Bar Graph function

void drawEXPBarGraph(float expVal, int barY) {
  if(expVal <= 0.3) {
    fill(color(225, 10, 10));
  } 
  else if (expVal > 0.3 & expVal < 0.7 ){
    fill(color(255, 160, 0));
  }
    else if (expVal >= 0.7 ){
    fill(color(22, 255, 30));
    }
  
  float len = map(expVal, 0.0, 1.0, 0, 323);
  rect(GP, barY, len, 10);
  } 


void drawOSCSend() {
  
}

void oscEvent(OscMessage theOscMessage) {
  
int OSCSend = 1;
  
  // PACK 1 OSC ROUTER
  
  if(theOscMessage.checkAddrPattern(OSCIn1) == true) {
    NetAddress P1 = new NetAddress(OutIP,Port1);
  M1 = theOscMessage.get(0).floatValue();
    OscMessage M1Message = new OscMessage(OSCOut1);
    M1Message.add(M1);
    oscP5.send(M1Message, P1);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn2) == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  M2 = theOscMessage.get(0).floatValue();
    OscMessage M2Message = new OscMessage(OSCOut2);
    M2Message.add(M2);
    oscP5.send(M2Message, P1); 
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn3) == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  M3 = theOscMessage.get(0).floatValue();
    OscMessage M3Message = new OscMessage(OSCOut3);
    M3Message.add(M3);
    oscP5.send(M3Message, P1);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn4) == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  M4 = theOscMessage.get(0).floatValue();
    OscMessage M4Message = new OscMessage(OSCOut4);
    M4Message.add(M4);
    oscP5.send(M4Message, P1);
  }
      
  if(theOscMessage.checkAddrPattern(OSCIn5) == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  M5 = theOscMessage.get(0).floatValue();
    OscMessage M5Message = new OscMessage(OSCOut5);
    M5Message.add(M5);
    oscP5.send(M5Message, P1);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn6) == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  M6 = theOscMessage.get(0).floatValue();
    OscMessage M6Message = new OscMessage(OSCOut6);
    M6Message.add(M6);
    oscP5.send(M6Message, P1);
  }
  
  
  // PACK 2 OSC  ROUTER
  
  if(theOscMessage.checkAddrPattern(OSCIn7) == true) {
    NetAddress P2 = new NetAddress(OutIP, Port2);
  M7 = theOscMessage.get(0).floatValue();
    OscMessage M7Message = new OscMessage(OSCOut7);
    M7Message.add(M7);
    oscP5.send(M7Message, P2);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn8) == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  M8 = theOscMessage.get(0).floatValue();
    OscMessage M8Message = new OscMessage(OSCOut8);
    M8Message.add(M8);
    oscP5.send(M8Message, P2);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn9) == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  M9 = theOscMessage.get(0).floatValue();
    OscMessage M9Message = new OscMessage(OSCOut9);
    M9Message.add(M9);
    oscP5.send(M9Message, P2);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn10) == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  M10 = theOscMessage.get(0).floatValue();
    OscMessage M10Message = new OscMessage(OSCOut10);
    M10Message.add(M10);
    oscP5.send(M10Message, P2);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn11) == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  M11 = theOscMessage.get(0).floatValue();
    OscMessage M11Message = new OscMessage(OSCOut11);
    M11Message.add(M11);
    oscP5.send(M11Message, P2);
  }
   
  if(theOscMessage.checkAddrPattern(OSCIn12) == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  M12 = theOscMessage.get(0).floatValue();
    OscMessage M12Message = new OscMessage(OSCOut12);
    M12Message.add(M12);
    oscP5.send(M12Message, P2);
  }
  
  // PACK 3 OSC ROUTER
  
  if(theOscMessage.checkAddrPattern(OSCIn13) == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  M13 = theOscMessage.get(0).floatValue();
    OscMessage M13Message = new OscMessage(OSCOut13);
    M13Message.add(M13);
    oscP5.send(M13Message, P3);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn14) == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  M14 = theOscMessage.get(0).floatValue();
    OscMessage M14Message = new OscMessage(OSCOut14);
    M14Message.add(M14);
    oscP5.send(M14Message, P3);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn15) == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  M15 = theOscMessage.get(0).floatValue();
    OscMessage M15Message = new OscMessage(OSCOut15);
    M15Message.add(M15);
    oscP5.send(M15Message, P3);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn16) == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  M16 = theOscMessage.get(0).floatValue();
    OscMessage M16Message = new OscMessage(OSCOut16);
    M16Message.add(M16);
    oscP5.send(M16Message, P3);
  }
  
  if(theOscMessage.checkAddrPattern(OSCIn17) == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  M17 = theOscMessage.get(0).floatValue();
    OscMessage M17Message = new OscMessage(OSCOut17);
    M17Message.add(M17);
    oscP5.send(M17Message, P3);
  }
  
  
  
 } // oscEvent Function End

