/******************** start of sendData.pde ********************/


public class SerialWrite implements Runnable {
	boolean flag = true;    //if false ,the thread will die
	public void run () {
		long oldTime = millis ();
		long currentTime;
		int x1 = timeLine.local_x;
		while (flag) {
			currentTime = millis ();                 
			if (serialOpen.isOpen () && !timeLine.isBeginMove && timeLine.local_x != x1) {
				cmdSend (timeLine.local_x);
				//println (currentTime);
				//oldTime = currentTime;
				x1 = timeLine.local_x;
			}
			delay (10);
			//delay (10);
		}
	}
}

//read the TimeLine value and get <Id Angle> then send to serial port
void cmdSend (float axisValue) {
	for (int i=0; i < servoList.list.size (); i++) {
		if (axisValue > (servoList.list.get(i)).getPx1() 
				&& axisValue < (servoList.list.get(i)).getPx2()) {
			float data = map (axisValue-(servoList.list.get(i)).getPx1(), 
					0, servoList.list.get(i).getWidth (), 
					servoList.list.get(i).getBeginAngle(), 
					servoList.list.get(i).getEndAngle());
			println (">"+servoList.list.get(i).getServoId ()+"  "+int (data));
			serialOpen.cmdSend (servoList.list.get(i).getServoId (), int (data));
			delay(3);
		}
	}
	serialOpen.myPort.write ('\r');
	println ("\r");
}

/******************** end of sendData.pde ********************/
