From 2ecbb91c28b33142afb69e44f493e3bfc4fafac8 Mon Sep 17 00:00:00 2001 From: Adam Martindale Date: Fri, 2 Sep 2011 15:52:27 +0100 Subject: Finalized circleExpand, setLED check for legitimacy. --- PunchingBag/src/PunchingBag.java | 74 +++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 32 deletions(-) (limited to 'PunchingBag/src') diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 8408225..aa8a632 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -7,11 +7,13 @@ import java.util.Iterator; // TODO: Loads of threads in this class, make sure everything is thread safe. public class PunchingBag implements Runnable { + final public int ledHeight = 20; + final public int ledWidth = 9; private byte[] rawLeds = new byte[6 * 8]; - private Colour[][] leds = new Colour[9][20]; + private Colour[][] leds = new Colour[ledWidth][ledHeight]; private byte[] ledGridIntensities = new byte[6]; private boolean[][] buttons = new boolean[8][19]; - + private ArrayList runningEffects = new ArrayList(); enum Colour { @@ -39,13 +41,18 @@ public class PunchingBag implements Runnable { ledListenerList.add(LEDListener.class, l); } - public void setLED(int i, int j, Colour colour) { - leds[i][j] = colour; + public boolean setLED(int x, int y, Colour colour) { + if (x >= 0 && x < ledWidth && y >= 0 && y < ledHeight) { + leds[x][y] = colour; + return true; + } else { + return false; + } } void CircleExpand(int x, int y, int delay, Colour colour) { // TODO: Adam correct CircleExpand constructor, or this method? - //runningEffects.add(new CircleExpand(x,y,delay,colour)); + // runningEffects.add(new CircleExpand(x,y,delay,colour)); } class Effect implements Runnable { @@ -73,12 +80,13 @@ public class PunchingBag implements Runnable { } class CircleExpand extends Effect { - double currentWidth; final byte x; final byte y; byte intensity; boolean drawnSomething = true; + float currentRadius = 0; + public CircleExpand(byte x, byte y, byte intensity) { this.x = x; this.y = y; @@ -97,42 +105,39 @@ public class PunchingBag implements Runnable { colour = Colour.Green; } - drawnSomething = false; - - for (double i = 0; i < 360; i++) { - // int x = x + (int) Math.round(currentWidth * Math.cos(i)); - // int y = y + (int) Math.round(currentWidth * Math.sin(i)); - - if (x >= 0 && x <= 8 && y >= 0 && y <= 19) { - setLED(x, y, colour); - drawnSomething = true; - } - - } - - currentWidth += 0.1 + (currentWidth / 10); + currentRadius += 0.1 + (currentRadius / 10); + // longhand: currentRadius = currentRadius + 0.1 + (currentRadius / 10); - if (!drawnSomething) + if (!DrawEllipse(2, 4, (int) currentRadius, (int) currentRadius, colour)) stop = true; } } - public void 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) { - setLED(x, y, colour); - return; + return setLED(x, y, colour); } + boolean doneSomething = false; + int dx = 0, dy = rady; /* first quadrant from top left to bottom right */ int a2 = radx * radx, b2 = rady * rady; int err = b2 - (2 * rady - 1) * a2, e2; /* error value in the first step */ do { - setLED(x + dx, y + dy, colour); /* I. Quadrant */ - setLED(x - dx, y + dy, colour); /* II. Quadrant */ - setLED(x - dx, y - dy, colour); /* III. Quadrant */ - setLED(x + dx, y - dy, colour); /* IV. Quadrant */ + if (setLED(x + dx, y + dy, colour)) {/* I. Quadrant */ + doneSomething = true; + } + if (setLED(x - dx, y + dy, colour)) {/* II. Quadrant */ + doneSomething = true; + } + if (setLED(x - dx, y - dy, colour)) {/* III. Quadrant */ + doneSomething = true; + } + if (setLED(x + dx, y - dy, colour)) {/* IV. Quadrant */ + doneSomething = true; + } e2 = 2 * err; if (e2 < (2 * dx + 1) * b2) { @@ -146,9 +151,14 @@ public class PunchingBag implements Runnable { } while (dy >= 0); while (dx++ < radx) { /* correction for flat ellipses (b=1) */ - setLED(x + dx, y, colour); - setLED(x - dx, y, colour); + if (setLED(x + dx, y, colour)) { + doneSomething = true; + } + if (setLED(x - dx, y, colour)) { + doneSomething = true; + } } + return doneSomething; } private void calculateRawLeds() { @@ -187,7 +197,7 @@ public class PunchingBag implements Runnable { /* Clears the led grid and stops all running effects */ public void clearLEDGrid() { - for (Iterator iter = runningEffects.iterator(); iter.hasNext(); ) { + for (Iterator iter = runningEffects.iterator(); iter.hasNext();) { ((Effect) iter.next()).stop(); } Arrays.fill(leds, Boolean.FALSE); @@ -196,7 +206,7 @@ public class PunchingBag implements Runnable { public void setLEDGridIntensity(byte grid, byte intensity) { ledGridIntensities[grid] = intensity; } - + public void setLEDIntensities(byte intensity) { Arrays.fill(ledGridIntensities, intensity); } -- cgit v1.2.3