aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java')
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java110
1 files changed, 75 insertions, 35 deletions
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();