aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-02 10:56:45 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-02 10:56:45 +0100
commita5dceb099dcca2eb30b663c0f8f1767e8e68343b (patch)
treedb94f22a5e9434967e175d10389d4e5fe463c962 /PunchingBag
parent1d7fd079f80ad6b097aae795d74dd19e10b2e14e (diff)
downloadpunchingbag-a5dceb099dcca2eb30b663c0f8f1767e8e68343b.tar
punchingbag-a5dceb099dcca2eb30b663c0f8f1767e8e68343b.tar.gz
Inproved punching bag and general api.
Diffstat (limited to 'PunchingBag')
-rw-r--r--PunchingBag/.classpath2
-rw-r--r--PunchingBag/bin/Arduino$SerialReader.classbin0 -> 998 bytes
-rw-r--r--PunchingBag/bin/Arduino$SerialWriter.class (renamed from PunchingBag/bin/SlaveArduino$SerialWriter.class)bin917 -> 897 bytes
-rw-r--r--PunchingBag/bin/Arduino.classbin0 -> 3875 bytes
-rw-r--r--PunchingBag/bin/ButtonArduino.classbin320 -> 360 bytes
-rw-r--r--PunchingBag/bin/PunchingBag$Colour.classbin0 -> 1102 bytes
-rw-r--r--PunchingBag/bin/PunchingBag.classbin755 -> 2643 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI$Ripple.classbin1614 -> 0 bytes
-rw-r--r--PunchingBag/bin/PunchingBagGUI.classbin3613 -> 3447 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino$Colour.classbin1106 -> 0 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino$SerialReader.classbin1018 -> 0 bytes
-rw-r--r--PunchingBag/bin/SlaveArduino.classbin4271 -> 0 bytes
-rw-r--r--PunchingBag/src/Arduino.java (renamed from PunchingBag/src/SlaveArduino.java)327
-rw-r--r--PunchingBag/src/ButtonArduino.java8
-rw-r--r--PunchingBag/src/PunchingBag.java97
-rw-r--r--PunchingBag/src/PunchingBagGUI.java40
16 files changed, 245 insertions, 229 deletions
diff --git a/PunchingBag/.classpath b/PunchingBag/.classpath
index 3b45536..2c8ecc4 100644
--- a/PunchingBag/.classpath
+++ b/PunchingBag/.classpath
@@ -2,6 +2,6 @@
<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="lib" path="C:/Documents and Settings/Christopher/My Documents/OUPunchingBag/punchingbag/PunchingBag/RXTXcomm.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/PunchingBag/bin/Arduino$SerialReader.class b/PunchingBag/bin/Arduino$SerialReader.class
new file mode 100644
index 0000000..8b8c133
--- /dev/null
+++ b/PunchingBag/bin/Arduino$SerialReader.class
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino$SerialWriter.class b/PunchingBag/bin/Arduino$SerialWriter.class
index a313e5f..310f84c 100644
--- a/PunchingBag/bin/SlaveArduino$SerialWriter.class
+++ b/PunchingBag/bin/Arduino$SerialWriter.class
Binary files differ
diff --git a/PunchingBag/bin/Arduino.class b/PunchingBag/bin/Arduino.class
new file mode 100644
index 0000000..7e81456
--- /dev/null
+++ b/PunchingBag/bin/Arduino.class
Binary files differ
diff --git a/PunchingBag/bin/ButtonArduino.class b/PunchingBag/bin/ButtonArduino.class
index 0dd56f5..77269cb 100644
--- a/PunchingBag/bin/ButtonArduino.class
+++ b/PunchingBag/bin/ButtonArduino.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBag$Colour.class b/PunchingBag/bin/PunchingBag$Colour.class
new file mode 100644
index 0000000..8524f32
--- /dev/null
+++ b/PunchingBag/bin/PunchingBag$Colour.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBag.class b/PunchingBag/bin/PunchingBag.class
index 65f48c7..aa67aa2 100644
--- a/PunchingBag/bin/PunchingBag.class
+++ b/PunchingBag/bin/PunchingBag.class
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI$Ripple.class b/PunchingBag/bin/PunchingBagGUI$Ripple.class
deleted file mode 100644
index b824480..0000000
--- a/PunchingBag/bin/PunchingBagGUI$Ripple.class
+++ /dev/null
Binary files differ
diff --git a/PunchingBag/bin/PunchingBagGUI.class b/PunchingBag/bin/PunchingBagGUI.class
index 362e5e7..23fe615 100644
--- a/PunchingBag/bin/PunchingBagGUI.class
+++ b/PunchingBag/bin/PunchingBagGUI.class
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino$Colour.class b/PunchingBag/bin/SlaveArduino$Colour.class
deleted file mode 100644
index b70919a..0000000
--- a/PunchingBag/bin/SlaveArduino$Colour.class
+++ /dev/null
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino$SerialReader.class b/PunchingBag/bin/SlaveArduino$SerialReader.class
deleted file mode 100644
index 52b0fda..0000000
--- a/PunchingBag/bin/SlaveArduino$SerialReader.class
+++ /dev/null
Binary files differ
diff --git a/PunchingBag/bin/SlaveArduino.class b/PunchingBag/bin/SlaveArduino.class
deleted file mode 100644
index 8d24c5a..0000000
--- a/PunchingBag/bin/SlaveArduino.class
+++ /dev/null
Binary files differ
diff --git a/PunchingBag/src/SlaveArduino.java b/PunchingBag/src/Arduino.java
index e3016c5..70ac608 100644
--- a/PunchingBag/src/SlaveArduino.java
+++ b/PunchingBag/src/Arduino.java
@@ -1,169 +1,158 @@
-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();
- }
- }
- }
-
-}
+import java.util.Enumeration;
+import java.util.HashSet;
+
+import gnu.io.CommPort;
+import gnu.io.CommPortIdentifier;
+import gnu.io.SerialPort;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+
+import gnu.io.*;
+
+public class Arduino {
+
+ 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();
+ }
+ }
+ }
+
+}
diff --git a/PunchingBag/src/ButtonArduino.java b/PunchingBag/src/ButtonArduino.java
index 415f370..b56a9ed 100644
--- a/PunchingBag/src/ButtonArduino.java
+++ b/PunchingBag/src/ButtonArduino.java
@@ -1,5 +1,9 @@
-public class ButtonArduino {
- public boolean[][] buttons = new boolean[4][7];
+public class ButtonArduino extends Arduino {
+
+ void setButtons(boolean[][] buttons) {
+
+
+ }
}
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java
index 278a8fc..69d9fd0 100644
--- a/PunchingBag/src/PunchingBag.java
+++ b/PunchingBag/src/PunchingBag.java
@@ -1,24 +1,87 @@
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-
-import javax.swing.JButton;
+import java.util.Arrays;
import javax.swing.event.EventListenerList;
+public class PunchingBag implements Runnable {
+ private byte[] rawLeds = new byte[6*8];
+ private Colour[][] leds = new Colour[9][20];
+ private byte[] ledGridIntensities = new byte[6];
+ private boolean[][] buttons = new boolean[8][19];
+
+ enum Colour {
+ None, Red, Yellow, Green
+ };
+
+ private EventListenerList buttonListenerList = new EventListenerList();
+ private EventListenerList ledListenerList = new EventListenerList();
+
+ public PunchingBag() {
+ new Thread(this).start();
+ }
+
+ /**
+ * Adds an <code>ActionListener</code> to the button.
+ *
+ * @param l
+ * the <code>ActionListener</code> to be added
+ */
+ public void addButtonListener(ButtonListener l) {
+ buttonListenerList.add(ButtonListener.class, l);
+ }
+
+ public void addLEDChangeListener(ButtonListener l) {
+ ledListenerList.add(ButtonListener.class, l);
+ }
+
+ public void setLED(byte x, byte y, Colour colour) {
+ leds[x][y] = colour;
+ }
-public class PunchingBag {
- protected EventListenerList listenerList = new EventListenerList();
-
- /**
- * Adds an <code>ActionListener</code> to the button.
- * @param l the <code>ActionListener</code> to be added
- */
- public void addButtonListener(ButtonListener l) {
- listenerList.add(ButtonListener.class, l);
- }
+ private void calculateRawLeds() {
+ for (int y = 0; y <= 18; y++) {
+ for (int x = 0; x <= 6; x++) {
+ if ((y % 2) == 0) {
+ if (leds[x][y] == Colour.Green
+ || leds[x][y] == Colour.Yellow) {
+ rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
+ .floor(y / 4)] | (1 << (7 - x)));
+ }
+ } else {
+ if (leds[x][y] == Colour.Red || leds[x][y] == Colour.Yellow) {
+ rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
+ .floor(y / 4)] | (1 << (7 - x)));
+ }
+ }
+ }
+ }
+ int x = 7;
+ // TODO: Complete and test, or rethink the following?
+ for (int startY = 0; startY <= 4; startY++) {
+ for (int y = 0; y <= 3; y++) {
+ if (leds[x][startY + (y * 4)] == Colour.Red || leds[x][startY + (y * 4)] == Colour.Yellow) {
+ rawLeds[(5 * 8) + startY] = (byte) (rawLeds[(int) Math
+ .floor(y / 4)] | (1 << (7 - x)));
+ }
+ if (leds[x][y] == Colour.Green || leds[x][y] == Colour.Yellow) {
+ rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
+ .floor(y / 4)] | (1 << (7 - x)));
+ }
+ }
+ }
+ }
+
+ public void clearLEDGrid() {
+ Arrays.fill(leds, Boolean.FALSE);
+ }
- public void setLED(byte x, byte y, byte intensity) {
-
-
+ public void setLEDGridIntensity(byte grid, byte intensity) {
+ ledGridIntensities[grid] = intensity;
}
+ public void setLEDIntensities(byte intensity) {
+ Arrays.fill(ledGridIntensities, intensity);
+ }
+
+ public void run() {
+
+ }
}
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
index 1bd1415..c3014ac 100644
--- a/PunchingBag/src/PunchingBagGUI.java
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -70,49 +70,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
}
public void runInteractively(Contact contact) {
- bagSimFrame.add(new Ripple(contact));
- }
-
- class Ripple extends JPanel {
- final Contact contact;
- double currentWidth;
-
- Ripple(Contact contact) {
- super();
- this.contact = contact;
- }
-
- 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));
- boolean drawnSomething = false;
- for (double i = 0; i < 360; i++) {
- int x = contact.x
- + (int) Math.round(currentWidth * Math.cos(i));
- int y = contact.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) ;
- }
}
-
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub