diff options
author | unknown <Adam Martindale@.(none)> | 2011-09-12 13:06:49 +0100 |
---|---|---|
committer | unknown <Adam Martindale@.(none)> | 2011-09-12 13:06:49 +0100 |
commit | 4a097766a92c6d190e25c860097f20cd1cfbf069 (patch) | |
tree | face1b1aebd39edbfd6181efa266fe364f6b6d89 | |
parent | f03e95a0a1f178c3023f2f8c677de7a4ca9a2f67 (diff) | |
parent | 036d5b97bcf7e7e79d53f7e594e353be2d5efdd5 (diff) | |
download | punchingbag-4a097766a92c6d190e25c860097f20cd1cfbf069.tar punchingbag-4a097766a92c6d190e25c860097f20cd1cfbf069.tar.gz |
Merge branch 'master' of gitorious.org:punchingbag/punchingbag
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 97 |
1 files changed, 74 insertions, 23 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 3a364ac..dee4769 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -30,6 +30,12 @@ public class PunchingBag implements Runnable { private boolean[][] buttons = new boolean[buttonWidth][buttonHeight]; boolean buttonsChanged = false; + int bottomAccelX = 0; + int bottomAccelY = 0; + int topAccelX = 0; + int topAccelY = 0; + boolean acccelChanged = false; + private ArrayList<Effect> runningEffects = new ArrayList<Effect>(); Arduino buttonArduino = new Arduino(); @@ -281,7 +287,8 @@ public class PunchingBag implements Runnable { colour)) { doneSomething = true; } - if (setLEDInternal(x + (height / 2), (y - (height / 2)) + heightEx, colour)) { + if (setLEDInternal(x + (height / 2), (y - (height / 2)) + heightEx, + colour)) { doneSomething = true; } if (heightEx < height) { @@ -422,6 +429,14 @@ public class PunchingBag implements Runnable { } } + private void clearButtonGrid() { + for (int x = 0; x < buttonWidth; x++) { + for (int y = 0; y < buttonHeight; y++) { + buttons[x][y] = false; + } + } + } + private void clearEffects() { for (Iterator<Effect> iter = runningEffects.iterator(); iter.hasNext();) { ((Effect) iter.next()).stop(); @@ -466,22 +481,47 @@ public class PunchingBag implements Runnable { } return true; } - + private void readAccelData(String data) { - //System.out.print("Data: "); - System.out.println(data); - //String[] nums = data.split(" "); - /*System.out.println(nums); - for (int x=0; x<4; x++) { - System.out.println(nums[x]); - }*/ + String[] nums = data.split(" "); + for (int x = 1; x < 5; 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; + } + } + } + + private void readButtonData(String data) { + if (data.replaceAll("[^0-9]", "").length() > 0) { + // System.out.print(data + " length " + data.replaceAll("[^0-9]", + // "").length()); + int num = Integer.valueOf(data.replaceAll("[^0-9]", "")); + int x = (num % 8) - 1; + int y = num / 8; + // System.out.println("X: " + x + " Y: " + y); + buttons[x][y] = true; + buttonsChanged = true; + } } public void run() { while (true) { - //System.out.println("R"); + // System.out.println("R"); synchronized (leds) { clearLEDGrid(); + clearButtonGrid(); synchronized (runningEffects) { // Should prevent // ConcurrentModificationException's @@ -506,25 +546,36 @@ public class PunchingBag implements Runnable { try { int read; while ((read = buttonArduino.read()) != -1) { - //System.out.print((char) read); + // System.out.print((char) read); if ((char) read == 'A') { String str = ""; - //StringBuilder sb = new StringBuilder(20); + // StringBuilder sb = new StringBuilder(20); while ((read = buttonArduino.read()) != '\n') { str = str + String.valueOf((char) read); - //sb.append((char) read); + // sb.append((char) read); } - //System.out.println(""); + // 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]); - //} + // 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') { + String str = ""; + // StringBuilder sb = new StringBuilder(20); + while ((read = buttonArduino.read()) != '\n') { + str = str + String.valueOf((char) read); + // sb.append((char) read); + } + // System.out.println(""); + readButtonData(str); + } else { - //System.out.println((char) read); + // System.out.println((char) read); } - //System.out.print("|"); + // System.out.print("|"); } } catch (IOException e) { // TODO Auto-generated catch block @@ -532,7 +583,7 @@ public class PunchingBag implements Runnable { } } - /*if (ledArduino.out != null) { + if (ledArduino.out != null) { try { ledArduino.write(((byte) 108)); ledArduino.write(((byte) 101)); @@ -540,7 +591,7 @@ public class PunchingBag implements Runnable { } catch (IOException e1) { e1.printStackTrace(); } - }*/ + } if (ledsChanged) { LEDListener[] LEDListeners = ledListenerList |