From 036d5b97bcf7e7e79d53f7e594e353be2d5efdd5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 12 Sep 2011 11:47:56 +0100 Subject: Linked up the buttons and accelarometers with the computer. --- PunchingBag/src/PunchingBag.java | 97 ++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 23 deletions(-) diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 289405e..e925d4d 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -28,6 +28,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 runningEffects = new ArrayList(); Arduino buttonArduino = new Arduino(); @@ -246,7 +252,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) { @@ -387,6 +394,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 iter = runningEffects.iterator(); iter.hasNext();) { ((Effect) iter.next()).stop(); @@ -431,22 +446,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 @@ -471,25 +511,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 @@ -497,7 +548,7 @@ public class PunchingBag implements Runnable { } } - /*if (ledArduino.out != null) { + if (ledArduino.out != null) { try { ledArduino.write(((byte) 108)); ledArduino.write(((byte) 101)); @@ -505,7 +556,7 @@ public class PunchingBag implements Runnable { } catch (IOException e1) { e1.printStackTrace(); } - }*/ + } if (ledsChanged) { LEDListener[] LEDListeners = ledListenerList -- cgit v1.2.3