From a5dceb099dcca2eb30b663c0f8f1767e8e68343b Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 2 Sep 2011 10:56:45 +0100 Subject: Inproved punching bag and general api. --- PunchingBag/.classpath | 2 +- PunchingBag/bin/Arduino$SerialReader.class | Bin 0 -> 998 bytes PunchingBag/bin/Arduino$SerialWriter.class | Bin 0 -> 897 bytes PunchingBag/bin/Arduino.class | Bin 0 -> 3875 bytes PunchingBag/bin/ButtonArduino.class | Bin 320 -> 360 bytes PunchingBag/bin/PunchingBag$Colour.class | Bin 0 -> 1102 bytes PunchingBag/bin/PunchingBag.class | Bin 755 -> 2643 bytes PunchingBag/bin/PunchingBagGUI$Ripple.class | Bin 1614 -> 0 bytes PunchingBag/bin/PunchingBagGUI.class | Bin 3613 -> 3447 bytes PunchingBag/bin/SlaveArduino$Colour.class | Bin 1106 -> 0 bytes PunchingBag/bin/SlaveArduino$SerialReader.class | Bin 1018 -> 0 bytes PunchingBag/bin/SlaveArduino$SerialWriter.class | Bin 917 -> 0 bytes PunchingBag/bin/SlaveArduino.class | Bin 4271 -> 0 bytes PunchingBag/src/Arduino.java | 158 ++++++++++++++++++++++ PunchingBag/src/ButtonArduino.java | 8 +- PunchingBag/src/PunchingBag.java | 97 +++++++++++--- PunchingBag/src/PunchingBagGUI.java | 40 ------ PunchingBag/src/SlaveArduino.java | 169 ------------------------ 18 files changed, 245 insertions(+), 229 deletions(-) create mode 100644 PunchingBag/bin/Arduino$SerialReader.class create mode 100644 PunchingBag/bin/Arduino$SerialWriter.class create mode 100644 PunchingBag/bin/Arduino.class create mode 100644 PunchingBag/bin/PunchingBag$Colour.class delete mode 100644 PunchingBag/bin/PunchingBagGUI$Ripple.class delete mode 100644 PunchingBag/bin/SlaveArduino$Colour.class delete mode 100644 PunchingBag/bin/SlaveArduino$SerialReader.class delete mode 100644 PunchingBag/bin/SlaveArduino$SerialWriter.class delete mode 100644 PunchingBag/bin/SlaveArduino.class create mode 100644 PunchingBag/src/Arduino.java delete mode 100644 PunchingBag/src/SlaveArduino.java (limited to 'PunchingBag') diff --git a/PunchingBag/.classpath b/PunchingBag/.classpath index 3b45536..2c8ecc4 100644 --- a/PunchingBag/.classpath +++ b/PunchingBag/.classpath @@ -2,6 +2,6 @@ - + diff --git a/PunchingBag/bin/Arduino$SerialReader.class b/PunchingBag/bin/Arduino$SerialReader.class new file mode 100644 index 0000000..8b8c133 Binary files /dev/null and b/PunchingBag/bin/Arduino$SerialReader.class differ diff --git a/PunchingBag/bin/Arduino$SerialWriter.class b/PunchingBag/bin/Arduino$SerialWriter.class new file mode 100644 index 0000000..310f84c Binary files /dev/null and b/PunchingBag/bin/Arduino$SerialWriter.class differ diff --git a/PunchingBag/bin/Arduino.class b/PunchingBag/bin/Arduino.class new file mode 100644 index 0000000..7e81456 Binary files /dev/null and b/PunchingBag/bin/Arduino.class differ diff --git a/PunchingBag/bin/ButtonArduino.class b/PunchingBag/bin/ButtonArduino.class index 0dd56f5..77269cb 100644 Binary files a/PunchingBag/bin/ButtonArduino.class and b/PunchingBag/bin/ButtonArduino.class differ diff --git a/PunchingBag/bin/PunchingBag$Colour.class b/PunchingBag/bin/PunchingBag$Colour.class new file mode 100644 index 0000000..8524f32 Binary files /dev/null and b/PunchingBag/bin/PunchingBag$Colour.class differ diff --git a/PunchingBag/bin/PunchingBag.class b/PunchingBag/bin/PunchingBag.class index 65f48c7..aa67aa2 100644 Binary files a/PunchingBag/bin/PunchingBag.class and b/PunchingBag/bin/PunchingBag.class differ diff --git a/PunchingBag/bin/PunchingBagGUI$Ripple.class b/PunchingBag/bin/PunchingBagGUI$Ripple.class deleted file mode 100644 index b824480..0000000 Binary files a/PunchingBag/bin/PunchingBagGUI$Ripple.class and /dev/null differ diff --git a/PunchingBag/bin/PunchingBagGUI.class b/PunchingBag/bin/PunchingBagGUI.class index 362e5e7..23fe615 100644 Binary files a/PunchingBag/bin/PunchingBagGUI.class and b/PunchingBag/bin/PunchingBagGUI.class differ diff --git a/PunchingBag/bin/SlaveArduino$Colour.class b/PunchingBag/bin/SlaveArduino$Colour.class deleted file mode 100644 index b70919a..0000000 Binary files a/PunchingBag/bin/SlaveArduino$Colour.class and /dev/null differ diff --git a/PunchingBag/bin/SlaveArduino$SerialReader.class b/PunchingBag/bin/SlaveArduino$SerialReader.class deleted file mode 100644 index 52b0fda..0000000 Binary files a/PunchingBag/bin/SlaveArduino$SerialReader.class and /dev/null differ diff --git a/PunchingBag/bin/SlaveArduino$SerialWriter.class b/PunchingBag/bin/SlaveArduino$SerialWriter.class deleted file mode 100644 index a313e5f..0000000 Binary files a/PunchingBag/bin/SlaveArduino$SerialWriter.class and /dev/null differ diff --git a/PunchingBag/bin/SlaveArduino.class b/PunchingBag/bin/SlaveArduino.class deleted file mode 100644 index 8d24c5a..0000000 Binary files a/PunchingBag/bin/SlaveArduino.class and /dev/null differ diff --git a/PunchingBag/src/Arduino.java b/PunchingBag/src/Arduino.java new file mode 100644 index 0000000..70ac608 --- /dev/null +++ b/PunchingBag/src/Arduino.java @@ -0,0 +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.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 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 getAvailableSerialPorts() { + HashSet h = new HashSet(); + 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 ActionListener to the button. + * + * @param l + * the ActionListener 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 ActionListener to the button. - * @param l the ActionListener 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 diff --git a/PunchingBag/src/SlaveArduino.java b/PunchingBag/src/SlaveArduino.java deleted file mode 100644 index e3016c5..0000000 --- a/PunchingBag/src/SlaveArduino.java +++ /dev/null @@ -1,169 +0,0 @@ -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 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 getAvailableSerialPorts() { - HashSet h = new HashSet(); - 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(); - } - } - } - -} -- cgit v1.2.3