diff options
author | Christopher Baines <cbaines8@gmail.com> | 2011-09-08 18:50:55 +0100 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2011-09-08 18:50:55 +0100 |
commit | 8962f50d7c24d428f0fdc2d1d90c65d4f9e7955c (patch) | |
tree | 13552db5abf3caadb9125c6483348246e612c38d /PunchingBag | |
parent | d379f8e548b3d0ee5d0f6db291415c96cd993351 (diff) | |
download | punchingbag-8962f50d7c24d428f0fdc2d1d90c65d4f9e7955c.tar punchingbag-8962f50d7c24d428f0fdc2d1d90c65d4f9e7955c.tar.gz |
More changes, some wrong (mostly serial stuff).
Diffstat (limited to 'PunchingBag')
-rw-r--r-- | PunchingBag/rxtxSerial.dll | bin | 0 -> 77759 bytes | |||
-rw-r--r-- | PunchingBag/src/Arduino.java | 58 | ||||
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 60 | ||||
-rw-r--r-- | PunchingBag/src/PunchingBagGUI.java | 101 | ||||
-rw-r--r-- | PunchingBag/src/SerialReadListener.java | 7 | ||||
-rw-r--r-- | PunchingBag/src/SerialWriteListener.java | 9 |
6 files changed, 189 insertions, 46 deletions
diff --git a/PunchingBag/rxtxSerial.dll b/PunchingBag/rxtxSerial.dll Binary files differnew file mode 100644 index 0000000..c0e6b58 --- /dev/null +++ b/PunchingBag/rxtxSerial.dll diff --git a/PunchingBag/src/Arduino.java b/PunchingBag/src/Arduino.java index 12df25f..4fa7cfa 100644 --- a/PunchingBag/src/Arduino.java +++ b/PunchingBag/src/Arduino.java @@ -9,12 +9,25 @@ import java.io.IOException; import java.io.InputStream;
import java.io.OutputStream;
+import javax.swing.event.EventListenerList;
+
import gnu.io.*;
public class Arduino implements Runnable {
InputStream in;
OutputStream out;
+
+ private EventListenerList readListenerList = new EventListenerList();
+ private EventListenerList writeListenerList = new EventListenerList();
+
+ public void addSerialWriteListener(SerialWriteListener l) {
+ writeListenerList.add(SerialWriteListener.class, l);
+ }
+
+ public void addSerialReadListener(SerialReadListener l) {
+ readListenerList.add(SerialReadListener.class, l);
+ }
static void listPorts()
{
@@ -46,6 +59,25 @@ public class Arduino implements Runnable { }
}
+ public String getID() throws IOException {
+ byte[] idBytes = {0x49, 0x44};
+ this.write(idBytes);
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ int id;
+ while ((id = this.read()) == -1) {}
+ if (id == 66) {
+ return "B";
+ } else if (id == 76) {
+ return "L";
+ } else {
+ return "ERROR " + id;
+ }
+ }
+
/**
* @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.
*/
@@ -60,7 +92,7 @@ public class Arduino implements Runnable { CommPort thePort = com.open("CommUtil", 50);
thePort.close();
h.add(com);
- System.out.println("Found a port: " + com.getPortType());
+ //System.out.println("Found a port: " + com.getPortType());
} catch (PortInUseException e) {
System.out.println("Port, " + com.getName() + ", is in use.");
} catch (Exception e) {
@@ -86,12 +118,12 @@ public class Arduino implements Runnable { if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
- serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
+ serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
- (new Thread(this)).start();
+ //(new Thread(this)).start();
}
else
{
@@ -168,7 +200,7 @@ public class Arduino implements Runnable { while ( ( len = this.in.read(buffer)) > -1 )
{
System.out.print(new String(buffer,0,len));
- input(new String(buffer,0,len));
+ //input(new String(buffer,0,len));
}
}
catch ( IOException e )
@@ -181,14 +213,30 @@ public class Arduino implements Runnable { public void write(byte[] bytes) throws IOException {
out.write(bytes);
+ SerialWriteListener[] sListeners = writeListenerList
+ .getListeners(SerialWriteListener.class);
+ for (int i = 0; i < writeListenerList.getListenerCount(); i++) {
+ sListeners[i].serialWriteEvent(bytes);
+ }
}
public void write(byte b) throws IOException {
out.write(b);
+ SerialWriteListener[] sListeners = writeListenerList
+ .getListeners(SerialWriteListener.class);
+ for (int i = 0; i < writeListenerList.getListenerCount(); i++) {
+ sListeners[i].serialWriteEvent(b);
+ }
}
public int read() throws IOException {
- return in.read();
+ int read = in.read();
+ SerialReadListener[] sListeners = readListenerList
+ .getListeners(SerialReadListener.class);
+ for (int i = 0; i < readListenerList.getListenerCount(); i++) {
+ sListeners[i].serialReadEvent(read);
+ }
+ return read;
}
}
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 4bab465..198e709 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -1,3 +1,5 @@ +import gnu.io.CommPortIdentifier; + import javax.swing.event.EventListenerList; import java.awt.Rectangle; @@ -7,6 +9,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.Iterator; // TODO: Loads of threads in this class, make sure everything is thread safe. @@ -175,7 +178,8 @@ public class PunchingBag implements Runnable { // longhand: currentRadius = currentRadius + 0.1 + (currentRadius / // 10); - if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, colour)) + if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, + colour)) stop = true; } @@ -264,12 +268,15 @@ public class PunchingBag implements Runnable { 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))); + 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))); + rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math + .floor(y / 4)] | (1 << (7 - x))); } } } @@ -319,6 +326,27 @@ public class PunchingBag implements Runnable { buttonsChanged = true; } + public boolean connectToArduinos() { + HashSet<CommPortIdentifier> serialPorts = Arduino + .getAvailableSerialPorts(); + for (Iterator iter = serialPorts.iterator(); iter.hasNext();) { + CommPortIdentifier comPort = (CommPortIdentifier) iter.next(); + // HACK FOR WINDOWS TO IDENIFY PORTS LIKELY TO CONTAIN ARDUINO + // (COM10 for instance). TODO: FIX + if (comPort.getName().length() == 5) { + System.out.println(comPort.getName()); + } + try { + buttonArduino.connect(comPort.getName()); + + // System.out.println("ID: " + buttonArduino.getID()); + } catch (Exception e) { + e.printStackTrace(); + } + } + return true; + } + public void run() { while (true) { synchronized (leds) { @@ -328,7 +356,8 @@ public class PunchingBag implements Runnable { // ConcurrentModificationException's // (havent managed to produce one // though - for (Iterator<Effect> iter = runningEffects.iterator(); iter.hasNext();) { + for (Iterator<Effect> iter = runningEffects.iterator(); iter + .hasNext();) { Effect ef = (Effect) iter.next(); if (ef.stop) { iter.remove(); @@ -342,6 +371,21 @@ public class PunchingBag implements Runnable { } } + if (buttonArduino.in != null) { + try { + int read = buttonArduino.read(); + if (read != -1) { + System.out.println(read); + } + byte[] bA = { 0x49 }; + if (Math.random() > 0.9) + buttonArduino.write(bA); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + /* * try { ledArduino.write(((byte) 108)); ledArduino.write(((byte) * 101)); ledArduino.write(rawLeds); } catch (IOException e1) { @@ -354,14 +398,16 @@ public class PunchingBag implements Runnable { */ if (ledsChanged) { - LEDListener[] LEDListeners = ledListenerList.getListeners(LEDListener.class); + LEDListener[] LEDListeners = ledListenerList + .getListeners(LEDListener.class); for (int i = 0; i < ledListenerList.getListenerCount(); i++) { LEDListeners[i].LEDchanged(); } } if (buttonsChanged) { - ButtonListener[] buttonListeners = buttonListenerList.getListeners(ButtonListener.class); + ButtonListener[] buttonListeners = buttonListenerList + .getListeners(ButtonListener.class); for (int i = 0; i < buttonListenerList.getListenerCount(); i++) { for (int x = 0; x < buttonWidth; x++) { for (int y = 0; y < buttonHeight; y++) { diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 19385f5..aacb2ac 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -27,7 +27,8 @@ import javax.swing.JTabbedPane; import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
-public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener {
+public class PunchingBagGUI implements MouseListener, MouseMotionListener,
+ ButtonListener, LEDListener {
public static enum Mode {
Menu, Interactive, Game
@@ -53,7 +54,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto }
PunchingBagGUI() throws InterruptedException {
- //bag.buttonArduino.listPorts();
+ bag.connectToArduinos();
frame = new JFrame("Punching Bag GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -67,12 +68,15 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto bag.addButtonListener(this);
bag.addLEDChangeListener(this);
- JSplitPane bottomTopSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
+ JSplitPane bottomTopSplitPane = new JSplitPane(
+ JSplitPane.VERTICAL_SPLIT);
- JSplitPane centerTopSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
+ JSplitPane centerTopSplitPane = new JSplitPane(
+ JSplitPane.HORIZONTAL_SPLIT);
JTabbedPane tabbedPane = new JTabbedPane();
- tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(), "Allows control of individual LED's");
+ tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(),
+ "Allows control of individual LED's");
centerTopSplitPane.setLeftComponent(new PunchingBagSim());
centerTopSplitPane.setRightComponent(tabbedPane);
@@ -81,11 +85,12 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto bottomTopSplitPane.setBottomComponent(new SerialPanel());
frame.add(bottomTopSplitPane, BorderLayout.CENTER);
-
- //frame.pack();
+
+ // frame.pack();
}
- class SerialPanel extends JPanel implements ActionListener {
+ class SerialPanel extends JPanel implements ActionListener,
+ SerialReadListener, SerialWriteListener {
JTextArea buttonIn = new JTextArea();
JTextArea buttonOut = new JTextArea();
JTextArea ledIn = new JTextArea();
@@ -93,8 +98,10 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto public SerialPanel() {
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- JSplitPane buttonSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- JSplitPane ledSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
+ JSplitPane buttonSplitPane = new JSplitPane(
+ JSplitPane.HORIZONTAL_SPLIT);
+ JSplitPane ledSplitPane = new JSplitPane(
+ JSplitPane.HORIZONTAL_SPLIT);
/*
* splitPane.setOneTouchExpandable(true);
* buttonSplitPane.setOneTouchExpandable(true);
@@ -108,56 +115,55 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto JButton clear = new JButton("Clear");
clear.addActionListener(this);
- // buttonIn.setColumns(20);
- // buttonIn.setRows(5);
- buttonIn.setBorder(BorderFactory.createTitledBorder("Serial In - Button Arduino"));
- // buttonOut.setColumns(20);
- // buttonOut.setRows(5);
- buttonOut.setBorder(BorderFactory.createTitledBorder("Serial Out - Button Arduino"));
- // ledIn.setColumns(20);
- // ledIn.setRows(5);
- ledIn.setBorder(BorderFactory.createTitledBorder("Serial In - LED Arduino"));
- // ledOut.setColumns(20);
- // ledOut.setRows(5);
- ledOut.setBorder(BorderFactory.createTitledBorder("Serial Out - LED Arduino"));
-
JPanel buttonInPanel = new JPanel();
JScrollPane areaScrollPane = new JScrollPane(buttonIn);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
buttonInPanel.add(areaScrollPane, BorderLayout.CENTER);
clear.setActionCommand("BI");
buttonInPanel.add(clear, BorderLayout.SOUTH);
+ buttonInPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial In - Button Arduino"));
buttonSplitPane.setLeftComponent(buttonInPanel);
JPanel buttonOutPanel = new JPanel();
areaScrollPane = new JScrollPane(buttonOut);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
buttonOutPanel.add(areaScrollPane, BorderLayout.CENTER);
clear = new JButton("Clear");
clear.setActionCommand("BO");
buttonOutPanel.add(clear, BorderLayout.SOUTH);
+ buttonOutPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial Out - Button Arduino"));
buttonSplitPane.setRightComponent(buttonOutPanel);
JPanel ledInPanel = new JPanel();
areaScrollPane = new JScrollPane(ledIn);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
ledInPanel.add(areaScrollPane, BorderLayout.CENTER);
clear = new JButton("Clear");
clear.setActionCommand("LI");
ledInPanel.add(clear, BorderLayout.SOUTH);
+ ledInPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial In - LED Arduino"));
ledSplitPane.setLeftComponent(ledInPanel);
JPanel ledOutPanel = new JPanel();
areaScrollPane = new JScrollPane(ledOut);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ areaScrollPane
+ .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 100));
ledOutPanel.add(areaScrollPane, BorderLayout.CENTER);
clear = new JButton("Clear");
clear.setActionCommand("LO");
ledOutPanel.add(clear, BorderLayout.SOUTH);
+ ledOutPanel.setBorder(BorderFactory
+ .createTitledBorder("Serial Out - LED Arduino"));
ledSplitPane.setRightComponent(ledOutPanel);
splitPane.setLeftComponent(buttonSplitPane);
@@ -165,6 +171,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto this.add(splitPane);
+ bag.buttonArduino.addSerialReadListener(this);
+ bag.buttonArduino.addSerialWriteListener(this);
+
}
@Override
@@ -172,6 +181,22 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto // TODO Auto-generated method stub
}
+
+ @Override
+ public void serialWriteEvent(byte b) {
+ buttonOut.append(String.valueOf(b));
+ }
+
+ @Override
+ public void serialWriteEvent(byte[] b) {
+ buttonOut.append(String.valueOf(b));
+ }
+
+ @Override
+ public void serialReadEvent(int b) {
+ if (b != -1)
+ buttonIn.append(String.valueOf(b));
+ }
}
class LEDControlPanel extends JPanel implements ActionListener {
@@ -195,7 +220,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto JComboBox rygCombo = new JComboBox(colours);
rygCombo.setSelectedIndex(0);
rygCombo.addActionListener(this);
- rygCombo.setActionCommand(String.valueOf(x) + "," + String.valueOf(y));
+ rygCombo.setActionCommand(String.valueOf(x) + ","
+ + String.valueOf(y));
// rygCombo.setRenderer(renderer);
this.add(rygCombo, c);
@@ -225,8 +251,12 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto } else if (((JComboBox) a.getSource()).getSelectedIndex() == 3) {
colour = PunchingBag.Colour.Green;
}
- System.out.println("Setting " + a.getActionCommand().split(",")[0] + " " + a.getActionCommand().split(",")[1]);
- bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]), Integer.valueOf(a.getActionCommand().split(",")[1]), colour);
+ System.out.println("Setting "
+ + a.getActionCommand().split(",")[0] + " "
+ + a.getActionCommand().split(",")[1]);
+ bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]),
+ Integer.valueOf(a.getActionCommand().split(",")[1]),
+ colour);
}
}
@@ -235,7 +265,9 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto super();
}
- public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+ public Component getListCellRendererComponent(JList list,
+ Object value, int index, boolean isSelected,
+ boolean cellHasFocus) {
// Get the selected index. (The index param isn't
// always valid, so just use the value.)
@@ -256,7 +288,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto }
}
- class PunchingBagSim extends JPanel implements ButtonListener, LEDListener, MouseListener {
+ class PunchingBagSim extends JPanel implements ButtonListener, LEDListener,
+ MouseListener {
int buttonMinSize = 10;
int buttonMaxSize = 50;
@@ -385,8 +418,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto g.setColor(Color.black);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
- g.setColor(new Color(220,220,220));
- g.fillRect(4, 4, this.getWidth()-8, this.getHeight()-8);
+ g.setColor(new Color(220, 220, 220));
+ g.fillRect(4, 4, this.getWidth() - 8, this.getHeight() - 8);
}
diff --git a/PunchingBag/src/SerialReadListener.java b/PunchingBag/src/SerialReadListener.java new file mode 100644 index 0000000..d8e20ed --- /dev/null +++ b/PunchingBag/src/SerialReadListener.java @@ -0,0 +1,7 @@ +import java.util.EventListener; + +interface SerialReadListener extends EventListener { + + void serialReadEvent(int b); + +} diff --git a/PunchingBag/src/SerialWriteListener.java b/PunchingBag/src/SerialWriteListener.java new file mode 100644 index 0000000..01a8db4 --- /dev/null +++ b/PunchingBag/src/SerialWriteListener.java @@ -0,0 +1,9 @@ +import java.util.EventListener; + +interface SerialWriteListener extends EventListener { + + void serialWriteEvent(byte b); + + void serialWriteEvent(byte[] b); + +} |