From 476322ce729ef56d808be66843fef18a83470191 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 12 Sep 2011 14:17:46 +0100 Subject: More led stuff. --- Arduino/LEDMatrix/LEDMatrix.pde | 23 ++++++++++- PunchingBag/src/PunchingBag.java | 78 +++++++++++++++++++++++++------------ PunchingBag/src/PunchingBagGUI.java | 7 +++- 3 files changed, 81 insertions(+), 27 deletions(-) diff --git a/Arduino/LEDMatrix/LEDMatrix.pde b/Arduino/LEDMatrix/LEDMatrix.pde index 4a07bfc..5aada79 100644 --- a/Arduino/LEDMatrix/LEDMatrix.pde +++ b/Arduino/LEDMatrix/LEDMatrix.pde @@ -24,10 +24,26 @@ void setup ( ) void loop ( ) { - for (int i=0; i<(chips * 8); i++) { - buffer[i] = 0b10101010;//random(-128,127); // Just Green + buffer[0] = 127; + buffer[1] = 0b10101010; + buffer[2] = 0b01010101; + for (int i=3; i<(chips * 8); i++) { + buffer[i] = 0;//random(-128,127); // Just Green 0b10101010 } matrix.Update(); + + /* + while (true) { + int read = Serial.read(); + if ((byte) read == (byte) 108) { + for (int b=0; b< (chips * 8); b++) { + buffer[b] = (byte) Serial.read(); + } + break; + } + } + matrix.Update();*/ + } @@ -38,3 +54,6 @@ void loop ( ) + + + diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index e925d4d..925d5c6 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -357,22 +357,37 @@ 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 ) { + //System.out.println("X: " + (x+1) + " Y: " + ((grid * 4) + (y/2)) + " : Green"); + //printlnByte((byte) (1 << (8-y))); + rawLeds[(grid*8) + x] = (byte) (rawLeds[(grid*8) + x] | (1 << (8-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 << (8-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; @@ -437,7 +452,7 @@ public class PunchingBag implements Runnable { System.out.println(comPort.getName()); } try { - buttonArduino.connect(comPort.getName()); + ledArduino.connect(comPort.getName()); // System.out.println("ID: " + buttonArduino.getID()); } catch (Exception e) { @@ -480,13 +495,30 @@ public class PunchingBag implements Runnable { buttonsChanged = true; } } + + private void printByte(byte b) { + String str; + for (int j = 0; j < 8; j++) { + if ((b & (1 << (8 - j))) > 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 @@ -547,11 +579,16 @@ public class PunchingBag implements Runnable { e.printStackTrace(); } } - + calculateRawLeds(); + /*String str; + for (int i = 0; i < (6 * 8); i++) { + printByte(rawLeds[i]); + } + System.out.println("");*/ if (ledArduino.out != null) { + //calculateRawLeds(); try { ledArduino.write(((byte) 108)); - ledArduino.write(((byte) 101)); ledArduino.write(rawLeds); } catch (IOException e1) { e1.printStackTrace(); @@ -579,14 +616,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); diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 9a23741..7af4d71 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -536,7 +536,12 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, public void buttonPressed(int x, int y) { System.out.println("Button Pressed: " + x + " " + y); // bag.circleExpand(x, y, 16); - bag.noise(new Rectangle(0, 0, 9, 20), 5000); + //bag.noise(new Rectangle(0, 0, 9, 20), 5000); + for (int bx=0; bx Date: Mon, 12 Sep 2011 15:50:50 +0100 Subject: Kinda working stuff. --- Arduino/LEDMatrix/LEDMatrix.pde | 30 +++++++++++++++++------------- PunchingBag/src/PunchingBag.java | 32 +++++++++++++++++++++----------- PunchingBag/src/PunchingBagGUI.java | 6 +++--- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Arduino/LEDMatrix/LEDMatrix.pde b/Arduino/LEDMatrix/LEDMatrix.pde index 5aada79..1a709be 100644 --- a/Arduino/LEDMatrix/LEDMatrix.pde +++ b/Arduino/LEDMatrix/LEDMatrix.pde @@ -24,26 +24,29 @@ void setup ( ) void loop ( ) { - buffer[0] = 127; - buffer[1] = 0b10101010; - buffer[2] = 0b01010101; - for (int i=3; i<(chips * 8); i++) { - buffer[i] = 0;//random(-128,127); // Just Green 0b10101010 - } - matrix.Update(); - - /* + /*buffer[0] = 127; + buffer[1] = 0b10101010; + buffer[2] = 0b01010101; + for (int i=3; i<(chips * 8); i++) { + buffer[i] = 0;//random(-128,127); // Just Green 0b10101010 + } + matrix.Update();*/ + + while (true) { int read = Serial.read(); if ((byte) read == (byte) 108) { - for (int b=0; b< (chips * 8); b++) { - buffer[b] = (byte) Serial.read(); + for (int b=0; b< (chips * 8); ) { + if ((read = Serial.read()) != -1) { + buffer[b] = (byte) read; + b++; + } } break; } } - matrix.Update();*/ - + matrix.Update(); + } @@ -57,3 +60,4 @@ void loop ( ) + diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index a7b7070..ef5cb18 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -8,6 +8,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; @@ -400,13 +401,11 @@ public class PunchingBag implements Runnable { 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 ) { - //System.out.println("X: " + (x+1) + " Y: " + ((grid * 4) + (y/2)) + " : Green"); - //printlnByte((byte) (1 << (8-y))); - rawLeds[(grid*8) + x] = (byte) (rawLeds[(grid*8) + x] | (1 << (8-y))); + 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 << (8-y))); + rawLeds[(grid*8) + x] = (byte) (rawLeds[(grid*8) + x] | (1 << (7-y))); } } } @@ -477,6 +476,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 serialPorts = Arduino .getAvailableSerialPorts(); for (Iterator iter = serialPorts.iterator(); iter.hasNext();) { @@ -487,9 +493,8 @@ public class PunchingBag implements Runnable { System.out.println(comPort.getName()); } try { - ledArduino.connect(comPort.getName()); - // System.out.println("ID: " + buttonArduino.getID()); + System.out.println(comPort.getName()); } catch (Exception e) { e.printStackTrace(); } @@ -532,9 +537,12 @@ public class PunchingBag implements Runnable { } private void printByte(byte b) { + //System.out.println("Byte: " + b); String str; for (int j = 0; j < 8; j++) { - if ((b & (1 << (8 - j))) > 0) { + byte val = (byte) (b & (1 << (7-j))); + //System.out.println("Val: " + val + " " + (1 << (7-j))); + if (val > 0) { str = "1"; } else { str = "0"; @@ -587,7 +595,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++) { @@ -615,11 +623,13 @@ public class PunchingBag implements Runnable { } } calculateRawLeds(); - /*String str; + String str; for (int i = 0; i < (6 * 8); i++) { - printByte(rawLeds[i]); + //printByte(rawLeds[i]); } - System.out.println("");*/ + //System.out.println(""); + + //Arrays.fill(rawLeds, (byte) -42); if (ledArduino.out != null) { //calculateRawLeds(); try { diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index adf30b0..b62c820 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -48,7 +48,6 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, final boolean useDebugScreen = true; public static void main(String[] args) throws InterruptedException { - new PunchingBagGUI(); } @@ -535,14 +534,15 @@ 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); /*for (int bx=0; bx