Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* As noted in the video this is just a hacked example of the centralnexus joystick api
- * (http://sourceforge.net/projects/javajoystick/)
- * Server command:
- * ser2net -d -C 1337:raw:0:/dev/ttyACM0
- * Arduino source: http://pastebin.com/Z5dtjkHN
- */
- package com.centralnexus.test;
- import java.awt.*;
- import java.net.*;
- import java.awt.event.*;
- import java.io.*;
- import com.centralnexus.input.*;
- public class WindowTest
- extends Frame
- implements Runnable, JoystickListener, ActionListener
- {
- Socket net;
- PrintStream ps;
- double servo1 = 0;
- double servo2 = 0;
- Joystick joy;
- Joystick joy2;
- /** polling interval for this joystick */
- private int interval = 50;
- Thread thread = new Thread(this);
- Label buttonLabel = new Label(),
- button2Label = new Label(),
- deadZoneLabel = new Label(),
- xLabel = new Label(),
- yLabel = new Label(),
- zLabel = new Label(),
- rLabel = new Label(),
- uLabel = new Label(),
- vLabel = new Label(),
- povLabel = new Label();
- Label xyLabel = new Label();
- Label intervalLabel = new Label();
- Button addButton = new Button("Add Listener");
- Button removeButton = new Button("Remove Listener");
- WindowTest() throws IOException {
- super();
- joy = Joystick.createInstance();
- for (int idx = joy.getID() + 1; idx < Joystick.getNumDevices(); idx++) {
- if (Joystick.isPluggedIn(idx)) {
- joy2 = Joystick.createInstance(idx);
- }
- }
- if (joy2 == null) {
- joy2 = joy;
- }
- doWindowLayout();
- net = new Socket("192.168.100.87",1337);
- ps = new PrintStream(net.getOutputStream());
- }
- WindowTest(int joystickID, int joyID2) throws IOException {
- super();
- joy = Joystick.createInstance(joystickID);
- joy2 = Joystick.createInstance(joyID2);
- doWindowLayout();
- }
- private void doWindowLayout() {
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- dispose();
- System.exit(0);
- }
- });
- setTitle("Joystick Test");
- setLayout(new GridLayout(20, 2));
- add(new Label("Number Of Devices: ", Label.RIGHT));
- add(new Label(Integer.toString(Joystick.getNumDevices())));
- add(new Label("Joystick ID: ", Label.RIGHT));
- add(new Label("joy(" + Integer.toString(joy.getID()) + "), joy#2(" + Integer.toString(joy2.getID()) + ")"));
- add(new Label("Description joy#1: ", Label.RIGHT));
- add(new Label(joy.toString()));
- add(new Label("Description joy#2: ", Label.RIGHT));
- add(new Label(joy2.toString()));
- add(new Label("Capabilities:", Label.RIGHT));
- add(new Label("joy(0x" + Integer.toHexString(joy.getCapabilities()) + "), joyEx(0x" + Integer.toHexString(joy2.getCapabilities()) + ")"));
- add(new Label("Axes: ", Label.RIGHT));
- add(new Label("joy(" + Integer.toString(joy.getNumAxes()) + "), joy#2(" + Integer.toString(joy2.getNumAxes()) + ")"));
- add(new Label("Buttons: ", Label.RIGHT));
- add(new Label("joy(" + Integer.toString(joy.getNumButtons()) + "), joy#2(" + Integer.toString(joy2.getNumButtons()) + ")"));
- add(new Label("Dead Zone Size: ", Label.RIGHT));
- add(deadZoneLabel);
- add(new Label("Buttons Pressed: 0x", Label.RIGHT));
- add(buttonLabel);
- add(new Label("X: ", Label.RIGHT));
- add(xLabel);
- add(new Label("Y: ", Label.RIGHT));
- add(yLabel);
- add(new Label("Z: ", Label.RIGHT));
- add(zLabel);
- add(new Label("R: ", Label.RIGHT));
- add(rLabel);
- add(new Label("U: ", Label.RIGHT));
- add(uLabel);
- add(new Label("V: ", Label.RIGHT));
- add(vLabel);
- add(new Label("POV: ", Label.RIGHT));
- add(povLabel);
- add(new Label("Joystick#2(x, y): ", Label.RIGHT));
- add(xyLabel);
- add(new Label("Buttons Pressed: 0x", Label.RIGHT));
- add(button2Label);
- add(new Label("Polling interval: ", Label.RIGHT));
- add(intervalLabel);
- addButton.addActionListener(this);
- removeButton.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e) {
- // System.out.println(e);
- if (e.getSource() == addButton) {
- try {
- net = new Socket("192.168.100.87",1337);
- ps = new PrintStream(net.getOutputStream());
- } catch (UnknownHostException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- else {
- joy.removeJoystickListener(this);
- joy2.removeJoystickListener(this);
- }
- }
- /**
- * This is used by the internal thread. It creates a lot of String
- * objects, so it uses the garbage collector a lot. Since this is
- * for testing only, this is not a problem for speed.
- */
- public void run() {
- for (;;) {
- joy.poll();
- joy2.poll();
- updateFieldsEx(joy);
- updateFields(joy2);
- try {
- Thread.sleep(interval);
- } catch(InterruptedException e) {
- break;
- }
- }
- }
- public void joystickAxisChanged(Joystick j) {
- // System.out.println(j.toString());
- if (j == joy) {
- updateFieldsEx(j);
- }
- else {
- updateFields(j);
- }
- }
- public void joystickButtonChanged(Joystick j) {
- // System.out.println(j.toString());
- if (j == joy) {
- updateFieldsEx(j);
- }
- else {
- updateFields(j);
- }
- }
- public void setPollInterval(int pollMillis) {
- interval = pollMillis;
- joy.setPollInterval(pollMillis);
- joy2.setPollInterval(pollMillis);
- intervalLabel.setText(Integer.toString(interval));
- }
- public void updateFields(Joystick joystick) {
- button2Label.setText(Integer.toHexString(joystick.getButtons()));
- xyLabel.setText(joystick.getX() + ", " + joystick.getY());
- }
- public void updateFieldsEx(Joystick joystick) {
- buttonLabel.setText(Integer.toHexString(joystick.getButtons()));
- xLabel.setText(Double.toString(joystick.getX()));
- yLabel.setText(Double.toString(joystick.getY()));
- zLabel.setText(Double.toString(joystick.getZ()));
- rLabel.setText(Double.toString(joystick.getR()));
- uLabel.setText(Double.toString(joystick.getU()));
- vLabel.setText(Double.toString(joystick.getV()));
- double joyX = joystick.getX();
- double joyY = joystick.getY();
- double povX = joystick.getU();
- double povY = joystick.getV();
- double povS = joystick.getZ()*10;
- double joyY1 = joyY;
- double joyY2 = joyY;
- double m1 = 0;
- double m2 = 0;
- joyY1 += joyX;
- joyY2 -= joyX;
- if (joyY1 > 0d){
- m1 = joyY1 * 126d;
- } else if (joyY1 < 0d){
- m1 = 128d + (-joyY1) * 126d;
- }
- if (joyY2 > 0d){
- m2 = joyY2 * 126d;
- } else if (joyY2 < 0d){
- m2 = 128d + (-joyY2) * 126d;
- }
- if (m1 > 255)
- m1 = 255;
- if (m2 > 255)
- m2 = 255;
- if (m1 < 0)
- m1 = 0;
- if (m2 < 0)
- m2 = 0;
- servo1+=povX*povS;
- servo2+=povY*povS;
- if (servo1 < 0)
- servo1 = 0;
- if (servo1 > 180)
- servo1 = 180;
- if (servo2 < 0)
- servo2 = 0;
- if (servo2 > 180)
- servo2 = 180;
- if ((joystick.getButtons() & 2) == 2){
- try {
- net.close();
- net = new Socket("192.168.100.87",1337);
- ps = new PrintStream(net.getOutputStream());
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- povLabel.setText(((int)m1)+" "+((int)m2)+" "+((int)servo1)+" "+((int)servo2));
- ps.print("c"+((int)m2)+" "+((int)m1)+" "+((int)servo1)+" "+((int)servo2));
- try {
- net.getOutputStream().write((byte)' ');
- } catch(Exception e){
- try {
- net = new Socket("192.168.100.87",1337);
- ps = new PrintStream(net.getOutputStream());
- System.err.println("REconnect");
- } catch (UnknownHostException e1) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (((!net.isConnected()) || net.isClosed())){
- try {
- net = new Socket("192.168.100.87",1337);
- ps = new PrintStream(net.getOutputStream());
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void startPolling() {
- thread.start();
- }
- public void addListeners() {
- add(addButton);
- add(removeButton);
- joy.addJoystickListener(this);
- joy2.addJoystickListener(this);
- }
- public void setDeadZone(double deadZone) {
- joy.setDeadZone(deadZone);
- updateDeadZone();
- }
- public void setDeadZoneEx(double deadZone) {
- joy2.setDeadZone(deadZone);
- updateDeadZone();
- }
- public void updateDeadZone() {
- deadZoneLabel.setText("joy(" + joy.getDeadZone() + "), joy#2("
- + joy2.getDeadZone() + ")");
- }
- private static void help() {
- System.out.println("Help:");
- System.out.println(" -h This help screen info");
- System.out.println(" -v Verbose Joystick debug information");
- System.out.println(" -j:n Set the Joystick ID to test (n is an integer)");
- System.out.println(" -j2:n Set the second joystick ID to test (n is an integer)");
- System.out.println(" -d:n Set the dead zone size of the Joystick (n is a real number)");
- System.out.println(" -d2:n Set the dead zone size of the second Joystick (n is a real number)");
- }
- public static void main(String args[]) {
- // This first and last one are never there, but this is for internal testing.
- // They should ALWAYS be false.
- try {
- WindowTest mainFrame;
- //WindowTest listenerFrame;
- int joystickNum = -1, joystickNumEx = -1;
- double deadZone = -1.0, deadZoneEx = -1.0;
- int interval = 50;
- for (int idx = 0; idx < args.length; idx++) {
- if (args[idx].startsWith("-d2:")) {
- deadZoneEx =
- Double.valueOf(args[idx].substring(4, args[idx].length()))
- .doubleValue();
- }
- else if (args[idx].startsWith("-d:")) {
- deadZone =
- Double.valueOf(args[idx].substring(3, args[idx].length()))
- .doubleValue();
- }
- else if (args[idx].startsWith("-i:")) {
- interval =
- Integer.valueOf(args[idx].substring(3, args[idx].length()))
- .intValue();
- }
- else if (args[idx].startsWith("-j:")) {
- joystickNum =
- Integer.valueOf(args[idx].substring(3, args[idx].length()))
- .intValue();
- }
- else if (args[idx].startsWith("-j2:")) {
- joystickNumEx =
- Integer.valueOf(args[idx].substring(4, args[idx].length()))
- .intValue();
- }
- else if (args[idx].startsWith("-v")) {
- for (int id = -1; id <= Joystick.getNumDevices(); id++) {
- System.out.println("Joystick " + id + ": " + Joystick.isPluggedIn(id));
- }
- }
- else if (args[idx].startsWith("-h")) {
- help();
- }
- else {
- System.out.println("Unknown option: " + args[idx]);
- help();
- }
- }
- if (joystickNum >= 0) {
- if (joystickNumEx < 0) {
- joystickNumEx = joystickNum;
- }
- mainFrame = new WindowTest(joystickNum, joystickNumEx);
- //listenerFrame = new WindowTest(joystickNum, joystickNumEx);
- }
- else {
- mainFrame = new WindowTest();
- // listenerFrame = new WindowTest();
- }
- if (deadZone >= 0.0) {
- mainFrame.setDeadZone(deadZone);
- // listenerFrame.setDeadZone(deadZone);
- }
- if (deadZoneEx >= 0.0) {
- mainFrame.setDeadZoneEx(deadZoneEx);
- // listenerFrame.setDeadZoneEx(deadZoneEx);
- }
- mainFrame.setPollInterval(interval);
- mainFrame.updateDeadZone();
- mainFrame.pack();
- mainFrame.setTitle("Polling Joystick");
- //mainFrame.show();
- mainFrame.setVisible(true);
- mainFrame.startPolling();
- //listenerFrame.setPollInterval(interval);
- //listenerFrame.updateDeadZone();
- // listenerFrame.addListeners();
- // listenerFrame.pack();
- // listenerFrame.setTitle("Listener Joystick");
- Point pt = mainFrame.getLocation();
- // pt.x += listenerFrame.getWidth();
- // listenerFrame.setLocation(pt);
- //listenerFrame.show();
- // listenerFrame.setVisible(true);
- } catch (IOException e) {
- System.err.println("");
- System.err.println(e.getMessage());
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement