aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBag.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/PunchingBag.java')
-rw-r--r--PunchingBag/src/PunchingBag.java132
1 files changed, 95 insertions, 37 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java
index 723d373..941e4d7 100644
--- a/PunchingBag/src/PunchingBag.java
+++ b/PunchingBag/src/PunchingBag.java
@@ -56,7 +56,9 @@ public class PunchingBag implements Runnable {
private EventListenerList accelListenerList = new EventListenerList();
public PunchingBag() {
- new Thread(this).start();
+ Thread bagControlThread = new Thread(this);
+ bagControlThread.setPriority(Thread.MAX_PRIORITY);
+ bagControlThread.start();
}
/**
@@ -106,6 +108,14 @@ public class PunchingBag implements Runnable {
runningEffects.add(new SquareExpand(x, y, intensity, colour));
}
+ void fillRect(int x, int y, int width, int height, Colour colour, long time) {
+ runningEffects.add(new FillRect(x, y, width, height, colour, time));
+ }
+
+ void rect(int x, int y, int width, int height, Colour colour) {
+ runningEffects.add(new Rect(x, y, width, height, colour));
+ }
+
void noise(Rectangle rect, long time) {
runningEffects.add(new Noise(rect, System.currentTimeMillis() + time));
}
@@ -139,6 +149,51 @@ public class PunchingBag implements Runnable {
}
}
+ class FillRect extends Effect {
+ final int x;
+ final int y;
+ final int width;
+ final int height;
+ final Colour colour;
+ final long time;
+
+ public FillRect(int x, int y, int width, int height, Colour colour, long time) {
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+ this.colour = colour;
+ this.time = System.currentTimeMillis() + time;
+ }
+
+ public void draw() {
+ fillRectInternal(x, y, width, height, colour);
+ if (System.currentTimeMillis() >= time) {
+ stop();
+ }
+ }
+ }
+
+ class Rect extends Effect {
+ final int x;
+ final int y;
+ final int width;
+ final int height;
+ final Colour colour;
+
+ public Rect(int x, int y, int width, int height, Colour colour) {
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+ this.colour = colour;
+ }
+
+ public void draw() {
+ drawRectInternal(x, y, width, height, colour);
+ }
+ }
+
class Text extends Effect {
final String string;
final Area area;
@@ -300,7 +355,7 @@ public class PunchingBag implements Runnable {
return doneSomething;
}
- public boolean drawRectCorner(int x, int y, int height, int width,
+ public boolean drawRectInternal(int x, int y, int width, int height,
Colour colour) {
if (height < 0) {
height = 0;
@@ -315,12 +370,11 @@ public class PunchingBag implements Runnable {
boolean doneSomething = false;
int heightEx = 0;
int widthEx = 0;
-
+ boolean finished = true;
do {
if (setLEDInternal(x + widthEx, y, colour)) {
doneSomething = true;
}
-
if (setLEDInternal(x, y + heightEx, colour)) {
doneSomething = true;
}
@@ -331,12 +385,14 @@ public class PunchingBag implements Runnable {
doneSomething = true;
}
if (heightEx < height) {
+ finished = false;
heightEx++;
}
if (widthEx < width) {
- heightEx++;
+ finished = false;
+ widthEx++;
}
- } while (height >= 0 && width >= 0);
+ } while (!finished);
return doneSomething;
}
@@ -405,20 +461,18 @@ public class PunchingBag implements Runnable {
return drawnSomething;
}
- public boolean rectfill(int x, int y, int height, int width, Colour colour) {
- if (height < 0) {
- height = 0;
- }
- if (width < 0) {
- width = 0;
- }
- if (width == 0 && height == 0) {
- return setLEDInternal(x, y, colour);
+ public boolean fillRectInternal(int x, int y, int height, int width,
+ Colour colour) {
+ boolean doneSomething = false;
+ for (int px = x; px < (x + height); px++) {
+ for (int py = y; py < (y + width); py++) {
+ if (setLEDInternal(px, py, colour)) {
+ doneSomething = true;
+ }
+ }
}
- boolean doneSomething = false;
-
return doneSomething;
}
@@ -559,15 +613,15 @@ public class PunchingBag implements Runnable {
private void readButtonData(String data) {
if (data.replaceAll("[^0-9]", "").length() > 0) {
- // System.out.print(data);
+ System.out.print(data);
int num = Integer.valueOf(data.replaceAll("[^0-9]", ""));
- int x = (num % 9);
- if (num != 0)
- x--;
- int y = num / 9;
- // System.out.println("X: " + x + " Y: " + y);
- buttons[x][y] = true;
- buttonsChanged = true;
+ int x = ((num-1) % 8);
+ int y = (num-1) / 8;
+ System.out.println("X: " + x + " Y: " + y);
+ if (!(x < 0)) {
+ buttons[x][y] = true;
+ buttonsChanged = true;
+ }
}
}
@@ -592,19 +646,21 @@ public class PunchingBag implements Runnable {
}
public void run() {
- long timeToSleep = 0;
+ long timeToSleep = 10;
while (true) {
- System.out.println("Time since last refresh: "
- + (System.currentTimeMillis() - lastRefresh));
- if (timeToSleep > 0) {
- if ((System.currentTimeMillis() - lastRefresh) > (1000 / 60)) {
+ //System.out.println("Time since last refresh: "
+ // + (System.currentTimeMillis() - lastRefresh));
+
+ if ((System.currentTimeMillis() - lastRefresh) > (1000 / 60)) {
+ if (timeToSleep > 0) {
timeToSleep--;
- } else {
- timeToSleep++;
}
+ } else {
+ timeToSleep++;
}
- System.out.println("Sleeping: " + timeToSleep);
+
+ //System.out.println("Sleeping: " + timeToSleep);
lastRefresh = System.currentTimeMillis();
// System.out.println("R");
@@ -629,7 +685,8 @@ public class PunchingBag implements Runnable {
}
}
}
- System.out.println("Effects: " + (System.currentTimeMillis() - beginTimeForEffects));
+ //System.out.println("Effects: "
+ // + (System.currentTimeMillis() - beginTimeForEffects));
}
long beginTimeButtonIn = System.currentTimeMillis();
@@ -673,8 +730,9 @@ public class PunchingBag implements Runnable {
e.printStackTrace();
}
}
- System.out.println("Button: " + (System.currentTimeMillis() - beginTimeButtonIn));
-
+ //System.out.println("Button: "
+ // + (System.currentTimeMillis() - beginTimeButtonIn));
+
calculateRawLeds();
String str;
for (int i = 0; i < (6 * 8); i++) {
@@ -717,7 +775,7 @@ public class PunchingBag implements Runnable {
clearButtonGrid();
try {
- Thread.sleep(1000 / 60);
+ Thread.sleep(timeToSleep);
} catch (InterruptedException e) {
e.printStackTrace();
}