Browse Source

Upload download

Thomas Buck 12 years ago
parent
commit
ec613daa68

+ 3
- 1
CubeControl/AFrame.java View File

@@ -61,7 +61,9 @@ public class AFrame {
61 61
 	 * @param d 64 bytes that contain data (8 bit per byte, so 8 LEDs)
62 62
 	 */
63 63
 	public void setData(short[] d) {
64
-		data = d;
64
+		for (int i = 0; i < 64; i++) {
65
+			data[i] = d[i];
66
+		}
65 67
 	}
66 68
 
67 69
 	/**

+ 9
- 3
CubeControl/Frame.java View File

@@ -81,7 +81,7 @@ public class Frame extends JFrame implements ListSelectionListener {
81 81
 	private JButton playAnimationFullscreen = new JButton();
82 82
 	// Ende Attribute
83 83
 
84
-	public cubeWorker worker = new cubeWorker();
84
+	public cubeWorker worker = new cubeWorker(this);
85 85
 	private boolean fileSelected = false;
86 86
 
87 87
 	// Ende Variablen
@@ -110,6 +110,12 @@ public class Frame extends JFrame implements ListSelectionListener {
110 110
 				JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
111 111
 	}
112 112
 
113
+	public void errorMessage(String title, String msg) {
114
+		String[] Optionen = { "OK" };
115
+		JOptionPane.showOptionDialog(this, msg, title, JOptionPane.YES_OPTION,
116
+				JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
117
+	}
118
+
113 119
 	public void valueChanged(ListSelectionEvent evt) {
114 120
 		if ((!evt.getValueIsAdjusting())
115 121
 				&& ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
@@ -965,8 +971,8 @@ public class Frame extends JFrame implements ListSelectionListener {
965 971
 
966 972
 				if (s.startsWith("send ")) {
967 973
 					s = s.substring(5);
968
-					HelperUtility.openPort(((String) f.jComboBox1
969
-							.getSelectedItem()) + "\n");
974
+					HelperUtility.openPort((String) f.jComboBox1
975
+							.getSelectedItem());
970 976
 					short[] dat = toShortArray(s);
971 977
 					HelperUtility.writeData(dat, dat.length);
972 978
 					HelperUtility.closePort();

+ 143
- 31
CubeControl/SerialHelper.java View File

@@ -29,22 +29,28 @@
29 29
  * @version 1.0
30 30
  */
31 31
 
32
- import java.util.Date;
32
+import java.util.Date;
33
+import java.util.ArrayList;
34
+import java.util.List;
33 35
 
34 36
 public class SerialHelper {
35 37
 
36 38
 	private final short OK = 0x42;
37 39
 	private final short ERROR = 0x23;
38 40
 
41
+	private Frame frame;
42
+
39 43
 	/**
40 44
 	 * Create new SerialHelper.
41 45
 	 * @param serialPort Name of serial port to use.
46
+	 * @param frame Frame to show error messages 
42 47
 	 * @throws Exception Could not open serial port.
43 48
 	 */
44
-	public SerialHelper(String serialPort) throws Exception {
49
+	public SerialHelper(String serialPort, Frame frame) throws Exception {
45 50
 		if (HelperUtility.openPort(serialPort) == false) {
46 51
 			throw new Exception("Could not open serial port \"" + serialPort + "\"");
47 52
 		}
53
+		this.frame = frame;
48 54
 	}
49 55
 
50 56
 	/**
@@ -52,27 +58,127 @@ public class SerialHelper {
52 58
 	 * @return A cubeWorker populated with the new data or null.
53 59
 	 */
54 60
 	public cubeWorker getAnimationsFromCube() {
55
-		return null;
61
+		List<Animation> animations = new ArrayList<Animation>();
62
+		int animationCount, frameCount;
63
+		short[] data, tmp = new short[1];
64
+
65
+		// Send download command
66
+		tmp[0] = 'g';
67
+		if (!writeData(tmp)) {
68
+			printErrorMessage("Timout Command");
69
+			return null;
70
+		}
71
+		data = readData(1);
72
+		if ((data == null) || (data[0] != OK)) {
73
+			printErrorMessage("Response Error");
74
+			return null;
75
+		}
76
+
77
+		// Get animation count
78
+		data = readData(1);
79
+		if (data == null) {
80
+			printErrorMessage("Response Error");
81
+			return null;
82
+		} else {
83
+			animationCount = data[0];
84
+		}
85
+		tmp[0] = OK;
86
+		if (!writeData(tmp)) {
87
+			printErrorMessage("Timout Response");
88
+			return null;
89
+		}
90
+
91
+		// Get animations
92
+		for (int a = 0; a < animationCount; a++) {
93
+			Animation currentAnim = new Animation();
94
+
95
+			// Get number of frames
96
+			data = readData(1);
97
+			if (data == null) {
98
+				printErrorMessage("Response Error");
99
+				return null;
100
+			} else {
101
+				frameCount = data[0];
102
+			}
103
+			tmp[0] = OK;
104
+			if (!writeData(tmp)) {
105
+				printErrorMessage("Timout Response");
106
+				return null;
107
+			}
108
+
109
+			// Get frames
110
+			for (int f = 0; f < frameCount; f++) {
111
+				AFrame currentFrame = new AFrame();
112
+
113
+				// Get frame duration
114
+				data = readData(1);
115
+				if (data == null) {
116
+					printErrorMessage("Response Error");
117
+					return null;
118
+				} else {
119
+					currentFrame.setTime(data[0]);
120
+				}
121
+				tmp[0] = OK;
122
+				if (!writeData(tmp)) {
123
+					printErrorMessage("Timout Response");
124
+					return null;
125
+				}
126
+
127
+				// Get frame data
128
+				data = readData(64);
129
+				if (data == null) {
130
+					printErrorMessage("Response Error");
131
+					return null;
132
+				} else {
133
+					currentFrame.setData(data);
134
+				}
135
+				tmp[0] = OK;
136
+				if (!writeData(tmp)) {
137
+					printErrorMessage("Timout Response");
138
+					return null;
139
+				}
140
+
141
+				// Add frame to animation
142
+				currentAnim.add(f, currentFrame);
143
+			}
144
+
145
+			// Add animation to animations list
146
+			animations.add(a, currentAnim);
147
+		}
148
+
149
+		return new cubeWorker(animations, frame);
56 150
 	}
57 151
 
58 152
 	/**
59 153
 	 * Send all animations in a cubeWorker to the Cube.
60 154
 	 * @param worker cubeWorker that containts data.
155
+	 * @return 0 on success. -1 on error.
61 156
 	 */
62
-	public void sendAnimationsToCube(cubeWorker worker) {
63
-		short[] data;
64
-		short[] tmp = new short[1];
157
+	public int sendAnimationsToCube(cubeWorker worker) {
158
+		short[] data, tmp = new short[1];
159
+
160
+		// Send upload command
161
+		tmp[0] = 's';
162
+		if (!writeData(tmp)) {
163
+			printErrorMessage("Timout Command");
164
+			return -1;
165
+		}
166
+		data = readData(1);
167
+		if ((data == null) || (data[0] != OK)) {
168
+			printErrorMessage("Response Error");
169
+			return -1;
170
+		}
65 171
 
66 172
 		// Send animation count
67 173
 		tmp[0] = (short)worker.numOfAnimations();
68 174
 		if (!writeData(tmp)) {
69
-			System.out.println("SerialHelper: Timeout numOfAnimations");
70
-			return;
175
+			printErrorMessage("Timeout numOfAnimations");
176
+			return -1;
71 177
 		}
72 178
 		data = readData(1);
73
-		if ((data != null) && (data[0] != OK)) {
74
-			System.out.println("SerialHelper: Response Error");
75
-			return;
179
+		if ((data == null) || (data[0] != OK)) {
180
+			printErrorMessage("Response Error");
181
+			return -1;
76 182
 		}
77 183
 
78 184
 		// Send animations
@@ -80,13 +186,13 @@ public class SerialHelper {
80 186
 			// Send frame count
81 187
 			tmp[0] = (short)worker.numOfFrames(a);
82 188
 			if (!writeData(tmp)) {
83
-				System.out.println("SerialHelper: Timeout numOfFrames");
84
-				return;
189
+				printErrorMessage("Timeout numOfFrames");
190
+				return -1;
85 191
 			}
86 192
 			data = readData(1);
87
-			if ((data != null) && (data[0] != OK)) {
88
-				System.out.println("SerialHelper: Response Error");
89
-				return;
193
+			if ((data == null) || (data[0] != OK)) {
194
+				printErrorMessage("Response Error");
195
+				return -1;
90 196
 			}
91 197
 
92 198
 			// Send frames
@@ -94,24 +200,24 @@ public class SerialHelper {
94 200
 				// Frame duration
95 201
 				tmp[0] = worker.getFrameTime(a, f);
96 202
 				if (!writeData(tmp)) {
97
-					System.out.println("SerialHelper: Timeout Frame duration");
98
-					return;
203
+					printErrorMessage("Timeout Frame duration");
204
+					return -1;
99 205
 				}
100 206
 				data = readData(1);
101
-				if ((data != null) && (data[0] != OK)) {
102
-					System.out.println("SerialHelper: Response Error");
103
-					return;
207
+				if ((data == null) || (data[0] != OK)) {
208
+					printErrorMessage("Response Error");
209
+					return -1;
104 210
 				}
105 211
 
106 212
 				// Frame data
107 213
 				if (!writeData(worker.getFrame(a, f))) {
108
-					System.out.println("SerialHelper: Timeout Frame");
109
-					return;
214
+					printErrorMessage("Timeout Frame");
215
+					return -1;
110 216
 				}
111 217
 				data = readData(1);
112
-				if ((data != null) && (data[0] != OK)) {
113
-					System.out.println("SerialHelper: Response Error");
114
-					return;
218
+				if ((data == null) || (data[0] != OK)) {
219
+					printErrorMessage("Response Error");
220
+					return -1;
115 221
 				}
116 222
 			}
117 223
 		}
@@ -123,14 +229,15 @@ public class SerialHelper {
123 229
 		tmp[2] = OK;
124 230
 		tmp[3] = OK;
125 231
 		if (!writeData(tmp)) {
126
-			System.out.println("SerialHelper: Timeout Finish");
127
-			return;
232
+			printErrorMessage("Timeout Finish");
233
+			return -1;
128 234
 		}
129 235
 		data = readData(1);
130
-		if ((data != null) && (data[0] != OK)) {
131
-			System.out.println("SerialHelper: Response Error");
132
-			return;
236
+		if ((data == null) || (data[0] != OK)) {
237
+			printErrorMessage("Response Error");
238
+			return -1;
133 239
 		}
240
+		return 0;
134 241
 	}
135 242
 
136 243
 	/**
@@ -140,6 +247,11 @@ public class SerialHelper {
140 247
 		HelperUtility.closePort();
141 248
 	}
142 249
 
250
+	private void printErrorMessage(String s) {
251
+		System.out.println("SerialHelper: " + s);
252
+		frame.errorMessage("Serial Error", s);
253
+	}
254
+
143 255
 	private boolean writeData(short[] data) {
144 256
 		// write data. return true if success
145 257
 		long startdate = (new Date()).getTime();

+ 38
- 6
CubeControl/cubeWorker.java View File

@@ -56,17 +56,19 @@ public class cubeWorker {
56 56
 	private List<Animation> animations = new ArrayList<Animation>();
57 57
 	private final int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
58 58
 	private boolean changedState = false;
59
+	private Frame parentFrame;
59 60
 
60 61
 	// --------------------
61 62
 
62 63
 	/**
63 64
 	 * Creates a worker with one animation, containing an empty frame.
64 65
 	 */
65
-	cubeWorker() {
66
+	public cubeWorker(Frame parent) {
66 67
 		animations.add(new Animation());
67 68
 		animations.get(0).setName("Animation 1");
68 69
 		animations.get(0).add(0);
69 70
 		animations.get(0).get(0).setName("Frame 1");
71
+		parentFrame = parent;
70 72
 	}
71 73
 
72 74
 	/**
@@ -74,8 +76,9 @@ public class cubeWorker {
74 76
 	 * 
75 77
 	 * @param anims List of animations
76 78
 	 */
77
-	cubeWorker(List<Animation> anims) {
79
+	public cubeWorker(List<Animation> anims, Frame parent) {
78 80
 		animations = anims;
81
+		parentFrame = parent;
79 82
 	}
80 83
 
81 84
 	/**
@@ -397,18 +400,47 @@ public class cubeWorker {
397 400
 	 * @return 0 on success, -1 on error
398 401
 	 */
399 402
 	public int uploadState(String port) {
400
-
401
-		return -1;
403
+		try {
404
+			SerialHelper sh = new SerialHelper(port, parentFrame);
405
+			int ret = sh.sendAnimationsToCube(this);
406
+			sh.closeSerialPort();
407
+			return ret;
408
+		} catch (Exception e) {
409
+			System.out.println("Serial Exception: " + e.getMessage());
410
+			return -1;
411
+		}
402 412
 	}
403 413
 
404 414
 	/**
405 415
 	 * Get all animations from the cube, place it in this object
406 416
 	 * 
407
-	 * @param port Name of serial port to use  @return 0 on success, -1 on error
417
+	 * @param port Name of serial port to use
418
+	 * @return 0 on success, -1 on error
408 419
 	 */
409 420
 	public int downloadState(String port) {
421
+		try {
422
+			SerialHelper sh = new SerialHelper(port, parentFrame);
423
+			cubeWorker ret = sh.getAnimationsFromCube();
424
+			sh.closeSerialPort();
425
+			if (ret == null) {
426
+				return -1;
427
+			} else {
428
+				changedState = true;
429
+				animations = ret.getAnimationList();
430
+				return 0;
431
+			}
432
+		} catch (Exception e) {
433
+			System.out.println("Serial Exception: " + e.getMessage());
434
+			return -1;
435
+		}
436
+	}
410 437
 
411
-		return -1;
438
+	/**
439
+	 * Get the list used internally to store all the animations.
440
+	 * @return The list.
441
+	 */
442
+	public List<Animation> getAnimationList() {
443
+		return animations;
412 444
 	}
413 445
 
414 446
 	/**

+ 16
- 8
CubeControl/libSerial/serialHelper.c View File

@@ -79,8 +79,9 @@ JNIEXPORT jstring JNICALL Java_HelperUtility_getThePorts(JNIEnv *env, jclass cla
79 79
 
80 80
 JNIEXPORT jshortArray JNICALL Java_HelperUtility_readData(JNIEnv *env, jclass class, jint length) {
81 81
 	jshortArray arr = (*env)->NewShortArray(env, length);
82
-	int toBeRead = 0, read;
82
+	int toBeRead = 0, read, i;
83 83
 	char *data = (char *)malloc(length * sizeof(char));
84
+	jshort *data2 = (jshort *)malloc(length * sizeof(jshort));
84 85
 
85 86
 	while (length > 0) {
86 87
 		read = serialRead(data + toBeRead, length);
@@ -88,19 +89,26 @@ JNIEXPORT jshortArray JNICALL Java_HelperUtility_readData(JNIEnv *env, jclass cl
88 89
 		length -= read;
89 90
 	}
90 91
 
91
-	(*env)->SetShortArrayRegion(env, arr, 0, (*env)->GetArrayLength(env, arr), (jshort *)data);
92
+	for (i = 0; i < (*env)->GetArrayLength(env, arr); i++) {
93
+		data2[i] = data[i];
94
+	}
95
+	(*env)->SetShortArrayRegion(env, arr, 0, (*env)->GetArrayLength(env, arr), data2);
92 96
 	return arr;
93 97
 }
94 98
 
95 99
 JNIEXPORT void JNICALL Java_HelperUtility_writeData(JNIEnv *env, jclass class, jshortArray data, jint length) {
96
-	int written = 0;
97
-	int lastIndex = length;
100
+	int toWrite = length, written = 0, now, i;
98 101
 	char *dat = (char *)malloc(length * sizeof(char));
102
+	jshort *dat2 = (jshort *)malloc(length * sizeof(jshort));
99 103
 
100
-	while (length > 0) {
101
-		(*env)->GetShortArrayRegion(env, data, written, length, (jshort *)dat);
102
-		written = serialWrite(dat, length);
103
-		length -= written;
104
+	while (toWrite > 0) {
105
+		(*env)->GetShortArrayRegion(env, data, written, length, dat2);
106
+		for (i = 0; i < length; i++) {
107
+			dat[i] = dat2[i];
108
+		}
109
+		now = serialWrite(dat, toWrite);
110
+		written += now;
111
+		toWrite -= now;
104 112
 	}
105 113
 }
106 114
 

+ 2
- 3
CubeControl/makefile View File

@@ -20,7 +20,7 @@ JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java
20 20
 
21 21
 # --------------------------------------
22 22
 
23
-all: CubeControl.jar clean
23
+all: CubeControl.jar
24 24
 
25 25
 doc: doc/index.html
26 26
 
@@ -45,15 +45,14 @@ endif
45 45
 Serial.dll: libSerial/serialHelper.c libSerial/winSerial.c
46 46
 	make -C libSerial
47 47
 	mv libSerial/Serial.dll Serial.dll
48
-	make -C libSerial clean
49 48
 
50 49
 libSerial.jnilib: libSerial/serialHelper.c libSerial/unixSerial.c
51 50
 	make -C libSerial
52 51
 	mv libSerial/libSerial.jnilib libSerial.jnilib
53
-	make -C libSerial clean
54 52
 
55 53
 # Delete intermediate files
56 54
 clean:
55
+	make -C libSerial clean
57 56
 	$(RM) *.class
58 57
 	$(RM) *.
59 58
 	$(RM) *.o

Loading…
Cancel
Save