From 132db792e5e0f636318a9dacb8efa783c1ade085 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 13 Sep 2011 16:06:35 +0100 Subject: Improved threading, sound and button processing. --- PunchingBag/src/PunchingBag.java | 132 ++++++++++++++++++++++++++---------- PunchingBag/src/PunchingBagGUI.java | 95 +++++++++++++++++++++++--- 2 files changed, 180 insertions(+), 47 deletions(-) diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 723d373..941e4d7 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -56,7 +56,9 @@ public class PunchingBag implements Runnable { private EventListenerList accelListenerList = new EventListenerList(); public PunchingBag() { - new Thread(this).start(); + Thread bagControlThread = new Thread(this); + bagControlThread.setPriority(Thread.MAX_PRIORITY); + bagControlThread.start(); } /** @@ -106,6 +108,14 @@ public class PunchingBag implements Runnable { runningEffects.add(new SquareExpand(x, y, intensity, colour)); } + void fillRect(int x, int y, int width, int height, Colour colour, long time) { + runningEffects.add(new FillRect(x, y, width, height, colour, time)); + } + + void rect(int x, int y, int width, int height, Colour colour) { + runningEffects.add(new Rect(x, y, width, height, colour)); + } + void noise(Rectangle rect, long time) { runningEffects.add(new Noise(rect, System.currentTimeMillis() + time)); } @@ -139,6 +149,51 @@ public class PunchingBag implements Runnable { } } + class FillRect extends Effect { + final int x; + final int y; + final int width; + final int height; + final Colour colour; + final long time; + + public FillRect(int x, int y, int width, int height, Colour colour, long time) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.colour = colour; + this.time = System.currentTimeMillis() + time; + } + + public void draw() { + fillRectInternal(x, y, width, height, colour); + if (System.currentTimeMillis() >= time) { + stop(); + } + } + } + + class Rect extends Effect { + final int x; + final int y; + final int width; + final int height; + final Colour colour; + + public Rect(int x, int y, int width, int height, Colour colour) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.colour = colour; + } + + public void draw() { + drawRectInternal(x, y, width, height, colour); + } + } + class Text extends Effect { final String string; final Area area; @@ -300,7 +355,7 @@ public class PunchingBag implements Runnable { return doneSomething; } - public boolean drawRectCorner(int x, int y, int height, int width, + public boolean drawRectInternal(int x, int y, int width, int height, Colour colour) { if (height < 0) { height = 0; @@ -315,12 +370,11 @@ public class PunchingBag implements Runnable { boolean doneSomething = false; int heightEx = 0; int widthEx = 0; - + boolean finished = true; do { if (setLEDInternal(x + widthEx, y, colour)) { doneSomething = true; } - if (setLEDInternal(x, y + heightEx, colour)) { doneSomething = true; } @@ -331,12 +385,14 @@ public class PunchingBag implements Runnable { doneSomething = true; } if (heightEx < height) { + finished = false; heightEx++; } if (widthEx < width) { - heightEx++; + finished = false; + widthEx++; } - } while (height >= 0 && width >= 0); + } while (!finished); return doneSomething; } @@ -405,20 +461,18 @@ public class PunchingBag implements Runnable { return drawnSomething; } - public boolean rectfill(int x, int y, int height, int width, Colour colour) { - if (height < 0) { - height = 0; - } - if (width < 0) { - width = 0; - } - if (width == 0 && height == 0) { - return setLEDInternal(x, y, colour); + public boolean fillRectInternal(int x, int y, int height, int width, + Colour colour) { + boolean doneSomething = false; + for (int px = x; px < (x + height); px++) { + for (int py = y; py < (y + width); py++) { + if (setLEDInternal(px, py, colour)) { + doneSomething = true; + } + } } - boolean doneSomething = false; - return doneSomething; } @@ -559,15 +613,15 @@ public class PunchingBag implements Runnable { private void readButtonData(String data) { if (data.replaceAll("[^0-9]", "").length() > 0) { - // System.out.print(data); + System.out.print(data); int num = Integer.valueOf(data.replaceAll("[^0-9]", "")); - int x = (num % 9); - if (num != 0) - x--; - int y = num / 9; - // System.out.println("X: " + x + " Y: " + y); - buttons[x][y] = true; - buttonsChanged = true; + int x = ((num-1) % 8); + int y = (num-1) / 8; + System.out.println("X: " + x + " Y: " + y); + if (!(x < 0)) { + buttons[x][y] = true; + buttonsChanged = true; + } } } @@ -592,19 +646,21 @@ public class PunchingBag implements Runnable { } public void run() { - long timeToSleep = 0; + long timeToSleep = 10; while (true) { - System.out.println("Time since last refresh: " - + (System.currentTimeMillis() - lastRefresh)); - if (timeToSleep > 0) { - if ((System.currentTimeMillis() - lastRefresh) > (1000 / 60)) { + //System.out.println("Time since last refresh: " + // + (System.currentTimeMillis() - lastRefresh)); + + if ((System.currentTimeMillis() - lastRefresh) > (1000 / 60)) { + if (timeToSleep > 0) { timeToSleep--; - } else { - timeToSleep++; } + } else { + timeToSleep++; } - System.out.println("Sleeping: " + timeToSleep); + + //System.out.println("Sleeping: " + timeToSleep); lastRefresh = System.currentTimeMillis(); // System.out.println("R"); @@ -629,7 +685,8 @@ public class PunchingBag implements Runnable { } } } - System.out.println("Effects: " + (System.currentTimeMillis() - beginTimeForEffects)); + //System.out.println("Effects: " + // + (System.currentTimeMillis() - beginTimeForEffects)); } long beginTimeButtonIn = System.currentTimeMillis(); @@ -673,8 +730,9 @@ public class PunchingBag implements Runnable { e.printStackTrace(); } } - System.out.println("Button: " + (System.currentTimeMillis() - beginTimeButtonIn)); - + //System.out.println("Button: " + // + (System.currentTimeMillis() - beginTimeButtonIn)); + calculateRawLeds(); String str; for (int i = 0; i < (6 * 8); i++) { @@ -717,7 +775,7 @@ public class PunchingBag implements Runnable { clearButtonGrid(); try { - Thread.sleep(1000 / 60); + Thread.sleep(timeToSleep); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 03e8f97..ef34459 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -12,6 +12,8 @@ import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.io.File; +import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JButton; @@ -27,6 +29,19 @@ import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.ListCellRenderer; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.Line; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.Mixer; +import javax.sound.sampled.TargetDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; +import javax.sound.sampled.AudioFormat.Encoding; +import javax.sound.sampled.Mixer.Info; + public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener { @@ -43,6 +58,13 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, None, Up, Down, Left, Right }; + String soundDir = System.getProperty("user.dir") + + System.getProperty("file.separator"); + + File[] keys = { new File(soundDir + "C5.wav"), + new File(soundDir + "D5.wav"), new File(soundDir + "E5.wav"), + new File(soundDir + "F5.wav"), new File(soundDir + "G5.wav") }; + static PunchingBag bag = new PunchingBag(); final boolean useDebugScreen = true; @@ -53,6 +75,13 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, } PunchingBagGUI() throws InterruptedException { + + //System.out.println(AudioSystem.getMixerInfo()[0].getName()); + //System.out.println(AudioSystem.getMixerInfo()[0].getDescription()); + //Mixer mix = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]); + + + bag.connectToArduinos(); frame = new JFrame("Punching Bag GUI"); @@ -87,6 +116,26 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, // frame.pack(); } + + void playKey(int key) { + try { + AudioInputStream sound = AudioSystem.getAudioInputStream(keys[key]); + DataLine.Info dataLine = new DataLine.Info(Clip.class, + sound.getFormat()); + Clip clip = (Clip) AudioSystem.getLine(dataLine); + clip.open(sound); + clip.start(); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnsupportedAudioFileException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } class SerialPanel extends JPanel implements ActionListener, SerialReadListener, SerialWriteListener { @@ -533,16 +582,42 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, @Override public void buttonPressed(int x, int y) { - System.out.println("Button Pressed: " + x + " " + y); - bag.circleExpand(x, y, 16); - //bag.setLED(x, y, PunchingBag.Colour.Red); - //bag.noise(new Rectangle(0, 0, 9, 20), 5000); - /*for (int bx=0; bx