diff options
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java')
-rw-r--r-- | PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java | 45 |
1 files changed, 34 insertions, 11 deletions
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(); + } + } + + } } |