aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-08 18:50:55 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-08 18:50:55 +0100
commit8962f50d7c24d428f0fdc2d1d90c65d4f9e7955c (patch)
tree13552db5abf3caadb9125c6483348246e612c38d
parentd379f8e548b3d0ee5d0f6db291415c96cd993351 (diff)
downloadpunchingbag-8962f50d7c24d428f0fdc2d1d90c65d4f9e7955c.tar
punchingbag-8962f50d7c24d428f0fdc2d1d90c65d4f9e7955c.tar.gz
More changes, some wrong (mostly serial stuff).
-rwxr-xr-xArduino/ButtonMatrix/ButtonMatrix.pde110
-rw-r--r--PunchingBag/rxtxSerial.dllbin0 -> 77759 bytes
-rw-r--r--PunchingBag/src/Arduino.java58
-rw-r--r--PunchingBag/src/PunchingBag.java60
-rw-r--r--PunchingBag/src/PunchingBagGUI.java101
-rw-r--r--PunchingBag/src/SerialReadListener.java7
-rw-r--r--PunchingBag/src/SerialWriteListener.java9
7 files changed, 261 insertions, 84 deletions
diff --git a/Arduino/ButtonMatrix/ButtonMatrix.pde b/Arduino/ButtonMatrix/ButtonMatrix.pde
index 41ae6ab..99c879d 100755
--- a/Arduino/ButtonMatrix/ButtonMatrix.pde
+++ b/Arduino/ButtonMatrix/ButtonMatrix.pde
@@ -11,29 +11,48 @@ const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)
const int selfTest = A0;
+const int id = 1;
+
/* Each of the buttons can be refered to by a number
- At the moment the byte overflow for the 127+ numbers is dealt with at the
- getKey time, but if a unsigned byte could be used here it would remove the need
- for the correction?
-*/
+ At the moment the byte overflow for the 127+ numbers is dealt with at the
+ getKey time, but if a unsigned byte could be used here it would remove the need
+ for the correction?
+ */
char keys[COLS][ROWS] = {
-{1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97, 105, 113, 121, 129, 137, 145},
-{2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98, 106, 114, 122, 130, 138, 146},
-{3, 11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91, 99, 107, 115, 123, 131, 139, 147},
-{4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148},
-{5, 13, 21, 29, 37, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 149},
-{6, 14, 22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 142, 150},
-{7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 135, 143, 151},
-{8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152}
+ {
+ 1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97, 105, 113, 121, 129, 137, 145 }
+ ,
+ {
+ 2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98, 106, 114, 122, 130, 138, 146 }
+ ,
+ {
+ 3, 11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91, 99, 107, 115, 123, 131, 139, 147 }
+ ,
+ {
+ 4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148 }
+ ,
+ {
+ 5, 13, 21, 29, 37, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 149 }
+ ,
+ {
+ 6, 14, 22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 142, 150 }
+ ,
+ {
+ 7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 135, 143, 151 }
+ ,
+ {
+ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152 }
};
-byte colPins[COLS] = {9, 8, 7, 6, 5, 4, 3, 2};
-byte rowPins[ROWS] = {14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42};
+byte colPins[COLS] = {
+ 9, 8, 7, 6, 5, 4, 3, 2};
+byte rowPins[ROWS] = {
+ 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42};
/*
- Becuase of the diodes being fitted the WRONG WAY ROUND,
- the columns are rows and the rows are columns, have fun!
-*/
+ Becuase of the diodes being fitted the WRONG WAY ROUND,
+ the columns are rows and the rows are columns, have fun!
+ */
// ROW COL ROWS COLS
Keypad keypad = Keypad( makeKeymap(keys), colPins, rowPins, COLS, ROWS);
@@ -53,8 +72,8 @@ void setup(){
//digitalWrite(selfTest, HIGH);
}
/* Send numbers back and forth to test the integrity of the serial communication
- Computer sends the first byte
-*/
+ Computer sends the first byte
+ */
boolean runSerialTest() {
Serial.println("Begining Serial Test");
delay(10);
@@ -68,24 +87,39 @@ boolean runSerialTest() {
}
return true;
}
-
+
void loop(){
- char customKey = keypad.getKey();
- int customKeyInt = customKey;
- if (customKeyInt < 0) customKeyInt += 256;
- if (customKey != NO_KEY){
- Serial.println(customKeyInt);
- }
-
- // print the sensor values:
- Serial.print(analogRead(xpin));
- // print a tab between values:
- Serial.print("\t");
- Serial.print(analogRead(ypin));
- // print a tab between values:
- Serial.print("\t");
- Serial.print(analogRead(zpin));
- Serial.println();
- // delay before next reading:
- delay(10);
+ //if (Serial.peek() == 0x49) {
+ Serial.write(37);
+ //Serial.write(Serial.read());
+ /*if (Serial.read() == 0x44) {
+ Serial.write(id);
+ }*/
+ //} else {
+ // Serial.write(1);
+ //Serial.write(Serial.read());
+ //}
+
+ /*char customKey = keypad.getKey();
+ int customKeyInt = customKey;
+ if (customKeyInt < 0) customKeyInt += 256;
+ if (customKey != NO_KEY){
+ Serial.println(customKeyInt);
+ }
+
+ // print the sensor values:
+ Serial.print(analogRead(xpin));
+ // print a tab between values:
+ Serial.print("\t");
+ Serial.print(analogRead(ypin));
+ // print a tab between values:
+ Serial.print("\t");
+ Serial.print(analogRead(zpin));
+ Serial.println();
+ // delay before next reading:
+ //delay(10);*/
}
+
+
+
+
diff --git a/PunchingBag/rxtxSerial.dll b/PunchingBag/rxtxSerial.dll
new file mode 100644
index 0000000..c0e6b58
--- /dev/null
+++ b/PunchingBag/rxtxSerial.dll
Binary files differ
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);
+
+}