Browse Source

Can now send animations to cube.

Thomas Buck 12 years ago
parent
commit
51a4d18ae9

+ 1
- 1
CubeControl/AnimationUtility.java View File

@@ -106,7 +106,7 @@ public class AnimationUtility {
106 106
 	 * Get the last error that occured while writing
107 107
 	 * 
108 108
 	 * @return Text of the exception that occured
109
-	 * @see AnimationUtility#writeFile(String, ArrayList) writeFile()
109
+	 * @see AnimationUtility#writeFile(String, List) writeFile()
110 110
 	 */
111 111
 	public static String getLastError() {
112 112
 		String tmp = lastError;

+ 2
- 2
CubeControl/Frame.java View File

@@ -98,12 +98,12 @@ public class Frame extends JFrame implements ListSelectionListener {
98 98
 		}
99 99
 	}
100 100
 
101
-	private String askString(String title, String text) {
101
+	public String askString(String title, String text) {
102 102
 		return JOptionPane.showInputDialog(null, text, title,
103 103
 				JOptionPane.QUESTION_MESSAGE);
104 104
 	}
105 105
 
106
-	private void errorMessage(String s) {
106
+	public void errorMessage(String s) {
107 107
 		String[] Optionen = { "OK" };
108 108
 		JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION,
109 109
 				JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);

+ 1
- 1
CubeControl/HelperUtility.java View File

@@ -80,7 +80,7 @@ public class HelperUtility {
80 80
 	}
81 81
 
82 82
 	// http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
83
-	public static void fastChannelCopy(ReadableByteChannel src,
83
+	private static void fastChannelCopy(ReadableByteChannel src,
84 84
 			WritableByteChannel dest) throws IOException {
85 85
 		ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
86 86
 		while (src.read(buffer) != -1) {

+ 172
- 0
CubeControl/SerialHelper.java View File

@@ -0,0 +1,172 @@
1
+/*
2
+ * SerialHelper.java
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+ /**
24
+ * Implement commands of cube. You can only open one serial port.
25
+ * If you want to communicate with another port, close this one first!
26
+ * @author Thomas Buck
27
+ * @author Max Nuding
28
+ * @author Felix Bäder
29
+ * @version 1.0
30
+ */
31
+
32
+ import java.util.Date;
33
+
34
+public class SerialHelper {
35
+
36
+	private final short OK = 0x42;
37
+	private final short ERROR = 0x23;
38
+
39
+	/**
40
+	 * Create new SerialHelper.
41
+	 * @param serialPort Name of serial port to use.
42
+	 * @throws Exception Could not open serial port.
43
+	 */
44
+	public SerialHelper(String serialPort) throws Exception {
45
+		if (HelperUtility.openPort(serialPort) == false) {
46
+			throw new Exception("Could not open serial port \"" + serialPort + "\"");
47
+		}
48
+	}
49
+
50
+	/**
51
+	 * Recieve all animations saved in cube.
52
+	 * @return A cubeWorker populated with the new data or null.
53
+	 */
54
+	public cubeWorker getAnimationsFromCube() {
55
+		return null;
56
+	}
57
+
58
+	/**
59
+	 * Send all animations in a cubeWorker to the Cube.
60
+	 * @param worker cubeWorker that containts data.
61
+	 */
62
+	public void sendAnimationsToCube(cubeWorker worker) {
63
+		short[] data;
64
+		short[] tmp = new short[1];
65
+
66
+		// Send animation count
67
+		tmp[0] = (short)worker.numOfAnimations();
68
+		if (!writeData(tmp)) {
69
+			System.out.println("SerialHelper: Timeout numOfAnimations");
70
+			return;
71
+		}
72
+		data = readData(1);
73
+		if ((data != null) && (data[0] != OK)) {
74
+			System.out.println("SerialHelper: Response Error");
75
+			return;
76
+		}
77
+
78
+		// Send animations
79
+		for (int a = 0; a < worker.numOfAnimations(); a++) {
80
+			// Send frame count
81
+			tmp[0] = (short)worker.numOfFrames(a);
82
+			if (!writeData(tmp)) {
83
+				System.out.println("SerialHelper: Timeout numOfFrames");
84
+				return;
85
+			}
86
+			data = readData(1);
87
+			if ((data != null) && (data[0] != OK)) {
88
+				System.out.println("SerialHelper: Response Error");
89
+				return;
90
+			}
91
+
92
+			// Send frames
93
+			for (int f = 0; f < worker.numOfFrames(a); f++) {
94
+				// Frame duration
95
+				tmp[0] = worker.getFrameTime(a, f);
96
+				if (!writeData(tmp)) {
97
+					System.out.println("SerialHelper: Timeout Frame duration");
98
+					return;
99
+				}
100
+				data = readData(1);
101
+				if ((data != null) && (data[0] != OK)) {
102
+					System.out.println("SerialHelper: Response Error");
103
+					return;
104
+				}
105
+
106
+				// Frame data
107
+				if (!writeData(worker.getFrame(a, f))) {
108
+					System.out.println("SerialHelper: Timeout Frame");
109
+					return;
110
+				}
111
+				data = readData(1);
112
+				if ((data != null) && (data[0] != OK)) {
113
+					System.out.println("SerialHelper: Response Error");
114
+					return;
115
+				}
116
+			}
117
+		}
118
+
119
+		// Send finish
120
+		tmp = new short[4];
121
+		tmp[0] = OK;
122
+		tmp[1] = OK;
123
+		tmp[2] = OK;
124
+		tmp[3] = OK;
125
+		if (!writeData(tmp)) {
126
+			System.out.println("SerialHelper: Timeout Finish");
127
+			return;
128
+		}
129
+		data = readData(1);
130
+		if ((data != null) && (data[0] != OK)) {
131
+			System.out.println("SerialHelper: Response Error");
132
+			return;
133
+		}
134
+	}
135
+
136
+	/**
137
+	 * Close the serial port again.
138
+	 */
139
+	public void closeSerialPort() {
140
+		HelperUtility.closePort();
141
+	}
142
+
143
+	private boolean writeData(short[] data) {
144
+		// write data. return true if success
145
+		long startdate = (new Date()).getTime();
146
+
147
+		SerialWriteThread t = new SerialWriteThread(data);
148
+		t.start();
149
+		while (!t.dataWasSent()) {
150
+			if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
151
+				// More than (length * 1000) milliseconds went by
152
+				return false;
153
+			}
154
+		}
155
+		return true;
156
+	}
157
+
158
+	private short[] readData(int length) {
159
+		// return data read or null if timeout
160
+		long startdate = (new Date()).getTime();
161
+
162
+		SerialReadThread t = new SerialReadThread(length);
163
+		t.start();
164
+		while (!t.dataIsReady()) {
165
+			if ((new Date()).getTime() >= (startdate + (length * 1000))) {
166
+				// More than (length * 1000) milliseconds went by
167
+				return null;
168
+			}
169
+		}
170
+		return t.getSerialData();
171
+	}
172
+}

+ 75
- 0
CubeControl/SerialReadThread.java View File

@@ -0,0 +1,75 @@
1
+/*
2
+ * SerialReadThread.java
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+ /**
24
+ * Thread that reads data from an opened serial port.
25
+ * @author Thomas Buck
26
+ * @author Max Nuding
27
+ * @author Felix Bäder
28
+ * @version 1.0
29
+ */
30
+
31
+import java.lang.Thread;
32
+
33
+public class SerialReadThread extends Thread {
34
+
35
+	private short[] storage;
36
+	private int length;
37
+	private boolean dataReady = false;
38
+
39
+	private static int idIndex = 0;
40
+
41
+	/**
42
+	 * Create a SerialReadThread
43
+	 * @param length Amount of data to be read.
44
+	 */
45
+	public SerialReadThread(int length) {
46
+		this.length = length;
47
+		setName("Serial Reader " + idIndex++);
48
+	}
49
+
50
+	/**
51
+	 * Start getting data.
52
+	 */
53
+	public void run() {
54
+		storage = HelperUtility.readData(length);
55
+		dataReady = true;
56
+	}
57
+
58
+	/**
59
+	 * Check if all data was recieved.
60
+	 * 
61
+	 * @return TRUE if all data was recieved.
62
+	 */
63
+	public boolean dataIsReady() {
64
+		return dataReady;
65
+	}
66
+
67
+	/**
68
+	 * Get the serial data that was recieved.
69
+	 * 
70
+	 * @return Array with recieved bytes.
71
+	 */
72
+	public short[] getSerialData() {
73
+		return storage;
74
+	}
75
+}

+ 65
- 0
CubeControl/SerialWriteThread.java View File

@@ -0,0 +1,65 @@
1
+/*
2
+ * SerialWriteThread.java
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+ /**
24
+ * Thread that writes data to an opened serial port.
25
+ * @author Thomas Buck
26
+ * @author Max Nuding
27
+ * @author Felix Bäder
28
+ * @version 1.0
29
+ */
30
+
31
+import java.lang.Thread;
32
+
33
+public class SerialWriteThread extends Thread {
34
+
35
+	private short[] data;
36
+	private boolean dataSent = false;
37
+
38
+	private static int idIndex = 0;
39
+
40
+	/**
41
+	 * Create a SerialWriteThread
42
+	 * @param data Data to be written.
43
+	 */
44
+	public SerialWriteThread(short[] data) {
45
+		this.data = data;
46
+		setName("Serial Writer " + idIndex++);
47
+	}
48
+
49
+	/**
50
+	 * Start writing data.
51
+	 */
52
+	public void run() {
53
+		HelperUtility.writeData(data, data.length);
54
+		dataSent = true;
55
+	}
56
+
57
+	/**
58
+	 * Check if all data was sent.
59
+	 * 
60
+	 * @return TRUE if all data was sent.
61
+	 */
62
+	public boolean dataWasSent() {
63
+		return dataSent;
64
+	}
65
+}

+ 17
- 7
CubeControl/cubeWorker.java View File

@@ -54,7 +54,7 @@ public class cubeWorker {
54 54
 	// --------------------
55 55
 
56 56
 	private List<Animation> animations = new ArrayList<Animation>();
57
-	private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
57
+	private final int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
58 58
 	private boolean changedState = false;
59 59
 
60 60
 	// --------------------
@@ -67,7 +67,6 @@ public class cubeWorker {
67 67
 		animations.get(0).setName("Animation 1");
68 68
 		animations.get(0).add(0);
69 69
 		animations.get(0).get(0).setName("Frame 1");
70
-		framesRemaining--;
71 70
 	}
72 71
 
73 72
 	/**
@@ -79,6 +78,19 @@ public class cubeWorker {
79 78
 		animations = anims;
80 79
 	}
81 80
 
81
+	/**
82
+	 * Returns number of frames in this cubeWorker.
83
+	 *
84
+	 * @return number of frames.
85
+	 */
86
+	public int completeNumOfFrames() {
87
+		int count = 0;
88
+		for (int i = 0; i < numOfAnimations(); i++) {
89
+			count += numOfFrames(i);
90
+		}
91
+		return count;
92
+	}
93
+
82 94
 	// --------------------
83 95
 	// Misc. Methods
84 96
 	// --------------------
@@ -108,7 +120,7 @@ public class cubeWorker {
108 120
 	 * @return number of frames remaining
109 121
 	 */
110 122
 	public int framesRemaining() {
111
-		return framesRemaining;
123
+		return framesRemaining - completeNumOfFrames();
112 124
 	}
113 125
 
114 126
 	// --------------------
@@ -122,7 +134,7 @@ public class cubeWorker {
122 134
 	 */
123 135
 	public int addAnimation() {
124 136
 		changedState = true;
125
-		if (framesRemaining <= 0) {
137
+		if (framesRemaining() <= 0) {
126 138
 			return -1;
127 139
 		} else {
128 140
 			int s = animations.size();
@@ -223,10 +235,9 @@ public class cubeWorker {
223 235
 	 */
224 236
 	public int addFrame(int anim) {
225 237
 		changedState = true;
226
-		if (framesRemaining <= 0) {
238
+		if (framesRemaining() <= 0) {
227 239
 			return -1;
228 240
 		}
229
-		framesRemaining--;
230 241
 		int s = animations.get(anim).size();
231 242
 		animations.get(anim).add(s);
232 243
 		animations.get(anim).get(s)
@@ -344,7 +355,6 @@ public class cubeWorker {
344 355
 		for (int i = 0; i < animations.size(); i++) {
345 356
 			size += animations.get(i).size();
346 357
 		}
347
-		framesRemaining = 2016 - size;
348 358
 		if (size > 2016) {
349 359
 			return -1;
350 360
 		}

+ 1
- 1
CubeControl/makefile View File

@@ -16,7 +16,7 @@ endif
16 16
 
17 17
 # All java files to be compiled
18 18
 # List so it works as target
19
-JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java
19
+JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java SerialReadThread.java SerialWriteThread.java SerialHelper.java
20 20
 
21 21
 # --------------------------------------
22 22
 

Loading…
Cancel
Save