import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import lejos.hardware.Sound;
+import lejos.hardware.lcd.LCD;
import lejos.remote.nxt.BTConnector;
import lejos.remote.nxt.NXTConnection;
+import lejos.utility.Delay;
-public abstract class BTMemory extends Thread{
- private List<String> COLORSTOFIND = Arrays.asList(new String[]{"Blue", "Red", "Yellow"});
+public abstract class BTMemory extends Thread {
+ private List<String> COLORSTOFIND = Arrays.asList(new String[] { "Blue", "Red", "Yellow" });
private Set<String> colors;
protected NXTConnection connection;
- protected DataOutputStream dataOutput;
+ protected OutputStream dataOutput;
protected DataInputStream dataInput;
-
- public BTMemory(){
- colors = new HashSet<String>();
- setup();
+ protected static PrintWriter writer;
+
+ public BTMemory() {
+ colors = new HashSet<String>();
+ setup();
}
-
+
public abstract void setup();
-
- public void addColor(String c){
+
+ public void addColor(String c) {
colors.add(c);
}
-
- public boolean finished(){
+
+ public boolean finished() {
return colors.equals(new HashSet<String>(COLORSTOFIND));
}
-
+
@Override
- public void run(){
+ public void run() {
try {
- while(true){
- if(dataInput.available()>0){
+ while (true) {
+ if (dataInput.available() > 0) {
+ //String msg = dataInput.readLine();
LCDPrinter.print(Character.toChars(dataInput.read()).toString());
}
}
- } catch (IOException e) {
+ }
+ catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
+
+ public static void SendMessage(String message){
+ writer.println(message);
+ writer.flush();
+ }
- public static BTMemory getBTMemory(boolean master, final String rovername){
- if(master){
- return new BTMemory(){
+ public static BTMemory getBTMemory(boolean master, final String rovername) {
+ if (master) {
+ return new BTMemory() {
@Override
public void setup() {
+ //LCD.drawString("Create connection", 0, 0);
BTConnector btconnector = new BTConnector();
+ //LCD.drawString("BTConnector created", 0, 1);
connection = btconnector.connect(rovername, NXTConnection.RAW);
+ //LCD.drawString("Connection created", 0, 2);
dataInput = connection.openDataInputStream();
- dataOutput = connection.openDataOutputStream();
+ dataOutput = connection.openOutputStream();
+ writer = new PrintWriter(dataOutput);
+ //LCD.drawString("Input output created", 0, 3);
try {
- dataOutput.write(77);
- } catch (IOException e) {
+ //dataOutput.write(77);
+ writer.println("Hellow");
+ writer.flush();
+ //LCD.clear();
+ //LCD.drawString("Message sent", 0, 4);
+ } catch (Exception e) {
e.printStackTrace();
}
}
};
} else {
- return new BTMemory(){
+ return new BTMemory() {
@Override
public void setup() {
+ //LCD.drawString("Create connection", 0, 0);
BTConnector btconnector = new BTConnector();
- btconnector.waitForConnection(60, NXTConnection.RAW);
+ //LCD.drawString("BTConnector created", 0, 1);
+ connection = btconnector.waitForConnection(60000, NXTConnection.RAW);
+ //LCD.drawString("Connection created", 0, 2);
dataInput = connection.openDataInputStream();
- dataOutput = connection.openDataOutputStream();
+ dataOutput = connection.openOutputStream();
+ writer = new PrintWriter(dataOutput);
+ //LCD.drawString("Input output created", 0, 3);
}
};
}
}
-}
+}
\ No newline at end of file
import lejos.hardware.ev3.EV3;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.Font;
+import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.hardware.sensor.EV3ColorSensor;
public static void main(String[] args) {
EV3 brick = LocalEV3.get();
-
- //BTMemory.getBTMemory(true, null);
- BTMemory.getBTMemory(false, "Rover1");
LCDPrinter lcdprinter = new LCDPrinter(brick.getGraphicsLCD(), Font.getSmallFont());
+
+ /*
+ * Start the slave first. "Socket is 18" is always displayed, we can just ignore this
+ */
+ //BTMemory.getBTMemory(false, "Rover2");
+ BTMemory.getBTMemory(true, "Rover1");
+ LCDPrinter.print("Robots are connected");
+
LCDPrinter.print("Starting up systems");
lcdprinter.start();