diff options
Diffstat (limited to 'PunchingBag/src/PunchingBag.java')
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 110 |
1 files changed, 62 insertions, 48 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 00be6e0..7833eaf 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -58,7 +58,7 @@ public class PunchingBag implements Runnable { ledListenerList.add(LEDListener.class, l); } - public void addLEDChangeListener(AccelListener l) { + public void addAccelChangeListener(AccelListener l) { accelListenerList.add(AccelListener.class, l); } @@ -66,7 +66,7 @@ public class PunchingBag implements Runnable { return leds[x][y]; } - public boolean setLED(int x, int y, Colour colour) { + private boolean setLEDInternal(int x, int y, Colour colour) { if (x >= 0 && x < ledWidth && y >= 0 && y < ledHeight) { if (leds[x][y] != colour) { leds[x][y] = colour; @@ -78,8 +78,12 @@ public class PunchingBag implements Runnable { return false; } } + + public void setLED(int x, int y, Colour colour) { + runningEffects.add(new Point(x,y,colour)); + } - void CircleExpand(int x, int y, int intensity) { + void circleExpand(int x, int y, int intensity) { runningEffects.add(new CircleExpand(x, y, 100)); } @@ -95,6 +99,23 @@ public class PunchingBag implements Runnable { abstract public void draw(); } + + class Point extends Effect { + final int x; + final int y; + final Colour colour; + + public Point(int x, int y, Colour colour) { + this.x = x; + this.y = y; + this.colour = colour; + } + + + public void draw() { + setLEDInternal(x,y,colour); + } + } class Text extends Effect { final String string; @@ -148,16 +169,16 @@ public class PunchingBag implements Runnable { // longhand: currentRadius = currentRadius + 0.1 + (currentRadius / // 10); - if (!DrawEllipse(x, y, (int) currentRadius, (int) currentRadius, + if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, colour)) stop = true; } } - public boolean DrawEllipse(int x, int y, int radx, int rady, Colour colour) { + public boolean drawEllipse(int x, int y, int radx, int rady, Colour colour) { if (radx == 0 && rady == 0) { - return setLED(x, y, colour); + return setLEDInternal(x, y, colour); } boolean doneSomething = false; @@ -167,16 +188,16 @@ public class PunchingBag implements Runnable { int err = b2 - (2 * rady - 1) * a2, e2; /* error value in the first step */ do { - if (setLED(x + dx, y + dy, colour)) {/* I. Quadrant */ + if (setLEDInternal(x + dx, y + dy, colour)) {/* I. Quadrant */ doneSomething = true; } - if (setLED(x - dx, y + dy, colour)) {/* II. Quadrant */ + if (setLEDInternal(x - dx, y + dy, colour)) {/* II. Quadrant */ doneSomething = true; } - if (setLED(x - dx, y - dy, colour)) {/* III. Quadrant */ + if (setLEDInternal(x - dx, y - dy, colour)) {/* III. Quadrant */ doneSomething = true; } - if (setLED(x + dx, y - dy, colour)) {/* IV. Quadrant */ + if (setLEDInternal(x + dx, y - dy, colour)) {/* IV. Quadrant */ doneSomething = true; } @@ -192,10 +213,10 @@ public class PunchingBag implements Runnable { } while (dy >= 0); while (dx++ < radx) { /* correction for flat ellipses (b=1) */ - if (setLED(x + dx, y, colour)) { + if (setLEDInternal(x + dx, y, colour)) { doneSomething = true; } - if (setLED(x - dx, y, colour)) { + if (setLEDInternal(x - dx, y, colour)) { doneSomething = true; } } @@ -232,7 +253,7 @@ public class PunchingBag implements Runnable { } /* Clears the led grid and stops all running effects */ - void clearLEDGrid() { + private void clearLEDGrid() { /* * for (Iterator<Effect> iter = runningEffects.iterator(); * iter.hasNext();) { ((Effect) iter.next()).stop(); } @@ -260,44 +281,38 @@ public class PunchingBag implements Runnable { public void run() { while (true) { - synchronized (runningEffects) { // Should prevent - // ConcurrentModificationException's - // (havent managed to produce one - // though - for (Iterator<Effect> iter = runningEffects.iterator(); iter - .hasNext();) { - Effect ef = (Effect) iter.next(); - if (ef.stop) { - iter.remove(); - } else {// if ((ef.lastRefresh + (1000 / ef.refreshRate)) <= - // Systems.currentTimeMillis()) { - ef.draw(); - ef.lastRefresh = System.currentTimeMillis(); + synchronized (leds) { + clearLEDGrid(); + + synchronized (runningEffects) { // Should prevent + // ConcurrentModificationException's + // (havent managed to produce one + // though + for (Iterator<Effect> iter = runningEffects.iterator(); iter + .hasNext();) { + Effect ef = (Effect) iter.next(); + if (ef.stop) { + iter.remove(); + } else {// if ((ef.lastRefresh + (1000 / + // ef.refreshRate)) <= + // Systems.currentTimeMillis()) { + ef.draw(); + ef.lastRefresh = System.currentTimeMillis(); + } } } } - try { - ledArduino.write(((byte) 108)); - ledArduino.write(((byte) 101)); - ledArduino.write(rawLeds); - } catch (IOException e1) { - e1.printStackTrace(); - } - - try { - while (true) { - int value = ledArduino.read(); - if (value != -1) { - System.out.print(ledArduino.read()); - } else { - break; - } - } - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } + /* + * try { ledArduino.write(((byte) 108)); ledArduino.write(((byte) + * 101)); ledArduino.write(rawLeds); } catch (IOException e1) { + * e1.printStackTrace(); } + * + * try { while (true) { int value = ledArduino.read(); if (value != + * -1) { System.out.print(ledArduino.read()); } else { break; } } } + * catch (IOException e1) { // TODO Auto-generated catch block + * e1.printStackTrace(); } + */ if (ledsChanged) { LEDListener[] LEDListeners = ledListenerList @@ -328,7 +343,6 @@ public class PunchingBag implements Runnable { } // TODO: Hack? - clearLEDGrid(); try { Thread.sleep(1000 / 60); |