import processing.serial.*;
//Needed to be able to use the special serial communication functions

//////VARIABLES////////////////////////////////////////////////////////////////////////////////////////////////  
int xpos; //x position of the ellipse
int ypos;
int ediam = 10; // diameter of the ellipse

int R,G,B;

Serial port;                         // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
//int xpos, ypos;              // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller

int mansize = 10;
int manposx;
int manposy;
int manbh = mansize*5; //man body width
int manbw = mansize*3; //man body height
int armwidth = 4;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void setup()
{
  size(600, 600);
  background(0);
  smooth();

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  port = new Serial(this, Serial.list()[2], 9600);
  port.write(65);    // Send a capital A to start the microcontroller sending

}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void draw() 
{ 
  
  //ediam = ediam/4;
  
  R = ediam/4;
  G = (ediam/4)^2;
  B = (ediam/4)-100;
  
  G = constrain(G,0,255);
  B = constrain(B,0,255);
  
  println("mX: " + manposx + " my: " + manposy + " diam: " + ediam);  // debug line
  background(0);

  // If no serial data has beeen received, send again until we get some.
  // (in case you tend to start Processing before you start your 
  // external device): 
/*  if (firstContact == false) {
    println("nothing");
    delay(300);
  }
*/
ediam=ediam;
 //manposx = ediam;
 //manposy = ediam;
  smooth();
  background (0);
  noStroke();
  rectMode(CENTER);
  fill(R,G,B);
  rect(manposx,manposy-30,mansize,mansize);
  rect(manposx,manposy,manbw,manbh);
  rectMode(CORNER);
  rect(manposx-15,manposy+25,mansize-3,manbh);
  rect(manposx+8,manposy+25,mansize-3,manbh);
  stroke(R,G,B);
  strokeWeight(armwidth);
  line(manposx-(manbw/2),manposy-(manbh/2)+armwidth/2,manposx-(manbh/2)-20,manposy-(manbw/2)+40);
  line(manposx+(manbw/2),manposy-(manbh/2)+armwidth/2,manposx+(manbh/2)+20,manposy-(manbw/2)+40);

}



/*
//////////FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////// 
void variableEllipse(int x, int y, int ed) {
  ellipseMode(CENTER);
  noStroke();
  fill (150, 15, 0);
  ellipse(x, y, ed , ed);
}
*/

//////////SERIAL FUNCTION//////////////////////////////////////////////////////////////////////////////////////////// 
void serialEvent(Serial port) {
  String stringFromArduino = port.readStringUntil(9);  // read until the end, e.g. "9" or a tab character
  if (stringFromArduino != null) {
    //println("String from Arduino: " + stringFromArduino);  // DEBUG
    String[] parts = stringFromArduino.split(",");  // We know it's a comma because we sent "44" from Arduino.
    //println(parts.length);
    // assign our variables
    manposy = int(parts[0]);  // we know this is first because we sent it first.
    manposx = int(parts[1]);  // int() casts the string chunk into a integer type.
    ediam = int(parts[2].trim());  // use .trim() to get rid of the tab character.
    // readStringUntil apparently includes what you're reading until (in this case, the tab character)
    
    port.write(65);  // tell Arduino we're ready for more.

  }
}
