aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java32
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java4
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java45
3 files changed, 55 insertions, 26 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
index 7f0fe06..7477f0c 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
@@ -108,7 +108,7 @@ public class PunchingBag implements Runnable {
* linux)
*/
String buttonArduinoDeviceAddress = "COM4"; // TODO: This should be cached
- // localy
+ // localy
/**
* The Arduino managing the led drivers
*/
@@ -117,7 +117,8 @@ public class PunchingBag implements Runnable {
* The device address of the led arduino (COM* on Windows, /dev/tty* on
* linux)
*/
- String ledArduinoDeviceAddress = "COM3"; // TODO: This should be cached localy
+ String ledArduinoDeviceAddress = "COM3"; // TODO: This should be cached
+ // localy
/**
* Turn on to activate the command line messages regarding the run loop
@@ -334,7 +335,7 @@ public class PunchingBag implements Runnable {
public void noise(Rectangle rect, long time) {
runningEffects.add(new Noise(rect, System.currentTimeMillis() + time));
}
-
+
/**
* @param rect
* @param time
@@ -359,7 +360,7 @@ public class PunchingBag implements Runnable {
/**
* Set when the effect should stop (use <code>stop()</code> to set).
*/
- private boolean stop = false;
+ protected boolean stop = false;
/**
* Used to stop the effect, removing it from the list of effects
@@ -535,14 +536,16 @@ public class PunchingBag implements Runnable {
this.area = area;
this.endTime = endTime;
}
-
+
public Noise(Rectangle area) {
this(area, (long) 0);
}
@Override
public void draw() {
- if (endTime >= System.currentTimeMillis() && endTime != 0) {
+ if (endTime <= System.currentTimeMillis() && endTime != 0) {
+ stop();
+ } else {
for (int y = area.y; y < (area.y + area.height); y++) {
for (int x = area.x; x < (area.x + area.width); x++) {
double random = Math.random();
@@ -557,8 +560,7 @@ public class PunchingBag implements Runnable {
}
}
}
- } else {
- stop();
+
}
}
}
@@ -889,7 +891,8 @@ public class PunchingBag implements Runnable {
/**
* Processes the serial data regarding accelerations
*
- * @param data The data string to be processed
+ * @param data
+ * The data string to be processed
*/
private void readAccelData(String data) {
// System.out.println("Data: " + data);
@@ -928,7 +931,8 @@ public class PunchingBag implements Runnable {
/**
* Processes the serial data regarding buttons
*
- * @param data The data string to be processed
+ * @param data
+ * The data string to be processed
*/
private void readButtonData(String data) {
// System.out.println("Data: " + data);
@@ -970,13 +974,15 @@ public class PunchingBag implements Runnable {
System.out.println("");
}
- /*
- * The run method starts a loop which processes the serial data, effects and events
+ /*
+ * The run method starts a loop which processes the serial data, effects and
+ * events
*
* @see java.lang.Runnable#run()
*/
public void run() {
- long timeToSleep = 10; // The time slept at the end of the loop (to maintain the refresh rate)
+ long timeToSleep = 10; // The time slept at the end of the loop (to
+ // maintain the refresh rate)
while (true) {
if (debugTimings) {
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
index e03c4e6..2af7996 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
@@ -104,14 +104,14 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
// Mixer mix = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]);
try {
- // bag.connectToArduinos();
+ bag.connectToArduinos();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame = new JFrame("Punching Bag GUI");
- frame.setUndecorated(true);
+ //frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(600, 600);
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
index 2d91735..882a32f 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
@@ -1,6 +1,7 @@
package uk.ac.open.punchingbag.examples;
import java.awt.Color;
+import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
@@ -26,9 +27,8 @@ import uk.ac.open.punchingbag.ButtonListener;
import uk.ac.open.punchingbag.Contact;
import uk.ac.open.punchingbag.PunchingBag;
-public class SimpleKeyboard implements ButtonListener {
-
- String soundDir = System.getProperty("user.dir")
+public class SimpleKeyboard implements ButtonListener, Runnable {
+ String soundDir = System.getProperty("user.dir")
+ System.getProperty("file.separator");
File[] keys = { new File(soundDir + "G4.wav"),
@@ -37,6 +37,7 @@ public class SimpleKeyboard implements ButtonListener {
new File(soundDir + "G5.wav") };
static PunchingBag bag = PunchingBag.getBag();
+ long lastActionTime = System.currentTimeMillis();
public static final int DAMPER_PEDAL = 64;
@@ -51,9 +52,16 @@ public class SimpleKeyboard implements ButtonListener {
-4, -2, 0, 1, 3, 5, 7 };
public static void main(String[] args) throws InvalidMidiDataException, MidiUnavailableException, IOException {
+ try {
+ bag.connectToArduinos();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
new SimpleKeyboard();
- int instrument = 0;
+ /*int instrument = 0;
int tempo = 120;
String filename = null;
@@ -115,20 +123,16 @@ public class SimpleKeyboard implements ButtonListener {
MidiSystem.write(sequence, allowedTypes[0], new File(filename));
System.exit(0);
}
- }
+ }*/
}
public SimpleKeyboard() {
bag.addButtonListener(this);
- try {
- bag.connectToArduinos();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ new Thread (this).start();
}
public void buttonPressed(int x, int y) {
+ lastActionTime = System.currentTimeMillis();
System.out.println("Button Pressed: " + x + " " + y);
if (y < 3) {
bag.fillRect(0, 0, 9, 4, Color.red, 500);
@@ -287,5 +291,24 @@ public class SimpleKeyboard implements ButtonListener {
track.add(new MidiEvent(on, startTick));
track.add(new MidiEvent(off, startTick + tickLength));
}
+ @Override
+ public void run() {
+ while (true) {
+ System.out.println("Checking last action time");
+ if ((System.currentTimeMillis() - lastActionTime) > 60000) {
+ System.out.println("Activating Noise");
+ bag.noise(new Rectangle(0, 0, 9, 20));
+ } else {
+ System.out.println("Waiting more");
+ }
+ try {
+ Thread.sleep(20000);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ }
}