aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java159
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/examples/Keyboard.java14
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java9
3 files changed, 109 insertions, 73 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
index 7477f0c..4861ad9 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java
@@ -8,13 +8,18 @@ import java.awt.Color;
import java.awt.Rectangle;
import java.awt.geom.Area;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.Writer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Properties;
/**
* This class represents the punching bag, reads and sends data to and from the
@@ -25,6 +30,11 @@ import java.util.Iterator;
* @author Adam Martindale <awiamartindale@googlemail.com>
*/
public class PunchingBag implements Runnable {
+
+ private String configDirectory = System.getProperty("user.dir") + System.getProperty("file.separator") + ".punchbag";
+ private File configDir = new File(configDirectory);
+ private File preferencesFile = new File(configDirectory + System.getProperty("file.separator") + "preferences");
+ private Properties preferences = new Properties();
/**
* Used by the run method to decide if to call the LEDListeners
*/
@@ -118,7 +128,7 @@ public class PunchingBag implements Runnable {
* linux)
*/
String ledArduinoDeviceAddress = "COM3"; // TODO: This should be cached
- // localy
+ // localy
/**
* Turn on to activate the command line messages regarding the run loop
@@ -165,11 +175,57 @@ public class PunchingBag implements Runnable {
* Private constructor, starts the run method.
*/
private PunchingBag() {
+ loadConfig();
Thread bagControlThread = new Thread(this);
bagControlThread.setPriority(Thread.MAX_PRIORITY);
bagControlThread.start();
}
+ private boolean loadConfig() {
+ setDefaultConfig();
+ if (configDir.exists()) {
+ if (configDir.isDirectory()) {
+ if (configDir.canRead()) {
+ try {
+ preferences.load(this.getClass().getClassLoader().getResourceAsStream(configDir));
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ } else {
+ System.err.println("The config directrory (" + configDir
+ + ") exists but cant be read by this application, falling back to hard coded values.");
+ }
+ } else {
+ System.err.println("Weird, the config directrory (" + configDir + ") is not a directory, perhaps delete it?");
+ }
+ } else {
+ System.out.println("Config directory " + configDir + " does not exist, creating using default hardcoded values");
+ createDefaultConfig();
+ }
+
+ return true;
+ }
+
+ private void setDefaultConfig() {
+ preferences.setProperty("buttonArduinoDeviceAddress", buttonArduinoDeviceAddress);
+ preferences.setProperty("ledArduinoDeviceAddress", ledArduinoDeviceAddress);
+ }
+
+ private boolean createDefaultConfig() {
+ setDefaultConfig();
+ try {
+ preferences.store(new FileOutputStream(configDir), "Default hardcoded preferences file created automaticaly");
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return true;
+ }
+
/**
* Adds an <code>ButtonListener</code> to the bag.
*
@@ -232,12 +288,11 @@ public class PunchingBag implements Runnable {
*/
private boolean setLEDInternal(int x, int y, Color colour) {
if (x >= 0 && x < ledWidth && y >= 0 && y < ledHeight) { // Check if the
- // led being
- // set
- // exists
+ // led being
+ // set
+ // exists
if (leds[x][y] != colour) { // Check if the led is being changed
- if (!(colour == Color.red) && !(colour == Color.green)
- && !(colour == Color.yellow)) {
+ if (!(colour == Color.red) && !(colour == Color.green) && !(colour == Color.yellow)) {
if (leds[x][y] != Color.white) {
leds[x][y] = Color.white;
ledsChanged = true;
@@ -312,8 +367,7 @@ public class PunchingBag implements Runnable {
* @param colour
* @param time
*/
- public void fillRect(int x, int y, int width, int height, Color colour,
- long time) {
+ public void fillRect(int x, int y, int width, int height, Color colour, long time) {
runningEffects.add(new FillRect(x, y, width, height, colour, time));
}
@@ -406,8 +460,7 @@ public class PunchingBag implements Runnable {
final Color colour;
final long time;
- public FillRect(int x, int y, int width, int height, Color colour,
- long time) {
+ public FillRect(int x, int y, int width, int height, Color colour, long time) {
this.x = x;
this.y = y;
this.width = width;
@@ -565,8 +618,7 @@ public class PunchingBag implements Runnable {
}
}
- public boolean drawRectCenter(int x, int y, int height, int width,
- Color colour) {
+ public boolean drawRectCenter(int x, int y, int height, int width, Color colour) {
if (height < 0) {
height = 0;
}
@@ -582,20 +634,16 @@ public class PunchingBag implements Runnable {
int heightEx = 0;
do {
- if (setLEDInternal((x - (height / 2)) + widthEx, y - (height / 2),
- colour)) {
+ if (setLEDInternal((x - (height / 2)) + widthEx, y - (height / 2), colour)) {
doneSomething = true;
}
- if (setLEDInternal(x - (height / 2), (y - (height / 2)) + heightEx,
- colour)) {
+ if (setLEDInternal(x - (height / 2), (y - (height / 2)) + heightEx, colour)) {
doneSomething = true;
}
- if (setLEDInternal((x - (height / 2)) + widthEx, y + (height / 2),
- colour)) {
+ if (setLEDInternal((x - (height / 2)) + widthEx, y + (height / 2), colour)) {
doneSomething = true;
}
- if (setLEDInternal(x + (height / 2), (y - (height / 2)) + heightEx,
- colour)) {
+ if (setLEDInternal(x + (height / 2), (y - (height / 2)) + heightEx, colour)) {
doneSomething = true;
}
if (heightEx < height) {
@@ -610,8 +658,7 @@ public class PunchingBag implements Runnable {
return doneSomething;
}
- public boolean drawRectInternal(int x, int y, int width, int height,
- Color colour) {
+ public boolean drawRectInternal(int x, int y, int width, int height, Color colour) {
if (height < 0) {
height = 0;
}
@@ -716,8 +763,7 @@ public class PunchingBag implements Runnable {
return drawnSomething;
}
- public boolean fillRectInternal(int x, int y, int height, int width,
- Color colour) {
+ public boolean fillRectInternal(int x, int y, int height, int width, Color colour) {
boolean doneSomething = false;
for (int px = x; px < (x + height); px++) {
@@ -740,16 +786,12 @@ public class PunchingBag implements Runnable {
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)] == Color.green
- || leds[1 + x][(grid * 4) + (y / 2)] == Color.yellow) {
- rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8)
- + x] | (1 << (7 - y)));
+ if (leds[1 + x][(grid * 4) + (y / 2)] == Color.green || leds[1 + x][(grid * 4) + (y / 2)] == Color.yellow) {
+ rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8) + x] | (1 << (7 - y)));
}
} else {
- if (leds[1 + x][(grid * 4) + (y / 2)] == Color.red
- || leds[1 + x][(grid * 4) + (y / 2)] == Color.yellow) {
- rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8)
- + x] | (1 << (7 - y)));
+ if (leds[1 + x][(grid * 4) + (y / 2)] == Color.red || leds[1 + x][(grid * 4) + (y / 2)] == Color.yellow) {
+ rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8) + x] | (1 << (7 - y)));
}
}
}
@@ -872,8 +914,7 @@ public class PunchingBag implements Runnable {
*/
private boolean findArduinos() {
// TODO: Detect OS and continue appropiately
- HashSet<CommPortIdentifier> serialPorts = Arduino
- .getAvailableSerialPorts();
+ HashSet<CommPortIdentifier> serialPorts = Arduino.getAvailableSerialPorts();
for (Iterator iter = serialPorts.iterator(); iter.hasNext();) {
CommPortIdentifier comPort = (CommPortIdentifier) iter.next();
// HACK FOR WINDOWS TO IDENIFY PORTS LIKELY TO CONTAIN ARDUINO
@@ -899,8 +940,7 @@ public class PunchingBag implements Runnable {
String[] nums = data.split(" ");
for (int x = 1; x < nums.length; x++) {
// Regex expression to strip newline at end (normally just 4th)
- if (nums[x].replaceAll("[^0-9]", "").length() == 0
- || nums[x].replaceAll("[^0-9]", "").length() > 4) {
+ if (nums[x].replaceAll("[^0-9]", "").length() == 0 || nums[x].replaceAll("[^0-9]", "").length() > 4) {
System.err.println("Accel Data Error: " + data);
continue;
}
@@ -982,16 +1022,13 @@ public class PunchingBag implements Runnable {
*/
public void run() {
long timeToSleep = 10; // The time slept at the end of the loop (to
- // maintain the refresh rate)
+ // maintain the refresh rate)
while (true) {
if (debugTimings) {
- System.out.println("Time since last refresh: "
- + (System.currentTimeMillis() - lastRefresh));
+ System.out.println("Time since last refresh: " + (System.currentTimeMillis() - lastRefresh));
if ((System.currentTimeMillis() - lastRefresh) != 0)
- System.out
- .println("FPS: "
- + (1000 / (System.currentTimeMillis() - lastRefresh)));
+ System.out.println("FPS: " + (1000 / (System.currentTimeMillis() - lastRefresh)));
}
if ((System.currentTimeMillis() - lastRefresh) > (1000 / 60)) {
@@ -1014,8 +1051,7 @@ public class PunchingBag implements Runnable {
// ConcurrentModificationException's
// (havent managed to produce one
// though
- for (Iterator<Effect> iter = runningEffects.iterator(); iter
- .hasNext();) {
+ for (Iterator<Effect> iter = runningEffects.iterator(); iter.hasNext();) {
Effect ef = (Effect) iter.next();
if (ef.stop) {
iter.remove();
@@ -1046,8 +1082,7 @@ public class PunchingBag implements Runnable {
System.out.println("Outside " + (char) read);
if ((char) read == 'B') {
if (debugSerial)
- System.out
- .println(" Got an B, begining reading the button data");
+ System.out.println(" Got an B, begining reading the button data");
String str = "";
// StringBuilder sb = new StringBuilder(20);
while (true) {
@@ -1056,21 +1091,17 @@ public class PunchingBag implements Runnable {
break;
str = str + String.valueOf((char) read);
if (debugSerial)
- System.out.println("Reading button data: "
- + (char) read);
+ System.out.println("Reading button data: " + (char) read);
// sb.append((char) read);
}
if (debugSerial)
- System.out
- .print("Finished reading button data because of newline "
- + (char) read);
+ System.out.print("Finished reading button data because of newline " + (char) read);
// System.out.println("");
doneB = true;
readButtonData(str);
} else if ((char) read == 'A') {
if (debugSerial)
- System.out
- .println(" Got an A, begining reading the accel data");
+ System.out.println(" Got an A, begining reading the accel data");
String str = "";
// StringBuilder sb = new StringBuilder(20);
while (true) {
@@ -1079,8 +1110,7 @@ public class PunchingBag implements Runnable {
break;
str = str + String.valueOf((char) read);
if (debugSerial)
- System.out.println("Reading accel data "
- + (char) read);
+ System.out.println("Reading accel data " + (char) read);
// sb.append((char) read);
}
// System.out.println("");
@@ -1100,8 +1130,7 @@ public class PunchingBag implements Runnable {
}
}
if (debugTimings)
- System.out.println("Button: "
- + (System.currentTimeMillis() - beginTimeButtonIn));
+ System.out.println("Button: " + (System.currentTimeMillis() - beginTimeButtonIn));
calculateRawLeds();
// String str;
@@ -1122,38 +1151,32 @@ public class PunchingBag implements Runnable {
}
}
if (debugTimings)
- System.out.println("Leds: "
- + (System.currentTimeMillis() - beginTimeLedOut));
+ System.out.println("Leds: " + (System.currentTimeMillis() - beginTimeLedOut));
// Arrays.fill(rawLeds, (byte) -42);
long beginTimeListeners = System.currentTimeMillis();
if (ledsChanged) {
- LEDListener[] LEDListeners = ledListenerList
- .getListeners(LEDListener.class);
+ LEDListener[] LEDListeners = ledListenerList.getListeners(LEDListener.class);
for (int i = 0; i < ledListenerList.getListenerCount(); i++) {
LEDListeners[i].LEDchanged();
}
}
if (buttonsChanged) {
- ButtonListener[] buttonListeners = buttonListenerList
- .getListeners(ButtonListener.class);
+ ButtonListener[] buttonListeners = buttonListenerList.getListeners(ButtonListener.class);
for (int i = 0; i < buttonListenerList.getListenerCount(); i++) {
for (int x = 0; x < buttonWidth; x++) {
for (int y = 0; y < buttonHeight; y++) {
if (buttons[x][y]) {
buttonListeners[i].buttonPressed(x, y);
- buttonListeners[i].contact(new Contact(System
- .currentTimeMillis(), x, y,
- bottomAccelX));
+ buttonListeners[i].contact(new Contact(System.currentTimeMillis(), x, y, bottomAccelX));
}
}
}
}
}
if (debugTimings)
- System.out.println("Listeners: "
- + (System.currentTimeMillis() - beginTimeListeners));
+ System.out.println("Listeners: " + (System.currentTimeMillis() - beginTimeListeners));
clearButtonGrid();
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/Keyboard.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/Keyboard.java
index b502748..14d29a1 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/examples/Keyboard.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/Keyboard.java
@@ -79,7 +79,10 @@ public class Keyboard implements Runnable, ButtonListener {
e.printStackTrace();
}
}*/
- } catch (InvalidMidiDataException | MidiUnavailableException e) {
+ } catch (InvalidMidiDataException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@@ -97,8 +100,13 @@ public class Keyboard implements Runnable, ButtonListener {
try {
playKey(y - 10);
- } catch (InvalidMidiDataException | MidiUnavailableException
- | IOException e) {
+ } catch (InvalidMidiDataException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (MidiUnavailableException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
index 245ed34..2dd7cde 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
@@ -101,8 +101,13 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
bag.fillRect(0, 15, 9, 4, Color.red, 500);
playKey(0);
}
- } catch (InvalidMidiDataException | MidiUnavailableException
- | IOException e) {
+ } catch (InvalidMidiDataException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (MidiUnavailableException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}