RCMain
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.OutputStream;
 
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.Sound;
 
import java.io.IOException;
 
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
 
/**
 * Enables remote control of a pilot object using Bluetooth. Communicates with
 * RemotePilotControl running on PC or other device Receives commands from the
 * controller and calls the corresponding methods on the pilot; when the method
 * returns, sends a reply to the controller.
 * 
 * You can run the PilotParams sample to create a property file which sets the
 * parameters of the Pilot to the dimensions and motor connections for your
 * robot.
 * 
 * @author Roger Glassey
 */
public class RCMain implements ButtonListener {
    public final int FORWARD = 0;
    public final int BACKWARD = 1;
    public final int STOP = 2;
    public final int LEFT = 3;
    public final int RIGHT = 4;
    public final int REPORT = 5;
    public final int DIE = 6;
    public final int ATTACK = 7;
    public final int PLAY = 8;
    public final int LAUGH = 9;
    public final int PSYCHO = 10;
 
    private boolean running = true;
 
    /**
     * connect and wait for orders from the controller
     */
    public void go() {
        Sound.beepSequence();
        connect();
        while (running) {
            readData();
            executeCommand();
            // report();
        }
    }
 
    /**
     * translates the received message and calls the corresponding pilot method
     */
    protected void executeCommand() {
        if (_code == FORWARD)
            Mover.forward();
        else if (_code == BACKWARD)
            Mover.backward();
        else if (_code == LEFT)
            Mover.left();
        else if (_code == RIGHT)
            Mover.right();
        else if (_code == STOP)
            Mover.stop();
        else if (_code == ATTACK)
            Mover.attack();
        else if (_code == PLAY)
            Sound.playSample(new File("imp.wav"));
        else if (_code == LAUGH)
            Sound.playSample(new File("laugh.wav"));
        else if (_code == PSYCHO)
            Sound.playSample(new File("psycho.wav"));
        else if (_code == DIE) {
            Mover.quit();
            Sound.playSample(new File("laugh.wav"));
            System.exit(0);
        }
    }
 
    /**
     * decodes incoming messages and stores in instance variables for use by
     * executeCommand
     */
    protected void readData() {
        try {
            _code = dataIn.readInt();
 
        } catch (IOException e) {
        }
        if (_code != REPORT)
            System.out.println("code " + _code);
    }
 
    /**
     * Estabish bluetooth connection to mission control
     */
    protected void connect() {
        BTConnection connection;
        boolean fail = false;
        LCD.clear();
        LCD.drawString("Waiting", 0, 0);
        connection = Bluetooth.waitForConnection(); // this method is very
                                                    // patient.
        LCD.clear();
        try {
            byte[] hello = new byte[32];
            int len = connection.read(hello, hello.length);
            if (len != 3 || hello[0] != 'R' || hello[1] != 'C'
                    || (hello[2] != 'P')) {
                fail = true;
                LCD.drawString("Console no h/s    ", 0, 0);
                connection.close();
                return;
            } else {
                os = connection.openOutputStream();
                os.write(hello);
                os.flush();
            }
        } catch (Exception e) {
            LCD.drawString("connection error " + e.getMessage(), 0, 0);
        }
        if (!fail) {
            LCD.drawString("Connected", 0, 0);
            dataIn = connection.openDataInputStream();
            dataOut = connection.openDataOutputStream();
            Sound.playSample(new File("imp.wav"));
        }
    }
 
    /**
     * @param args
     *            the command line arguments
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        RCMain predator = new RCMain();
        Button.ESCAPE.addButtonListener(predator);
        predator.go();
    }
 
    protected DataInputStream dataIn;
    protected DataOutputStream dataOut;
    protected OutputStream os;
    protected int _code; // set by readData() used by executeCommand
 
    @Override
    public void buttonPressed(Button b) {
    }
 
    @Override
    public void buttonReleased(Button b) {
        if (b.equals(Button.ESCAPE)) {
            running = false;
            System.exit(0);
        }
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License