import processing.core.*; import processing.serial.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class ddrpad_quickhack extends PApplet {// Program draws a black square in the middle of the screen. 
// It reacts to multiple key-presses, growing.

// Includes



// Constants

int MININTERVAL = 100;
float MULTIPLIER = 0.00004f;
float FORCE_MULTIPLIER = 90;
float MINSIZE = 100.f;
float MAXSIZE = 640.f;
int XSIZE = 768;
int YSIZE = 1024;
int CONTRACTPERIOD = 1;

int INCREMENT=10;

// Program Variables

int hits = 0;
int i=0;
int timelasthit = -2000;
int hitinterval = 2000;
float cursize = 100.f;
float lastsize = 100.f;
int inByte = 0;
Serial myPort;  // The serial port


// Program

public void setup() 
{ 

  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].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[1], 57600);


  //  size(200, 200); 
  size(XSIZE, YSIZE);
  rectMode(CENTER);
  noStroke();
  fill(0);
  background(255);

} 

public void draw() 
{ 
  float subtractor;

  subtractor = (float)Reciprocal_Logarithmic_model(cursize);
  cursize = constrain(cursize-subtractor, MINSIZE, MAXSIZE);
//  println(" - " + cursize + " - " + subtractor);
  background(255);
  rect(XSIZE/2, YSIZE/2, cursize, cursize);
} 

public double Logarithmic_model(double x_in)
{
  //log model from zunzun.com
  //fit to data points
  //  100, 40
  //  2000, 1
   
  double temp;
  temp = 0.0f;
  
  // coefficients
  double a = 9.9952499373538743E+01f;
  double b = -1.3018519827118025E+01f;

  temp = a + b*Math.log(x_in);
  print (" - " + temp);
  return temp;
}

public double Reciprocal_Logarithmic_model(double x_in)
{
  //reciprocal log model from zunzun.com
  //fit to data points
  //  100, 0
  //  640, 40

  double temp;
  temp = 0.0f;

  // coefficients
//  double a = 2.7716949837720617E-01;
//  double b = -3.8471867754609625E-02;
  double a = 3.4188147348130071E+00f;
  double b = -5.2523894603906762E-01f;


  temp = 1.0f /(a + b*Math.log(x_in)) + - 10;
  return temp;
}

public void serialEvent(Serial myPort) {
  float increment;
  inByte = myPort.read();
  print(inByte);
  //cursize += inByte * FORCE_MULTIPLIER;
  
  hits++;
  int timenow = millis();
  hitinterval = (timenow-timelasthit);
  print(hitinterval);
  timelasthit = timenow;
  
  if (hitinterval < 1) hitinterval = 1;
  
  lastsize = cursize;
  increment = (float)Logarithmic_model(hitinterval);
  if(increment>40)increment=40.f;
  cursize = constrain(cursize + increment, MINSIZE, MAXSIZE);
  println(" - " + cursize);

}

/*
void mousePressed() 
{
  hits++;
  int timenow = millis();
  hitinterval = (timenow-timelasthit);
  print(hitinterval);
  timelasthit = timenow;
  
  if (hitinterval < 1) hitinterval = 1;
  
  lastsize = cursize;
  cursize = constrain(cursize + (float)Logarithmic_model(hitinterval), MINSIZE, MAXSIZE);
  println(" - " + cursize);
//  inByte=myPort.read();
//  cursize += inByte * FORCE_MULTIPLIER;
//  print(inByte + " - ");

  redraw();
} */
static public void main(String args[]) {   PApplet.main(new String[] { "ddrpad_quickhack" });}}