diff --git a/src/ve/ucv/ciens/ccg/nxtarbot/NxtAR_bot.java b/src/ve/ucv/ciens/ccg/nxtarbot/NxtAR_bot.java index 81b0cb6..6568c2a 100644 --- a/src/ve/ucv/ciens/ccg/nxtarbot/NxtAR_bot.java +++ b/src/ve/ucv/ciens/ccg/nxtarbot/NxtAR_bot.java @@ -15,11 +15,47 @@ */ package ve.ucv.ciens.ccg.nxtarbot; -import lejos.nxt.Button; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import lejos.nxt.comm.Bluetooth; +import lejos.nxt.comm.NXTConnection; +import ve.ucv.ciens.ccg.nxtarbot.threads.CommRecv; +import ve.ucv.ciens.ccg.nxtarbot.threads.CommSend; public class NxtAR_bot{ + private static DataOutputStream dataOutputStream; + private static DataInputStream dataInputStream; + private static NXTConnection bluetoothConnection; + private static CommRecv recv; + private static CommSend send; + public static void main(String[] args){ - System.out.println("Hello, world!"); - Button.waitForAnyPress(); + bluetoothConnection = Bluetooth.waitForConnection(); + bluetoothConnection.setIOMode(NXTConnection.RAW); + dataOutputStream = bluetoothConnection.openDataOutputStream(); + dataInputStream = bluetoothConnection.openDataInputStream(); + + System.out.println("Connected"); + + send = new CommSend(dataOutputStream); + recv = new CommRecv(dataInputStream); + + recv.start(); + send.start(); + + try{ + recv.join(); + send.join(); + }catch(InterruptedException i){ } + + try{ + dataOutputStream.close(); + dataInputStream.close(); + }catch(IOException io){ + System.out.println(io.getMessage()); + } + bluetoothConnection.close(); } } diff --git a/src/ve/ucv/ciens/ccg/nxtarbot/threads/CommRecv.java b/src/ve/ucv/ciens/ccg/nxtarbot/threads/CommRecv.java new file mode 100644 index 0000000..7813df5 --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtarbot/threads/CommRecv.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2014 Miguel Angel Astor Romero + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ve.ucv.ciens.ccg.nxtarbot.threads; + +import java.io.DataInputStream; +import java.io.IOException; + +public class CommRecv extends Thread { + private boolean done; + private byte msg; + private DataInputStream iStream; + + public CommRecv(DataInputStream iStream){ + done = false; + this.iStream = iStream; + } + + @Override + public void run(){ + while(!done){ + try{ + msg = iStream.readByte(); + System.out.println("Byte: " + Byte.toString(msg)); + }catch(IOException io){ + done = true; + } + } + } +} diff --git a/src/ve/ucv/ciens/ccg/nxtarbot/threads/CommSend.java b/src/ve/ucv/ciens/ccg/nxtarbot/threads/CommSend.java new file mode 100644 index 0000000..779ab07 --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtarbot/threads/CommSend.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2014 Miguel Angel Astor Romero + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ve.ucv.ciens.ccg.nxtarbot.threads; + +import java.io.DataOutputStream; +import java.io.IOException; + +import lejos.nxt.LightSensor; +import lejos.nxt.SensorPort; + +public class CommSend extends Thread { + private boolean done; + private byte msg; + private LightSensor light; + private DataOutputStream oStream; + + public CommSend(DataOutputStream oStream){ + light = new LightSensor(SensorPort.S1); + done = false; + msg = 0; + this.oStream = oStream; + } + + @Override + public void run(){ + while(!done){ + try{ + if((msg = (byte)light.getLightValue()) < 40){ + oStream.writeByte(msg); + oStream.flush(); + } + }catch(IOException io){ + done = true; + } + } + } +}