diff options
Diffstat (limited to 'PunchingBag/src/PunchingBag.java')
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 60 |
1 files changed, 53 insertions, 7 deletions
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++) { |