Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
hlfbt committed Sep 22, 2012
1 parent 4451956 commit be3eb22
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
37 changes: 10 additions & 27 deletions src/me/mastermind/NXJ_pc/Connector.java
@@ -1,7 +1,3 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package me.mastermind.NXJ_pc;

import java.io.IOException;
Expand All @@ -12,22 +8,22 @@
import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTCommInputStream;
import lejos.pc.comm.NXTConnector;
import lejos.pc.comm.NXTInfo;

/**
*
* @author Alexander
*/
public class Connector implements Runnable {
public class Connector {

private NXTComm nxtComm = null;
private NXTInfo[] nxtInfo = null;
private NXTConnector nxtCon = new NXTConnector();
private OutputStream nxtOut = null;
private InputStream nxtIn = null;

public int data = 0;

public static NXTCommInputStream nxtIn = null;

public Connector() {
try {
Expand All @@ -45,14 +41,17 @@ public NXTInfo[] getNXTInfo() {
public boolean tryConnection(int index, int mode) {
if (nxtCon.connectTo(nxtInfo[index], mode)) {
nxtOut = nxtCon.getOutputStream();
nxtIn = nxtCon.getInputStream();
new Thread(this).start();
nxtIn = (NXTCommInputStream) nxtCon.getInputStream();
return true;
} else {
return false;
}
}

public InputStream getNXTInputStream() {
return nxtIn;
}

public boolean writeData(int data) {
try {
if (data >=255 || data < 0) {
Expand All @@ -74,20 +73,4 @@ public void killNXT() {
System.exit(1);
}
}

@Override
public void run() {
while(true) {
try {
data = nxtIn.read();
if (data == 255) {
System.out.println("Shutdown by NXT");
System.exit(0);
}
} catch (IOException ex) {
Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
System.exit(0); // workaround because it doesn't get the data packet for some reason...
}
}
}
}
}
22 changes: 16 additions & 6 deletions src/me/mastermind/NXJ_pc/GUI.java
@@ -1,16 +1,10 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package me.mastermind.NXJ_pc;

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
Expand Down Expand Up @@ -43,6 +37,20 @@ public boolean dispatchKeyEvent(KeyEvent e) {
lastKeyPressed = KeyEvent.VK_DOWN;
}
break;
case KeyEvent.VK_LEFT:
if (lastKeyPressed != KeyEvent.VK_LEFT) {
statusLabel.setText("Status: Left");
RemoteController.write(15);
lastKeyPressed = KeyEvent.VK_LEFT;
}
break;
case KeyEvent.VK_RIGHT:
if (lastKeyPressed != KeyEvent.VK_RIGHT) {
statusLabel.setText("Status: Right");
RemoteController.write(25);
lastKeyPressed = KeyEvent.VK_RIGHT;
}
break;
case KeyEvent.VK_PLUS:
statusLabel.setText("Status: Speed+");
RemoteController.write(40);
Expand All @@ -61,6 +69,8 @@ public boolean dispatchKeyEvent(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_PLUS:
case KeyEvent.VK_MINUS:
statusLabel.setText("Status: Stop");
Expand Down
31 changes: 31 additions & 0 deletions src/me/mastermind/NXJ_pc/Receiver.java
@@ -0,0 +1,31 @@
package me.mastermind.NXJ_pc;

import java.io.InputStream;
import lejos.pc.comm.NXTCommInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author alex
*/
public class Receiver implements Runnable {
@Override
public void run() {
int data;
NXTCommInputStream nxtIn = Connector.nxtIn;
while(true) {
try {
data = nxtIn.read();
System.out.println(data);
if (data == 255) {
System.out.println("Shutdown by NXT");
System.exit(0);
}
} catch (Exception ex) {
Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
// System.exit(0); // workaround because it doesn't get the data packet for some reason...
}
}
}
}
11 changes: 3 additions & 8 deletions src/me/mastermind/NXJ_pc/RemoteController.java
@@ -1,7 +1,3 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package me.mastermind.NXJ_pc;

import lejos.pc.comm.NXTComm;
Expand All @@ -11,12 +7,9 @@
* @author Alexander
*/
public class RemoteController {

/**
* @param args the command line arguments
*/

private static Connector nxt = new Connector();
private static Thread receiver;

public static void main(String[] args) {
GUI.main(args);
Expand All @@ -31,6 +24,8 @@ public static boolean initalize() {
System.out.println("Could not connect to NXT.");
return false;
}
receiver = new Thread(new Receiver());
receiver.start();
return true;
}

Expand Down

0 comments on commit be3eb22

Please sign in to comment.