
import java.lang.String;



public class SerProc extends Thread
{
	Serial port; 
	boolean running;
	int input;
	int last_input = 0;
	int difference = 100;
	boolean available;
	
	
	Speaker mySpeaker;
	
	SerProc() {
		for (int i = 0; i < Serial.list().length; i++)
		{System.out.println(Serial.list()[i]);}
		
		port = new Serial(null, Serial.list()[0], 9600);
		running = false;
		available = true;
		
	}
	public void start() {
		// mySpeaker = new Speaker();
		super.start();
		running = true;
	}
	
	
	public int getData() {
		available = false;
		return input;
	}
	
	public void run() {
		while (running) {
			if (port.available() > 0) {
				input = port.read();
						
				
				System.out.println(input);
		

				//serial data anaylyses */
				
				//find difference
				difference = last_input - input;
				
				if (input > 50 && !available){
					 System.out.print("goodbye");
					 mySpeaker.stop();
					 //mySerial.stop();
					 available = true;
				}
				  
				
				if (input < 20 && available){
				  System.out.print("hello there");
					available = false;
					mySpeaker= new Speaker();
					mySpeaker.start();
				}
				
				last_input = input;
			}
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	

	public void serialEvent(Serial b)
	{
//		println(b.read());
	}

	public void sendSerial(String _value){
		port.write(_value);
		
		/*port.write(_value.getBytes());
		System.out.println(_value);*/
		
	}
	public boolean available() {
		// TODO Auto-generated method stub
		return available;
	} 

}

