aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-14 14:41:46 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-14 14:41:46 +0100
commitd3aa36dfa804686ebc60eeb32c59f53d681d5e0c (patch)
tree0318b70ff4f61dab095fa9f721fb9b118df816a8 /PunchingBag/src
parentae8432ffe5cea8421b04068d0fe62d8e15175505 (diff)
downloadpunchingbag-d3aa36dfa804686ebc60eeb32c59f53d681d5e0c.tar
punchingbag-d3aa36dfa804686ebc60eeb32c59f53d681d5e0c.tar.gz
More serial improements, Giggle and SimpleKeyboard now work, kinda.
Diffstat (limited to 'PunchingBag/src')
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/Arduino.java1
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java110
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java27
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java42
4 files changed, 103 insertions, 77 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java b/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java
index 46ed3b5..78d6726 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java
@@ -6,6 +6,7 @@ import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
+import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
index 31b0575..5ef8006 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
@@ -590,35 +590,47 @@ public class PunchingBag implements Runnable {
}
private void readAccelData(String data) {
- //System.out.println("Data: " + data);
+ // System.out.println("Data: " + data);
String[] nums = data.split(" ");
- for (int x = 1; x < 5; x++) {
+ for (int x = 1; x < nums.length; x++) {
// Regex expression to strip newline at end (normally just 4th)
- int num = Integer.valueOf(nums[x].replaceAll("[^0-9]", ""));
- if (x == 1 && num != topAccelX) {
- topAccelX = num;
- acccelChanged = true;
- } else if (x == 2 && num != topAccelY) {
- topAccelY = num;
- acccelChanged = true;
- } else if (x == 3 && num != bottomAccelX) {
- bottomAccelX = num;
- acccelChanged = true;
- } else if (x == 4 && num != bottomAccelY) {
- bottomAccelY = num;
- acccelChanged = true;
+ if (nums[x].replaceAll("[^0-9]", "").length() == 0 || nums[x].replaceAll("[^0-9]", "").length() > 4) {
+ System.err.println("Accel Data Error: " + data);
+ continue;
}
+ int num;
+ try {
+ num = Integer.valueOf(nums[x].replaceAll("[^0-9]", ""));
+ if (x == 1 && num != topAccelX) {
+ topAccelX = num;
+ acccelChanged = true;
+ } else if (x == 2 && num != topAccelY) {
+ topAccelY = num;
+ acccelChanged = true;
+ } else if (x == 3 && num != bottomAccelX) {
+ bottomAccelX = num;
+ acccelChanged = true;
+ } else if (x == 4 && num != bottomAccelY) {
+ bottomAccelY = num;
+ acccelChanged = true;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.err.println("Data: " + data);
+ }
+
}
}
private void readButtonData(String data) {
+ // System.out.println("Data: " + 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 - 1) % 8);
int y = (num - 1) / 8;
- //System.out.println("X: " + x + " Y: " + y);
- if (!(x < 0)) {
+ // System.out.println("X: " + x + " Y: " + y);
+ if (x > 0 && x < buttonWidth && y > 0 && y < buttonHeight) {
buttons[x][y] = true;
buttonsChanged = true;
}
@@ -692,40 +704,61 @@ public class PunchingBag implements Runnable {
}
long beginTimeButtonIn = System.currentTimeMillis();
+ boolean doneA = false;
+ boolean doneB = false;
if (buttonArduino.in != null) {
try {
int read;
- while ((read = buttonArduino.read()) != -1) {
- // System.out.print((char) read);
- if ((char) read == 'A') {
+ // System.out.println("Reading selector");
+ while (!doneA || !doneB) {
+ read = buttonArduino.read();
+ if (read == -1)
+ break;
+ if (debugSerial)
+ System.out.println("Outside " + (char) read);
+ if ((char) read == 'B') {
+ if (debugSerial)
+ System.out.println(" Got an B, begining reading the button data");
String str = "";
// StringBuilder sb = new StringBuilder(20);
- while ((read = buttonArduino.read()) != '\n') {
+ while (true) {
+ read = buttonArduino.read();
+ if (read == '\n')
+ break;
str = str + String.valueOf((char) read);
+ if (debugSerial)
+ System.out.println("Reading button data: " + (char) read);
// sb.append((char) read);
}
+ if (debugSerial)
+ System.out.print("Finished reading button data because of newline " + (char) read);
// System.out.println("");
- readAccelData(str);
- // String[] nums = sbString.trim().split(" ");
- // System.out.println(nums);
- // for (int x=0; x<4; x++) {
- // System.out.println(nums[x]);
- // }
-
- } else if ((char) read == 'B') {
+ doneB = true;
+ readButtonData(str);
+ } else if ((char) read == 'A') {
+ if (debugSerial)
+ System.out.println(" Got an A, begining reading the accel data");
String str = "";
// StringBuilder sb = new StringBuilder(20);
- while ((read = buttonArduino.read()) != '\n') {
+ while (true) {
+ read = buttonArduino.read();
+ if (read == '\n')
+ break;
str = str + String.valueOf((char) read);
+ if (debugSerial)
+ System.out.println("Reading accel data " + (char) read);
// sb.append((char) read);
}
// System.out.println("");
- readButtonData(str);
+ doneA = true;
+ readAccelData(str);
+ // String[] nums = sbString.trim().split(" ");
+ // System.out.println(nums);
+ // for (int x=0; x<4; x++) {
+ // System.out.println(nums[x]);
+ // }
- } else {
- // System.out.println((char) read);
}
- // System.out.print("|");
}
} catch (IOException e) {
// TODO Auto-generated catch block
@@ -743,6 +776,7 @@ public class PunchingBag implements Runnable {
// System.out.println("");
// Arrays.fill(rawLeds, (byte) -42);
+ long beginTimeLedOut = System.currentTimeMillis();
if (ledArduino.out != null) {
// calculateRawLeds();
try {
@@ -752,7 +786,11 @@ public class PunchingBag implements Runnable {
e1.printStackTrace();
}
}
+ if (debugTimings)
+ System.out.println("Leds: " + (System.currentTimeMillis() - beginTimeLedOut));
+ // Arrays.fill(rawLeds, (byte) -42);
+ long beginTimeListeners = System.currentTimeMillis();
if (ledsChanged) {
LEDListener[] LEDListeners = ledListenerList.getListeners(LEDListener.class);
for (int i = 0; i < ledListenerList.getListenerCount(); i++) {
@@ -773,6 +811,8 @@ public class PunchingBag implements Runnable {
}
}
}
+ if (debugTimings)
+ System.out.println("Listeners: " + (System.currentTimeMillis() - beginTimeListeners));
clearButtonGrid();
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
index e790abb..32ca78a 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
@@ -589,7 +589,7 @@ 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.circleExpand(x, y, 16);
// bag.setLED(x, y, PunchingBag.Colour.Red);
// bag.noise(new Rectangle(0, 0, 9, 20), 5000);
/*
@@ -599,31 +599,6 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
*/
// 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(5);
- } else if (y < 6) {
- bag.fillRect(0, 3, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(4);
- } else if (y < 9) {
- bag.fillRect(0, 6, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(3);
- } else if (y < 12) {
- bag.fillRect(0, 9, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(2);
- } else if (y < 15) {
- bag.fillRect(0, 12, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(1);
- } else {
- bag.fillRect(0, 15, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(0);
- }
}
@Override
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();