aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-13 16:06:35 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-13 16:06:35 +0100
commit132db792e5e0f636318a9dacb8efa783c1ade085 (patch)
tree1c45ed92f8efada0a6f2e86403100fd635195006
parentb96fb8f5d5ba51cc62055fc115231c66ed9eedfb (diff)
downloadpunchingbag-132db792e5e0f636318a9dacb8efa783c1ade085.tar
punchingbag-132db792e5e0f636318a9dacb8efa783c1ade085.tar.gz
Improved threading, sound and button processing.
-rw-r--r--PunchingBag/src/PunchingBag.java132
-rw-r--r--PunchingBag/src/PunchingBagGUI.java95
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<bag.ledWidth; bx++) {
- for (int by=0; by<bag.ledHeight; by++) {
- bag.setLED(bx, by, PunchingBag.Colour.Red);
- }
- }*/
- //bag.squareExpand(x,y, 16, PunchingBag.Colour.Red);
+ 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<bag.ledWidth; bx++) { for (int by=0;
+ * by<bag.ledHeight; by++) { bag.setLED(bx, by, PunchingBag.Colour.Red);
+ * } }
+ */
+ // bag.squareExpand(x,y, 16, PunchingBag.Colour.Red);
+
+ // Ode to Joy
+ //bag.rect(0, 0, 9, 4, PunchingBag.Colour.Red);
+ //bag.rect(0, 4, 9, 4, PunchingBag.Colour.Red);
+ //bag.rect(0, 8, 9, 4, PunchingBag.Colour.Red);
+ //bag.rect(0, 12, 9, 4, PunchingBag.Colour.Red);
+ //bag.rect(0, 16, 9, 4, PunchingBag.Colour.Red);
+ if (y < 3) {
+ bag.fillRect(0, 0, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(0);
+ } else if (y < 6) {
+ bag.fillRect(0, 3, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(1);
+ } else if (y < 9) {
+ bag.fillRect(0, 6, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(2);
+ } else if (y < 12) {
+ bag.fillRect(0, 9, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(3);
+ } else if (y < 15) {
+ bag.fillRect(0, 12, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(4);
+ } else {
+ bag.fillRect(0, 15, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(4);
+ }
}
@Override