aboutsummaryrefslogtreecommitdiff
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
parentae8432ffe5cea8421b04068d0fe62d8e15175505 (diff)
downloadpunchingbag-d3aa36dfa804686ebc60eeb32c59f53d681d5e0c.tar
punchingbag-d3aa36dfa804686ebc60eeb32c59f53d681d5e0c.tar.gz
More serial improements, Giggle and SimpleKeyboard now work, kinda.
-rwxr-xr-xArduino/ButtonMatrix/ButtonMatrix.pde40
-rw-r--r--PunchingBag/Laugh1.wavbin0 -> 115328 bytes
-rw-r--r--PunchingBag/Laugh2.wavbin0 -> 57400 bytes
-rw-r--r--PunchingBag/Laugh3.wavbin0 -> 182536 bytes
-rw-r--r--PunchingBag/Laugh4.wavbin0 -> 202688 bytes
-rw-r--r--PunchingBag/Laugh5.wavbin0 -> 229580 bytes
-rw-r--r--PunchingBag/Laugh6.wavbin0 -> 174984 bytes
-rw-r--r--PunchingBag/Laugh7.wavbin0 -> 179732 bytes
-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
12 files changed, 126 insertions, 94 deletions
diff --git a/Arduino/ButtonMatrix/ButtonMatrix.pde b/Arduino/ButtonMatrix/ButtonMatrix.pde
index d3a0874..0956659 100755
--- a/Arduino/ButtonMatrix/ButtonMatrix.pde
+++ b/Arduino/ButtonMatrix/ButtonMatrix.pde
@@ -31,28 +31,28 @@ const int id = 1;
*/
char keys[COLS][ROWS] = {
{
- 1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97, 105, 113, 121, 129, 137, 145 }
+ 1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97, 105, 113, 121, 129, 137, 145 }
,
{
- 2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98, 106, 114, 122, 130, 138, 146 }
+ 2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98, 106, 114, 122, 130, 138, 146 }
,
{
- 3, 11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91, 99, 107, 115, 123, 131, 139, 147 }
+ 3, 11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91, 99, 107, 115, 123, 131, 139, 147 }
,
{
- 4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148 }
+ 4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148 }
,
{
- 5, 13, 21, 29, 37, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 149 }
+ 5, 13, 21, 29, 37, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 149 }
,
{
- 6, 14, 22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 142, 150 }
+ 6, 14, 22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 142, 150 }
,
{
- 7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 135, 143, 151 }
+ 7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 135, 143, 151 }
,
{
- 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152 }
+ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152 }
};
byte colPins[COLS] = {
@@ -108,7 +108,12 @@ boolean runSerialTest() {
void loop(){
- Serial.write('B');
+ int ax = analogRead(axpin);
+ int ay = analogRead(aypin);
+ int bx = analogRead(bxpin);
+ int by = analogRead(bypin);
+
+ Serial.print("B");
Serial.print(" ");
char customKey = keypad.getKey();
int customKeyInt = customKey;
@@ -117,27 +122,28 @@ void loop(){
Serial.print(customKeyInt);
}
Serial.println();
-
- Serial.write('A');
+
+ Serial.print("A");
Serial.print(" ");
// print the sensor values:
- Serial.print(analogRead(axpin));
+ Serial.print(ax);
// print a tab between values:
Serial.print(" ");
- Serial.print(analogRead(aypin));
+ Serial.print(ay);
Serial.print(" ");
// print the sensor values:
- Serial.print(analogRead(bxpin));
+ Serial.print(bx);
// print a tab between values:
Serial.print(" ");
- Serial.println(analogRead(bypin));
+ Serial.println(by);
// print a tab between values:
// delay before next reading:
- //delay(10);*/
- delay(100);
+ delay(60);
+ //delay(100);
}
+
diff --git a/PunchingBag/Laugh1.wav b/PunchingBag/Laugh1.wav
new file mode 100644
index 0000000..3dfde0b
--- /dev/null
+++ b/PunchingBag/Laugh1.wav
Binary files differ
diff --git a/PunchingBag/Laugh2.wav b/PunchingBag/Laugh2.wav
new file mode 100644
index 0000000..00980a4
--- /dev/null
+++ b/PunchingBag/Laugh2.wav
Binary files differ
diff --git a/PunchingBag/Laugh3.wav b/PunchingBag/Laugh3.wav
new file mode 100644
index 0000000..ad11f12
--- /dev/null
+++ b/PunchingBag/Laugh3.wav
Binary files differ
diff --git a/PunchingBag/Laugh4.wav b/PunchingBag/Laugh4.wav
new file mode 100644
index 0000000..0b6ef0f
--- /dev/null
+++ b/PunchingBag/Laugh4.wav
Binary files differ
diff --git a/PunchingBag/Laugh5.wav b/PunchingBag/Laugh5.wav
new file mode 100644
index 0000000..72921ff
--- /dev/null
+++ b/PunchingBag/Laugh5.wav
Binary files differ
diff --git a/PunchingBag/Laugh6.wav b/PunchingBag/Laugh6.wav
new file mode 100644
index 0000000..3d28bdd
--- /dev/null
+++ b/PunchingBag/Laugh6.wav
Binary files differ
diff --git a/PunchingBag/Laugh7.wav b/PunchingBag/Laugh7.wav
new file mode 100644
index 0000000..0d4ac87
--- /dev/null
+++ b/PunchingBag/Laugh7.wav
Binary files differ
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();