aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-08-24 18:06:39 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-08-24 18:06:39 +0100
commitd01fc7da9367c0d5032741a9189258b467ce5eef (patch)
treed1a5c4d235fbf0fb09b17c56293fd2611f940147 /PunchingBag
downloadpunchingbag-d01fc7da9367c0d5032741a9189258b467ce5eef.tar
punchingbag-d01fc7da9367c0d5032741a9189258b467ce5eef.tar.gz
Initial commit of the java program.
Diffstat (limited to 'PunchingBag')
-rw-r--r--PunchingBag/.classpath7
-rw-r--r--PunchingBag/.project17
-rw-r--r--PunchingBag/.settings/org.eclipse.jdt.core.prefs12
-rw-r--r--PunchingBag/bin/ButtonArduino.classbin0 -> 320 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI$Area.classbin0 -> 595 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI$Contact.classbin0 -> 974 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI$Direction.classbin0 -> 1191 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI$Mode.classbin0 -> 1070 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI$Ripple.classbin0 -> 1943 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI.classbin0 -> 3842 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino$Colour.classbin0 -> 1106 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino$SerialReader.classbin0 -> 1018 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino$SerialWriter.classbin0 -> 917 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino.classbin0 -> 4271 bytes
-rw-r--r--PunchingBag/src/ButtonArduino.java5
-rw-r--r--PunchingBag/src/PunchingBagGUI.java192
-rw-r--r--PunchingBag/src/SlaveArduino.java169
17 files changed, 402 insertions, 0 deletions
diff --git a/PunchingBag/.classpath b/PunchingBag/.classpath
new file mode 100644
index 0000000..3b45536
--- /dev/null
+++ b/PunchingBag/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="lib" path="/usr/share/java/RXTXcomm.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/PunchingBag/.project b/PunchingBag/.project
new file mode 100644
index 0000000..844e538
--- /dev/null
+++ b/PunchingBag/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>PunchingBag</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/PunchingBag/.settings/org.eclipse.jdt.core.prefs b/PunchingBag/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..9fd8280
--- /dev/null
+++ b/PunchingBag/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Wed Jul 13 14:12:05 BST 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
diff --git a/PunchingBag/bin/ButtonArduino.class b/PunchingBag/bin/ButtonArduino.class
new file mode 100644
index 0000000..0dd56f5
--- /dev/null
+++ b/PunchingBag/bin/ButtonArduino.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI$Area.class b/PunchingBag/bin/PunchingBagGUI$Area.class
new file mode 100644
index 0000000..49b8d90
--- /dev/null
+++ b/PunchingBag/bin/PunchingBagGUI$Area.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI$Contact.class b/PunchingBag/bin/PunchingBagGUI$Contact.class
new file mode 100644
index 0000000..54a4389
--- /dev/null
+++ b/PunchingBag/bin/PunchingBagGUI$Contact.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI$Direction.class b/PunchingBag/bin/PunchingBagGUI$Direction.class
new file mode 100644
index 0000000..acb7f1a
--- /dev/null
+++ b/PunchingBag/bin/PunchingBagGUI$Direction.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI$Mode.class b/PunchingBag/bin/PunchingBagGUI$Mode.class
new file mode 100644
index 0000000..27359c3
--- /dev/null
+++ b/PunchingBag/bin/PunchingBagGUI$Mode.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI$Ripple.class b/PunchingBag/bin/PunchingBagGUI$Ripple.class
new file mode 100644
index 0000000..4a7e871
--- /dev/null
+++ b/PunchingBag/bin/PunchingBagGUI$Ripple.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI.class b/PunchingBag/bin/PunchingBagGUI.class
new file mode 100644
index 0000000..424f5bc
--- /dev/null
+++ b/PunchingBag/bin/PunchingBagGUI.class
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino$Colour.class b/PunchingBag/bin/SlaveArduino$Colour.class
new file mode 100644
index 0000000..b70919a
--- /dev/null
+++ b/PunchingBag/bin/SlaveArduino$Colour.class
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino$SerialReader.class b/PunchingBag/bin/SlaveArduino$SerialReader.class
new file mode 100644
index 0000000..52b0fda
--- /dev/null
+++ b/PunchingBag/bin/SlaveArduino$SerialReader.class
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino$SerialWriter.class b/PunchingBag/bin/SlaveArduino$SerialWriter.class
new file mode 100644
index 0000000..a313e5f
--- /dev/null
+++ b/PunchingBag/bin/SlaveArduino$SerialWriter.class
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino.class b/PunchingBag/bin/SlaveArduino.class
new file mode 100644
index 0000000..8d24c5a
--- /dev/null
+++ b/PunchingBag/bin/SlaveArduino.class
Binary files differ
diff --git a/PunchingBag/src/ButtonArduino.java b/PunchingBag/src/ButtonArduino.java
new file mode 100644
index 0000000..415f370
--- /dev/null
+++ b/PunchingBag/src/ButtonArduino.java
@@ -0,0 +1,5 @@
+
+public class ButtonArduino {
+ public boolean[][] buttons = new boolean[4][7];
+
+}
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
new file mode 100644
index 0000000..8008c58
--- /dev/null
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -0,0 +1,192 @@
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Toolkit;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+public class PunchingBagGUI implements MouseListener, MouseMotionListener {
+
+ public static enum Mode {
+ Menu, Interactive, Game
+ };
+
+ Mode currentMode = Mode.Interactive;
+
+ final int bagXSize = 360;
+ final int bagYSize = 840;
+
+ static JFrame frame;
+ static JFrame bagSimFrame;
+
+ // Contact
+ public enum Direction {
+ None, Up, Down, Left, Right
+ };
+
+ final boolean useDebugScreen = true;
+
+ public static void main(String[] args) throws InterruptedException {
+ SlaveArduino.listPorts();
+
+ //new PunchingBagGUI();
+ }
+
+ PunchingBagGUI() throws InterruptedException {
+ frame = new JFrame("Punching Bag GUI");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.setSize(300, 300);
+ frame.setVisible(true);
+
+ bagSimFrame = new JFrame("Punching Bag Sim");
+ bagSimFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ bagSimFrame.setSize(bagXSize, bagYSize);
+ bagSimFrame.addMouseListener(this);
+ bagSimFrame.addMouseMotionListener(this);
+ bagSimFrame.setVisible(true);
+
+ System.out.println("Bag X: " + bagSimFrame.getX() + " Bag Y: "
+ + bagSimFrame.getY());
+
+ draw();
+ }
+
+ void draw() throws InterruptedException {
+ bagSimFrame.paintComponents(bagSimFrame.getGraphics());
+ // bagSimFrame.paintAll(bagSimFrame.getGraphics());
+ Thread.sleep(16);
+ draw();
+ }
+
+ public void runInteractively(Contact contact) {
+ bagSimFrame.add(new Ripple(contact));
+ }
+
+ class Ripple extends JPanel {
+ final Contact contact;
+ final int width;
+ double currentWidth;
+
+ Ripple(Contact contact) {
+ super();
+ this.contact = contact;
+ if (contact.area.width >= contact.area.height) {
+ width = contact.area.width;
+ } else {
+ width = contact.area.height;
+ }
+ currentWidth = width;
+ if (currentWidth <= 0.5) currentWidth = 0.5;
+ }
+
+ public void paintComponent(Graphics g) {
+ // Max size of ripple is 60cm
+ // Ripple will expand 60cm * strength/100
+
+ SlaveArduino.Colour colour = SlaveArduino.Colour.Red;
+ g.setColor(new Color(255, 0, 0, 255 - width));
+ boolean drawnSomething = false;
+
+ for (double i = 0; i < 360; i++) {
+ int x = contact.area.x
+ + (int) Math.round(currentWidth * Math.cos(i));
+ int y = contact.area.y
+ + (int) Math.round(currentWidth * Math.sin(i));
+
+ if (x >= 0 && x <= 8 && y >= 0 && y <= 19) {
+ SlaveArduino.setLED(colour, x, y);
+ drawnSomething =true;
+ }
+
+ if (useDebugScreen) {
+ g.fillOval(40 * x, 40 * y, 6, 6);
+
+ }
+ }
+ if (!drawnSomething) bagSimFrame.remove(this);
+
+ currentWidth += 0.1 + (currentWidth/10) + (contact.strength/130) ;
+ }
+ }
+
+ class Contact {
+ // Height of bag is
+
+ final long time; // Time of the contact with the bag (in millis)
+ final Area area;
+ final Direction direction;
+ final int strength; // 0 - 100
+
+ Contact(long time, Area area, Direction direction, int strength) {
+ this.time = time;
+ this.area = area;
+ this.direction = direction;
+ this.strength = strength;
+ }
+
+ Contact(long time, Area area) {
+ this(time, area, Direction.None, 50);
+ }
+
+ }
+
+ class Area {
+ public final int x;
+ public final int y;
+ public final int width;
+ public final int height;
+
+ Area(int x, int y, int width, int height) {
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+ }
+
+ }
+
+ public void mouseClicked(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseEntered(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseExited(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mousePressed(MouseEvent arg0) {
+ System.out.println("Mouse moved");
+ if (currentMode == Mode.Interactive) {
+ System.out.println("Button: " + arg0.getButton());
+ runInteractively(new Contact(System.currentTimeMillis(), new Area(
+ (arg0.getX() - 4) / 40, (arg0.getY() - 30) / 40, 0, 0), Direction.None , 30 * arg0.getButton()));
+ }
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ public void mouseReleased(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseDragged(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void mouseMoved(MouseEvent arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/PunchingBag/src/SlaveArduino.java b/PunchingBag/src/SlaveArduino.java
new file mode 100644
index 0000000..e3016c5
--- /dev/null
+++ b/PunchingBag/src/SlaveArduino.java
@@ -0,0 +1,169 @@
+import java.util.Enumeration;
+import java.util.HashSet;
+
+import gnu.io.CommPort;
+import gnu.io.CommPortIdentifier;
+import gnu.io.SerialPort;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+
+import gnu.io.*;
+
+public class SlaveArduino {
+
+ enum Colour {None, Red, Yellow, Green};
+
+ static Colour[][] LED = new Colour[9][20];
+
+ public static void setLED(Colour colour, int x, int y) {
+ if (x < 0 || x > 8 || y < 0 || y > 19) return;
+ LED[x][y] = colour;
+ }
+
+
+ static void listPorts()
+ {
+ System.out.println("Listing Comm ports:");
+ java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
+ while ( portEnum.hasMoreElements() )
+ {
+ CommPortIdentifier portIdentifier = portEnum.nextElement();
+ System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) );
+ }
+ }
+
+ static String getPortTypeName ( int portType )
+ {
+ switch ( portType )
+ {
+ case CommPortIdentifier.PORT_I2C:
+ return "I2C";
+ case CommPortIdentifier.PORT_PARALLEL:
+ return "Parallel";
+ case CommPortIdentifier.PORT_RAW:
+ return "Raw";
+ case CommPortIdentifier.PORT_RS485:
+ return "RS485";
+ case CommPortIdentifier.PORT_SERIAL:
+ return "Serial";
+ default:
+ return "unknown type";
+ }
+ }
+
+ /**
+ * @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.
+ */
+ public static HashSet<CommPortIdentifier> getAvailableSerialPorts() {
+ HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>();
+ Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
+ while (thePorts.hasMoreElements()) {
+ CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();
+ switch (com.getPortType()) {
+ case CommPortIdentifier.PORT_SERIAL:
+ try {
+ CommPort thePort = com.open("CommUtil", 50);
+ thePort.close();
+ h.add(com);
+ System.out.println("Found a port: " + com.getPortType());
+ } catch (PortInUseException e) {
+ System.out.println("Port, " + com.getName() + ", is in use.");
+ } catch (Exception e) {
+ System.err.println("Failed to open port " + com.getName());
+ e.printStackTrace();
+ }
+ }
+ }
+ return h;
+ }
+
+ void connect ( String portName ) throws Exception
+ {
+ CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
+ if ( portIdentifier.isCurrentlyOwned() )
+ {
+ System.out.println("Error: Port is currently in use");
+ }
+ else
+ {
+ CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
+
+ if ( commPort instanceof SerialPort )
+ {
+ SerialPort serialPort = (SerialPort) commPort;
+ serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
+
+ InputStream in = serialPort.getInputStream();
+ OutputStream out = serialPort.getOutputStream();
+
+ (new Thread(new SerialReader(in))).start();
+ (new Thread(new SerialWriter(out))).start();
+
+ }
+ else
+ {
+ System.out.println("Error: Only serial ports are handled by this example.");
+ }
+ }
+ }
+
+ /** */
+ public static class SerialReader implements Runnable
+ {
+ InputStream in;
+
+ public SerialReader ( InputStream in )
+ {
+ this.in = in;
+ }
+
+ public void run ()
+ {
+ byte[] buffer = new byte[1024];
+ int len = -1;
+ try
+ {
+ while ( ( len = this.in.read(buffer)) > -1 )
+ {
+ System.out.print(new String(buffer,0,len));
+ }
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /** */
+ public static class SerialWriter implements Runnable
+ {
+ OutputStream out;
+
+ public SerialWriter ( OutputStream out )
+ {
+ this.out = out;
+ }
+
+ public void run ()
+ {
+ try
+ {
+ int c = 0;
+ while ( ( c = System.in.read()) > -1 )
+ {
+ this.out.write(c);
+ }
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+}