From d3aa36dfa804686ebc60eeb32c59f53d681d5e0c Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Wed, 14 Sep 2011 14:41:46 +0100 Subject: More serial improements, Giggle and SimpleKeyboard now work, kinda. --- .../uk/ac/open/punchingbag/examples/Giggle.java | 42 +++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java') diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java index 89c7f62..6a69503 100644 --- a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java @@ -17,20 +17,20 @@ import uk.ac.open.punchingbag.PunchingBag; public class Giggle implements ButtonListener { PunchingBag bag = PunchingBag.getBag(); - - String soundDir = System.getProperty("user.dir") - + System.getProperty("file.separator"); - - File[] laughs = { new File(soundDir + "G4.wav"), - 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") }; + + Clip lastClip; + long lastContact = 0; + + String soundDir = System.getProperty("user.dir") + System.getProperty("file.separator"); + + File[] laughs = { new File(soundDir + "Laugh1.wav"), new File(soundDir + "Laugh2.wav"), new File(soundDir + "Laugh3.wav"), + new File(soundDir + "Laugh4.wav"), new File(soundDir + "Laugh5.wav"), new File(soundDir + "Laugh6.wav"), new File(soundDir + "Laugh6.wav") }; public static void main(String[] args) { new Giggle(); } - + Giggle() { bag.addButtonListener(this); bag.connectToArduinos(); @@ -38,27 +38,37 @@ public class Giggle implements ButtonListener { @Override public void buttonPressed(int x, int y) { - // TODO Auto-generated method stub - + // laugh((int) (Math.random()*6)); + } @Override public void contact(Contact c) { System.out.println(c); bag.circleExpand(c.x, c.y, 16); - if (c.force > 520) { - //int laugh = c.force + if ( System.currentTimeMillis() > (lastContact + 250)|| lastContact == 0) { + lastContact = System.currentTimeMillis(); + if (c.force > 516) { + laugh(0); + } else if (c.force > 500) { + laugh(5); + } else if (c.force < 500) { + laugh(3); + } } } - + void laugh(int laugh) { try { AudioInputStream sound = AudioSystem.getAudioInputStream(laughs[laugh]); - DataLine.Info dataLine = new DataLine.Info(Clip.class, - sound.getFormat()); + DataLine.Info dataLine = new DataLine.Info(Clip.class, sound.getFormat()); + if (lastClip != null) { + lastClip.stop(); + } Clip clip = (Clip) AudioSystem.getLine(dataLine); clip.open(sound); clip.start(); + lastClip = clip; } catch (LineUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); -- cgit v1.2.3