diff options
author | Christopher Baines <cbaines8@gmail.com> | 2011-09-02 10:56:45 +0100 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2011-09-02 10:56:45 +0100 |
commit | a5dceb099dcca2eb30b663c0f8f1767e8e68343b (patch) | |
tree | db94f22a5e9434967e175d10389d4e5fe463c962 | |
parent | 1d7fd079f80ad6b097aae795d74dd19e10b2e14e (diff) | |
download | punchingbag-a5dceb099dcca2eb30b663c0f8f1767e8e68343b.tar punchingbag-a5dceb099dcca2eb30b663c0f8f1767e8e68343b.tar.gz |
Inproved punching bag and general api.
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 Binary files differnew file mode 100644 index 0000000..8b8c133 --- /dev/null +++ b/PunchingBag/bin/Arduino$SerialReader.class diff --git a/PunchingBag/bin/SlaveArduino$SerialWriter.class b/PunchingBag/bin/Arduino$SerialWriter.class Binary files differindex a313e5f..310f84c 100644 --- a/PunchingBag/bin/SlaveArduino$SerialWriter.class +++ b/PunchingBag/bin/Arduino$SerialWriter.class diff --git a/PunchingBag/bin/Arduino.class b/PunchingBag/bin/Arduino.class Binary files differnew file mode 100644 index 0000000..7e81456 --- /dev/null +++ b/PunchingBag/bin/Arduino.class diff --git a/PunchingBag/bin/ButtonArduino.class b/PunchingBag/bin/ButtonArduino.class Binary files differindex 0dd56f5..77269cb 100644 --- a/PunchingBag/bin/ButtonArduino.class +++ b/PunchingBag/bin/ButtonArduino.class diff --git a/PunchingBag/bin/PunchingBag$Colour.class b/PunchingBag/bin/PunchingBag$Colour.class Binary files differnew file mode 100644 index 0000000..8524f32 --- /dev/null +++ b/PunchingBag/bin/PunchingBag$Colour.class diff --git a/PunchingBag/bin/PunchingBag.class b/PunchingBag/bin/PunchingBag.class Binary files differindex 65f48c7..aa67aa2 100644 --- a/PunchingBag/bin/PunchingBag.class +++ b/PunchingBag/bin/PunchingBag.class diff --git a/PunchingBag/bin/PunchingBagGUI$Ripple.class b/PunchingBag/bin/PunchingBagGUI$Ripple.class Binary files differdeleted file mode 100644 index b824480..0000000 --- a/PunchingBag/bin/PunchingBagGUI$Ripple.class +++ /dev/null diff --git a/PunchingBag/bin/PunchingBagGUI.class b/PunchingBag/bin/PunchingBagGUI.class Binary files differindex 362e5e7..23fe615 100644 --- a/PunchingBag/bin/PunchingBagGUI.class +++ b/PunchingBag/bin/PunchingBagGUI.class diff --git a/PunchingBag/bin/SlaveArduino$Colour.class b/PunchingBag/bin/SlaveArduino$Colour.class Binary files differdeleted file mode 100644 index b70919a..0000000 --- a/PunchingBag/bin/SlaveArduino$Colour.class +++ /dev/null diff --git a/PunchingBag/bin/SlaveArduino$SerialReader.class b/PunchingBag/bin/SlaveArduino$SerialReader.class Binary files differdeleted file mode 100644 index 52b0fda..0000000 --- a/PunchingBag/bin/SlaveArduino$SerialReader.class +++ /dev/null diff --git a/PunchingBag/bin/SlaveArduino.class b/PunchingBag/bin/SlaveArduino.class Binary files differdeleted file mode 100644 index 8d24c5a..0000000 --- a/PunchingBag/bin/SlaveArduino.class +++ /dev/null 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
|