import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Move {
static SerialComm sc;
static RobotCamColorTrack prueba;
static OutputStream out;
public static void main(String[] args) throws Exception {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier( "COM3d" );
SerialPort sp = (SerialPort)portId.open("SerialCommunicationChannel", 2000);
sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
out = sp.getOutputStream();
DataInputStream i=new DataInputStream(System.in);
while(true) {
int direccion;
byte[] b = new byte[1];
b[0] = i.readByte();
switch(b[0]) {
case (byte)'w': direccion = RobotCamColorTrack.MOVER_ADELANTE; break;
case (byte)'a': direccion = RobotCamColorTrack.MOVER_IZQUIERDA; break;
case (byte)'s': direccion = RobotCamColorTrack.MOVER_ATRAS; break;
case (byte)'d': direccion = RobotCamColorTrack.MOVER_DERECHA; break;
default: direccion = -1;
}
if(direccion!=-1) moverRobot(direccion,(direccion != RobotCamColorTrack.MOVER_ADELANTE &&
direccion != RobotCamColorTrack.MOVER_ATRAS
) ? '2' : '5');
}
// out.close();
// sp.close();
}
private static void moverRobot(int direccion, char intensidad) throws IOException {
byte salida[] = new byte[3];
switch(direccion) {
case RobotCamColorTrack.MOVER_IZQUIERDA: salida[0] = 'L'; break;
case RobotCamColorTrack.MOVER_DERECHA: salida[0] = 'R'; break;
case RobotCamColorTrack.MOVER_ATRAS: salida[0] = 'B'; break;
case RobotCamColorTrack.MOVER_ADELANTE: salida[0] = 'F'; break;
case RobotCamColorTrack.NO_MOVE: return;
default: return;
}
salida[1] = (byte) intensidad;
salida[2] = '\r';
out.write(salida);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}