diff options
Diffstat (limited to 'PunchingBag/src/PunchingBag.java')
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 92 |
1 files changed, 66 insertions, 26 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index dc4573f..e5b8011 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -6,6 +6,7 @@ import java.awt.Rectangle; import java.awt.geom.Area; import java.io.IOException; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; @@ -405,22 +406,35 @@ public class PunchingBag implements Runnable { } private void calculateRawLeds() { - for (int y = 0; y <= 18; y++) { - for (int x = 0; x <= 6; x++) { - if ((y % 2) == 0) { - if (leds[x][y] == Colour.Green - || leds[x][y] == Colour.Yellow) { - rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math - .floor(y / 4)] | (1 << (7 - x))); - } - } else { - if (leds[x][y] == Colour.Red || leds[x][y] == Colour.Yellow) { - rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math - .floor(y / 4)] | (1 << (7 - x))); + // First clear everything + Arrays.fill(rawLeds, (byte) 0); + // First loop through the 5 easy arrays + for (int grid=0; grid<5; grid++) { + for (int x = 0; x < 8; x++) { + for (int y = 0; y < 8; y++) { + if ((y % 2) == 0) { + if (leds[1 + x][(grid * 4) + (y/2)] == Colour.Green || leds[1 + x][(grid * 4) + (y/2)] == Colour.Yellow ) { + rawLeds[(grid*8) + x] = (byte) (rawLeds[(grid*8) + x] | (1 << (7-y))); + } + } else { + if (leds[1 + x][(grid * 4) + (y/2)] == Colour.Red || leds[1 + x][(grid * 4) + (y/2)] == Colour.Yellow ) { + rawLeds[(grid*8) + x] = (byte) (rawLeds[(grid*8) + x] | (1 << (7-y))); + } } } } } + + + /* + * for (int y = 0; y <= 18; y++) { for (int x = 0; x <= 6; x++) { if ((y + * % 2) == 0) { if (leds[x][y] == Colour.Green || leds[x][y] == + * Colour.Yellow) { rawLeds[(int) Math.floor(y / 4)] = (byte) + * (rawLeds[(int) Math .floor(y / 4)] | (1 << (7 - x))); } } else { if + * (leds[x][y] == Colour.Red || leds[x][y] == Colour.Yellow) { + * rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math + * .floor(y / 4)] | (1 << (7 - x))); } } } } + */ /* * int x = 7; // TODO: Complete and test, or rethink the following? for * (int startY = 0; startY <= 4; startY++) { for (int y = 0; y <= 3; @@ -475,6 +489,13 @@ public class PunchingBag implements Runnable { } public boolean connectToArduinos() { + try { + ledArduino.connect("COM6"); + buttonArduino.connect("COM7"); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } HashSet<CommPortIdentifier> serialPorts = Arduino .getAvailableSerialPorts(); for (Iterator iter = serialPorts.iterator(); iter.hasNext();) { @@ -485,9 +506,8 @@ public class PunchingBag implements Runnable { System.out.println(comPort.getName()); } try { - buttonArduino.connect(comPort.getName()); - // System.out.println("ID: " + buttonArduino.getID()); + System.out.println(comPort.getName()); } catch (Exception e) { e.printStackTrace(); } @@ -528,13 +548,33 @@ public class PunchingBag implements Runnable { buttonsChanged = true; } } + + private void printByte(byte b) { + //System.out.println("Byte: " + b); + String str; + for (int j = 0; j < 8; j++) { + byte val = (byte) (b & (1 << (7-j))); + //System.out.println("Val: " + val + " " + (1 << (7-j))); + if (val > 0) { + str = "1"; + } else { + str = "0"; + } + System.out.print(str); + } + } + + private void printlnByte(byte b) { + printByte(b); + System.out.println(""); + } public void run() { while (true) { // System.out.println("R"); synchronized (leds) { clearLEDGrid(); - clearButtonGrid(); + // clearButtonGrid(); synchronized (runningEffects) { // Should prevent // ConcurrentModificationException's @@ -568,7 +608,7 @@ public class PunchingBag implements Runnable { // sb.append((char) read); } // System.out.println(""); - readAccelData(str); + //readAccelData(str); // String[] nums = sbString.trim().split(" "); // System.out.println(nums); // for (int x=0; x<4; x++) { @@ -595,11 +635,18 @@ public class PunchingBag implements Runnable { e.printStackTrace(); } } - + calculateRawLeds(); + String str; + for (int i = 0; i < (6 * 8); i++) { + //printByte(rawLeds[i]); + } + //System.out.println(""); + + //Arrays.fill(rawLeds, (byte) -42); if (ledArduino.out != null) { + //calculateRawLeds(); try { ledArduino.write(((byte) 108)); - ledArduino.write(((byte) 101)); ledArduino.write(rawLeds); } catch (IOException e1) { e1.printStackTrace(); @@ -627,14 +674,7 @@ public class PunchingBag implements Runnable { } } - // TODO: Hack? - for (int x = 0; x < buttonWidth; x++) { - for (int y = 0; y < buttonHeight; y++) { - buttons[x][y] = false; - } - } - - // TODO: Hack? + clearButtonGrid(); try { Thread.sleep(1000 / 60); |