import processing.core.*; 
import processing.xml.*; 

import processing.serial.*; 

import java.applet.*; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.event.MouseEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.FocusEvent; 
import java.awt.Image; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class Sonar_com0 extends PApplet {



// USER DEFINED size of the scanner (width of the window)
int scannerSize = 800;
int scannerLineWidth = 4; // 1/100? of size for example
int scannerDotRadius = 2; // 1/200? of size for example

// Computed radius (dependant of the size)
int radius = scannerSize / 2 - 20;

/** Variables **/

int theta = 0;   // current value of the detection angle
int sens = 1;    // current value of the scanner direction 
int value = 0;   // Distance cible
int angle = 0;   // Angle cible
// Store last detection values
int[] detection = new int[181];

// Store previous detection values
int[] previousdet = new int[181];

// The serial port:
Serial port;   
public void setup()
{  
  size(scannerSize,scannerSize/2);
  smooth();
  strokeWeight(scannerLineWidth);
  println("Available serial ports:");
  println(Serial.list()); 
  port = new Serial(this, Serial.list()[0], 38400); //----------------
}
// Convert 10 bits analog value into value on the scanner radius
public int getDistance(int val) {
  return (int)(val / 1023.0f * radius) - 1;
}

// Serial reception
public void receiveDistance()      //----------------------------
{
  while (port.available() >= 5)
  {
    if (port.read() == 0xff)
    {
      angle = (port.read() << 8) | (port.read());    
      value = (port.read() << 8) | (port.read());
      sens = (port.read() == 0x00?0:1);
      detection[angle] = getDistance(1024-value);
      theta = angle;
    }  
    
    port.clear();   //------------------------------------
  }         
}

//-----------------------------------------------------------------------
public void draw() 
{ 
  fill(250);
  text("+60\u00b0",width-width/15,height-height/2);
  text("-60\u00b0",width/20,height-height/2);
  text("+45\u00b0",width-width/6,height/4);
  text("-45\u00b0",width/7,height/4);
  text("+30\u00b0",width-width/3,height/20);
  text("-30\u00b0",width/3,height/20);
  text("0",width/2,height/40);
  if(value>920) 
  {
     text("BUT",width/2-10,height/2);
  }
  translate(width/2, height-10);    // retournement de 180\u00b0
  rotate(-3.14f);
  // Rectangle noir
  fill(0, 10);
  rect(-width/2, -10, width, height);
  // Centre du scanner
  fill(200); //
  ellipse(0,0,10,10);

  // Convert polar to cartesian
  float x = radius * cos(radians(theta));
  float y = radius * sin(radians(theta));

  // Affichage de la ligne de d?tection courante
  stroke(250,100,0);
  line(0,0,x,y);

  // Reception sur liaison s?rie
  receiveDistance();
  // Affichage des distances recues
  noStroke();
  for (int i=0; i<180; i++)
  {    
    if (detection[i] != -1 && detection[i] != 0)
    {
      fill(calculeIntense(i));
      float dx = detection[i]*cos(radians(i));
      float dy = detection[i]*sin(radians(i));   
      ellipse(dx,dy,scannerDotRadius,scannerDotRadius);
    }
  }
}
// Retour au noir apr\u00e9s d\u00e9tection d'une cible
public int calculeIntense(int i)  
{
  if (sens == 1)
  {  
    //
    if (i < theta+20 && i > theta - 60)
    {
      return floor((60 - theta + i)/60.0f*255.0f);      
    }
  } 
  else {
    //
    if (i > theta-20 && i < theta + 60)
    {
      return floor((60 - i + theta)/60.0f*255.0f);
    }     
  } 
  return 0;
}




  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#D4D0C8", "Sonar_com0" });
  }
}
