Преглед изворни кода

CubeWorker is sending data!

Thomas Buck пре 12 година
родитељ
комит
8414d2c418

+ 0
- 6
CubeControl/AnimationUtility.java Прегледај датотеку

@@ -156,12 +156,6 @@ public class AnimationUtility {
156 156
 		return frame;
157 157
 	}
158 158
 
159
-	/*
160
-	 * private static short[] invert(short[] a){ short[] tmp = new
161
-	 * short[a.length]; int j = 0; for(int i = a.length-1; i >= 0; i--){ tmp[j]
162
-	 * = a[i]; j++; } return tmp; }
163
-	 */
164
-
165 159
 	private static short[] concat(short[] a, short[] b) {
166 160
 		short[] c = new short[a.length + b.length];
167 161
 		System.arraycopy(a, 0, c, 0, a.length);

+ 2
- 2
CubeControl/Frame.java Прегледај датотеку

@@ -767,7 +767,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
767 767
 			public void actionPerformed(ActionEvent evt) {
768 768
 				// Error messages are generated by worker
769 769
 				if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
770
-					if (worker.cubeSendState((String) serialPortSelector.getSelectedItem()) == -1) {
770
+					if (worker.cubeSendState((String) serialPortSelector.getSelectedItem()) != -1) {
771 771
 						showDialog("Success!", "Data was sent successfuly!");
772 772
 					}
773 773
 				}
@@ -781,7 +781,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
781 781
 			public void actionPerformed(ActionEvent evt) {
782 782
 				// Error messages are generated by worker
783 783
 				if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
784
-					if (worker.cubeGetState((String) serialPortSelector.getSelectedItem()) == -1) {
784
+					if (worker.cubeGetState((String) serialPortSelector.getSelectedItem()) != -1) {
785 785
 						showDialog("Success!", "Got data successfuly!");
786 786
 					}
787 787
 				}

+ 8
- 6
CubeControl/HelperUtility.java Прегледај датотеку

@@ -179,9 +179,8 @@ public class HelperUtility {
179 179
 	/**
180 180
 	 * Read data from Cube
181 181
 	 * 
182
-	 * @param length
183
-	 *            Amount of data to read
184
-	 * @return Data read
182
+	 * @param length Amount of data to read
183
+	 * @return Data read, empty on error
185 184
 	 */
186 185
 	public static short[] readData(int length) {
187 186
 		try {
@@ -201,16 +200,19 @@ public class HelperUtility {
201 200
 	 *            Data to write
202 201
 	 * @param length
203 202
 	 *            Length of data
203
+	 *
204
+	 * @return TRUE on success, FALSE on error
204 205
 	 */
205
-	public static void writeData(short[] data, int length) {
206
+	public static boolean writeData(short[] data, int length) {
206 207
 		try {
207
-			writeDataNative(data, length);
208
+			return writeDataNative(data, length);
208 209
 		} catch (UnsatisfiedLinkError e) {
209 210
 			System.out.println("ERROR: Library not loaded! (writeData)");
211
+			return false;
210 212
 		}
211 213
 	}
212 214
 
213
-	private static native void writeDataNative(short[] data, int length);
215
+	private static native boolean writeDataNative(short[] data, int length);
214 216
 
215 217
 	// http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
216 218
 	private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {

+ 0
- 358
CubeControl/SerialHelper.java Прегледај датотеку

@@ -1,358 +0,0 @@
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
-import java.util.ArrayList;
34
-import java.util.List;
35
-
36
-public class SerialHelper {
37
-
38
-	private final short OK = 0x42;
39
-	private final short ERROR = 0x23;
40
-
41
-	// Timeout in milliseconds per byte
42
-	private final int transmitTimeout = 1000;
43
-	private final int recieveTimeout = 1000;
44
-
45
-	private Frame frame;
46
-
47
-	/**
48
-	 * Create new SerialHelper.
49
-	 * @param serialPort Name of serial port to use.
50
-	 * @param frame Frame to show error messages 
51
-	 * @throws Exception Could not open serial port.
52
-	 */
53
-	public SerialHelper(String serialPort, Frame frame) throws Exception {
54
-		if (HelperUtility.openPort(serialPort) == false) {
55
-			printErrorMessage("Could not open serial port \"" + serialPort + "\"");
56
-			throw new Exception("Could not open serial port \"" + serialPort + "\"");
57
-		}
58
-		this.frame = frame;
59
-	}
60
-
61
-	/**
62
-	 * Poll to check if the cube is there...
63
-	 * @return TRUE if cube is connected.
64
-	 */
65
-	public boolean probeForCube() {
66
-		short[] data = new short[1];
67
-		data[0] = OK;
68
-		if (!writeData(data)) {
69
-			printErrorMessage("Timeout sending probe for Cube");
70
-			return false;
71
-		}
72
-		data = readData(1);
73
-		if ((data == null) || (data[0] != OK)) {
74
-			printErrorMessage("No response from cube!");
75
-			if (data == null) {
76
-				System.out.println("Probe was null!");
77
-			} else {
78
-				System.out.println("Probe was " + data[0]);
79
-			}
80
-			return false;
81
-		}
82
-		return true;
83
-	}
84
-
85
-	/**
86
-	 * Recieve all animations saved in cube.
87
-	 * @return A cubeWorker populated with the new data or null.
88
-	 */
89
-	public cubeWorker getAnimationsFromCube() {
90
-		Animation[] animations;
91
-		int animationCount, frameCount;
92
-		short[] data, tmp = new short[1];
93
-
94
-		// Send download command
95
-		tmp[0] = 'g';
96
-		if (!writeData(tmp)) {
97
-			printErrorMessage("Timout sending Command");
98
-			return null;
99
-		}
100
-		data = readData(1);
101
-		if ((data == null) || (data[0] != OK)) {
102
-			printErrorMessage("Response Error");
103
-			if (data == null) {
104
-				System.out.println("Download was null!");
105
-			} else {
106
-				System.out.println("Download was " + data[0]);
107
-			}
108
-			return null;
109
-		}
110
-
111
-		// Get animation count
112
-		data = readData(1);
113
-		if (data == null) {
114
-			printErrorMessage("Response Error.");
115
-			System.out.println("Did not get animation count!");
116
-			return null;
117
-		} else {
118
-			animationCount = data[0];
119
-		}
120
-		tmp[0] = OK;
121
-		if (!writeData(tmp)) {
122
-			printErrorMessage("Timout acknowledging animation count!");
123
-			return null;
124
-		}
125
-
126
-		animations = new Animation[animationCount];
127
-
128
-		// Get animations
129
-		for (int a = 0; a < animationCount; a++) {
130
-			Animation currentAnim = new Animation();
131
-
132
-			// Get number of frames
133
-			data = readData(1);
134
-			if (data == null) {
135
-				printErrorMessage("Response Error");
136
-				System.out.println("Did not get frame count!");
137
-				return null;
138
-			} else {
139
-				frameCount = data[0];
140
-			}
141
-			tmp[0] = OK;
142
-			if (!writeData(tmp)) {
143
-				printErrorMessage("Timout sending response Frame Count.");
144
-				return null;
145
-			}
146
-
147
-			// Get frames
148
-			for (int f = 0; f < frameCount; f++) {
149
-				AFrame currentFrame = new AFrame();
150
-
151
-				// Get frame duration
152
-				data = readData(1);
153
-				if (data == null) {
154
-					printErrorMessage("Response Error");
155
-					System.out.println("Did not get frame duration!");
156
-					return null;
157
-				} else {
158
-					currentFrame.setTime(data[0]);
159
-				}
160
-				tmp[0] = OK;
161
-				if (!writeData(tmp)) {
162
-					printErrorMessage("Timout sending response Frame Duration");
163
-					return null;
164
-				}
165
-
166
-				// Get frame data
167
-				data = readData(64);
168
-				if (data == null) {
169
-					printErrorMessage("Response Error");
170
-					System.out.println("Did not get frame data!");
171
-					return null;
172
-				} else {
173
-					currentFrame.setData(data);
174
-				}
175
-				tmp[0] = OK;
176
-				if (!writeData(tmp)) {
177
-					printErrorMessage("Timout sending response Frame Data");
178
-					return null;
179
-				}
180
-
181
-				// Add frame to animation
182
-				currentAnim.setFrame(currentFrame, f);
183
-			}
184
-
185
-			// Add animation to animations list
186
-			animations[a] = currentAnim;
187
-		}
188
-
189
-		return new cubeWorker(animations, frame);
190
-	}
191
-
192
-	/**
193
-	 * Send all animations in a cubeWorker to the Cube.
194
-	 * @param worker cubeWorker that containts data.
195
-	 * @return 0 on success. -1 on error.
196
-	 */
197
-	public int sendAnimationsToCube(cubeWorker worker) {
198
-		short[] data, tmp = new short[1];
199
-
200
-		// Send upload command
201
-		tmp[0] = 's';
202
-		if (!writeData(tmp)) {
203
-			printErrorMessage("Timout sending Command");
204
-			return -1;
205
-		}
206
-		data = readData(1);
207
-		if ((data == null) || (data[0] != OK)) {
208
-			printErrorMessage("Response Error Command");
209
-			if (data == null) {
210
-				System.out.println("Response Command was null!");
211
-			} else {
212
-				System.out.println("Response Command was " + data[0]);
213
-			}
214
-			return -1;
215
-		}
216
-
217
-		// Send animation count
218
-		tmp[0] = (short)worker.size();
219
-		if (!writeData(tmp)) {
220
-			printErrorMessage("Timeout sending numOfAnimations");
221
-			return -1;
222
-		}
223
-		data = readData(1);
224
-		if ((data == null) || (data[0] != OK)) {
225
-			printErrorMessage("Response Error");
226
-			if (data == null) {
227
-				System.out.println("Response Count was null!");
228
-			} else {
229
-				System.out.println("Response Count was " + data[0]);
230
-			}
231
-			return -1;
232
-		}
233
-
234
-		// Send animations
235
-		for (int a = 0; a < worker.size(); a++) {
236
-			// Send frame count
237
-			tmp[0] = (short)worker.getAnimation(a).size();
238
-			if (!writeData(tmp)) {
239
-				printErrorMessage("Timeout sending numOfFrames");
240
-				return -1;
241
-			}
242
-			data = readData(1);
243
-			if ((data == null) || (data[0] != OK)) {
244
-				printErrorMessage("Response Error");
245
-				if (data == null) {
246
-				System.out.println("Frame Count was null!");
247
-			} else {
248
-				System.out.println("Frame Count was " + data[0]);
249
-			}
250
-				return -1;
251
-			}
252
-
253
-			// Send frames
254
-			for (int f = 0; f < worker.getAnimation(a).size(); f++) {
255
-				// Frame duration
256
-				tmp[0] = worker.getAnimation(a).getFrame(f).getTime();
257
-				if (!writeData(tmp)) {
258
-					printErrorMessage("Timeout sending Frame duration");
259
-					return -1;
260
-				}
261
-				data = readData(1);
262
-				if ((data == null) || (data[0] != OK)) {
263
-					printErrorMessage("Response Error");
264
-					if (data == null) {
265
-						System.out.println("Duration was null!");
266
-					} else {
267
-						System.out.println("Duration was " + data[0]);
268
-					}
269
-					return -1;
270
-				}
271
-
272
-				// Frame data
273
-				if (!writeData(worker.getAnimation(a).getFrame(f).getData())) {
274
-					printErrorMessage("Timeout sending Frame");
275
-					return -1;
276
-				}
277
-				data = readData(1);
278
-				if ((data == null) || (data[0] != OK)) {
279
-					printErrorMessage("Response Error");
280
-					if (data == null) {
281
-						System.out.println("Datawas null!");
282
-					} else {
283
-						System.out.println("Data was " + data[0]);
284
-					}
285
-					return -1;
286
-				}
287
-			}
288
-		}
289
-
290
-		// Send finish
291
-		tmp = new short[4];
292
-		tmp[0] = OK;
293
-		tmp[1] = OK;
294
-		tmp[2] = OK;
295
-		tmp[3] = OK;
296
-		if (!writeData(tmp)) {
297
-			printErrorMessage("Timeout sending Finish");
298
-			return -1;
299
-		}
300
-		data = readData(1);
301
-		if ((data == null) || (data[0] != OK)) {
302
-			printErrorMessage("Response Error");
303
-			if (data == null) {
304
-				System.out.println("Finish was null!");
305
-			} else {
306
-				System.out.println("Finish was " + data[0]);
307
-			}
308
-			return -1;
309
-		}
310
-		return 0;
311
-	}
312
-
313
-	/**
314
-	 * Close the serial port again.
315
-	 */
316
-	public void closeSerialPort() {
317
-		HelperUtility.closePort();
318
-	}
319
-
320
-	private void printErrorMessage(String s) {
321
-		System.out.println("SerialHelper: " + s);
322
-		frame.errorMessage("Serial Error", s);
323
-	}
324
-
325
-	private boolean writeData(short[] data) {
326
-		// write data. return true if success
327
-		long startdate = (new Date()).getTime();
328
-
329
-		SerialWriteThread t = new SerialWriteThread(data);
330
-		t.start();
331
-		while (!t.dataWasSent()) {
332
-			if ((new Date()).getTime() >= (startdate + (data.length * transmitTimeout))) {
333
-				return false;
334
-			}
335
-		}
336
-		return true;
337
-	}
338
-
339
-	private short[] readData(int length) {
340
-		// return data read or null if timeout
341
-		long startdate = (new Date()).getTime();
342
-		short[] result;
343
-		SerialReadThread t = new SerialReadThread(length);
344
-		t.start();
345
-		while (!t.dataIsReady()) {
346
-			if ((new Date()).getTime() >= (startdate + (length * recieveTimeout))) {
347
-				return null;
348
-			}
349
-		}
350
-		result = t.getSerialData();
351
-		if (result == null) {
352
-			System.out.println("Data read was null!");
353
-		} else if (result.length == 0) {
354
-			System.out.println("No data read!");
355
-		}
356
-		return result;
357
-	}
358
-}

+ 0
- 75
CubeControl/SerialReadThread.java Прегледај датотеку

@@ -1,75 +0,0 @@
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
-}

+ 0
- 65
CubeControl/SerialWriteThread.java Прегледај датотеку

@@ -1,65 +0,0 @@
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
-}

+ 208
- 33
CubeControl/cubeWorker.java Прегледај датотеку

@@ -213,28 +213,188 @@ public class cubeWorker {
213 213
 	}
214 214
 
215 215
 	/**
216
+	 * Get the array of animations in this worker.
217
+	 * @return animation array
218
+	 */
219
+	public Animation[] getAnimationArray() {
220
+		return animations;
221
+	}
222
+
223
+	/**
216 224
 	 * Send all animations to the cube.
217 225
 	 * 
218 226
 	 * @param port Name of serial port to use
219 227
 	 * @return 0 on success, -1 on error
220 228
 	 */
221 229
 	public int cubeSendState(String port) {
222
-		try {
223
-			SerialHelper sh = new SerialHelper(port, parentFrame);
224
-			int ret = sh.sendAnimationsToCube(this);
225
-			sh.closeSerialPort();
226
-			return ret;
227
-		} catch (Exception e) {
230
+		int a, f;
231
+		short[] d;
232
+
233
+		if (HelperUtility.openPort(port) == false) {
234
+			error("Could not open Port!");
228 235
 			return -1;
229 236
 		}
230
-	}
231 237
 
232
-	/**
233
-	 * Get the array of animations in this worker.
234
-	 * @return animation array
235
-	 */
236
-	public Animation[] getAnimationArray() {
237
-		return animations;
238
+		// Send command
239
+		d = new short[1];
240
+		d[0] = 's';
241
+		if (HelperUtility.writeData(d, 1) == false) {
242
+			error("Could not write to port!");
243
+			HelperUtility.closePort();
244
+			return -1;
245
+		}
246
+
247
+		// Recieve ack
248
+		d = HelperUtility.readData(1);
249
+		if (d.length == 0) {
250
+			error("Could not read from port!");
251
+			HelperUtility.closePort();
252
+			return -1;
253
+		}
254
+		if (d[0] != 0x42) {
255
+			error("Cube not OK!");
256
+			HelperUtility.closePort();
257
+			return -1;
258
+		}
259
+
260
+		System.out.println("Command sent!");
261
+
262
+		// Send animation count
263
+		d = new short[1];
264
+		if (animations.length >= 255) {
265
+			error("Too many animations");
266
+			return -1;
267
+		}
268
+		d[0] = (short)animations.length;
269
+		if (HelperUtility.writeData(d, 1) == false) {
270
+			error("Could not write to port!");
271
+			HelperUtility.closePort();
272
+			return -1;
273
+		}
274
+
275
+		// Recieve ack
276
+		d = HelperUtility.readData(1);
277
+		if (d.length == 0) {
278
+			error("Could not read from port!");
279
+			HelperUtility.closePort();
280
+			return -1;
281
+		}
282
+		if (d[0] != 0x42) {
283
+			error("Cube not OK!");
284
+			HelperUtility.closePort();
285
+			return -1;
286
+		}
287
+
288
+		System.out.println("Animation count sent (" + animations.length + ")!");
289
+
290
+		for (a = 0; a < animations.length; a++) {
291
+			// Send frame count
292
+			d = new short[1];
293
+			if (animations[a].size() >= 255) {
294
+				error("Too many frames!");
295
+				HelperUtility.closePort();
296
+				return -1;
297
+			}
298
+			d[0] = (short)animations[a].size();
299
+			if (HelperUtility.writeData(d, 1) == false) {
300
+				error("Could not write to port!");
301
+				HelperUtility.closePort();
302
+				return -1;
303
+			}
304
+
305
+			// Recieve ack
306
+			d = HelperUtility.readData(1);
307
+			if (d.length == 0) {
308
+				error("Could not read from port!");
309
+				HelperUtility.closePort();
310
+				return -1;
311
+			}
312
+			if (d[0] != 0x42) {
313
+				error("Cube not OK!");
314
+				HelperUtility.closePort();
315
+				return -1;
316
+			}
317
+
318
+			System.out.println("Frame count sent (" + animations[a].size() + ")!");
319
+
320
+			for (f = 0; f < animations[a].size(); f++) {
321
+				// Send duration
322
+				d = new short[1];
323
+				d[0] = animations[a].getFrame(f).getTime();
324
+				if (HelperUtility.writeData(d, 1) == false) {
325
+					error("Could not write to port!");
326
+					HelperUtility.closePort();
327
+					return -1;
328
+				}
329
+
330
+				// Recieve ack
331
+				d = HelperUtility.readData(1);
332
+				if (d.length == 0) {
333
+					error("Could not read from port!");
334
+					HelperUtility.closePort();
335
+					return -1;
336
+				}
337
+				if (d[0] != 0x42) {
338
+					error("Cube not OK!");
339
+					HelperUtility.closePort();
340
+					return -1;
341
+				}
342
+
343
+				System.out.println("Duration sent (" + animations[a].getFrame(f).getTime() + ")!");
344
+
345
+				// Send data
346
+				d = animations[a].getFrame(f).getData();
347
+				if (HelperUtility.writeData(d, 64) == false) {
348
+					error("Could not write to port!");
349
+					HelperUtility.closePort();
350
+					return -1;
351
+				}
352
+
353
+				// Recieve ack
354
+				d = HelperUtility.readData(1);
355
+				if (d.length == 0) {
356
+					error("Could not read from port!");
357
+					HelperUtility.closePort();
358
+					return -1;
359
+				}
360
+				if (d[0] != 0x42) {
361
+					error("Cube not OK!");
362
+					HelperUtility.closePort();
363
+					return -1;
364
+				}
365
+
366
+				System.out.println("Data sent");
367
+			}
368
+		}
369
+
370
+		// Send finish sequence
371
+		d = new short[4];
372
+		d[0] = 0x42;
373
+		d[1] = 0x42;
374
+		d[2] = 0x42;
375
+		d[3] = 0x42;
376
+		if (HelperUtility.writeData(d, 4) == false) {
377
+			error("Could not write to port!");
378
+			HelperUtility.closePort();
379
+			return -1;
380
+		}
381
+
382
+		// Recieve ack
383
+		d = HelperUtility.readData(1);
384
+		if (d.length == 0) {
385
+			error("Could not read from port!");
386
+			HelperUtility.closePort();
387
+			return -1;
388
+		}
389
+		if (d[0] != 0x42) {
390
+			error("Cube not OK!");
391
+			HelperUtility.closePort();
392
+			return -1;
393
+		}
394
+
395
+		System.out.println("Uploaded animations!");
396
+
397
+		return 0;
238 398
 	}
239 399
 
240 400
 	/**
@@ -243,21 +403,9 @@ public class cubeWorker {
243 403
 	 * @param port Name of serial port to use
244 404
 	 * @return 0 on success, -1 on error
245 405
 	 */
406
+	// We generate error messages in here
246 407
 	public int cubeGetState(String port) {
247
-		try {
248
-			SerialHelper sh = new SerialHelper(port, parentFrame);
249
-			cubeWorker ret = sh.getAnimationsFromCube();
250
-			sh.closeSerialPort();
251
-			if (ret == null) {
252
-				return -1;
253
-			} else {
254
-				changedState = true;
255
-				animations = ret.getAnimationArray();
256
-				return 0;
257
-			}
258
-		} catch (Exception e) {
259
-			return -1;
260
-		}
408
+		return -1;
261 409
 	}
262 410
 
263 411
 	/**
@@ -267,14 +415,41 @@ public class cubeWorker {
267 415
 	 * @param port Name of serial port
268 416
 	 */
269 417
 	public boolean cubeProbeConnected(String port) {
270
-		try {
271
-			SerialHelper sh = new SerialHelper(port, parentFrame);
272
-			boolean response = sh.probeForCube();
273
-			sh.closeSerialPort();
274
-			return response;
275
-		} catch (Exception e) {
418
+		if (HelperUtility.openPort(port) == false) {
419
+			error("Could not open Port!");
420
+			return false;
421
+		}
422
+
423
+		short[] d = new short[1];
424
+		d[0] = 0x42;
425
+		if (HelperUtility.writeData(d, 1) == false) {
426
+			error("Could not write to port!");
427
+			HelperUtility.closePort();
276 428
 			return false;
277 429
 		}
430
+
431
+		d = HelperUtility.readData(1);
432
+		if (d.length == 0) {
433
+			error("Could not read from port!");
434
+			HelperUtility.closePort();
435
+			return false;
436
+		}
437
+
438
+		HelperUtility.closePort();
439
+
440
+		if (d[0] != 0x42) {
441
+			error("Answer was not OK");
442
+			return false;
443
+		} else {
444
+			System.out.println("Got probe response!");
445
+			return true;
446
+		}
447
+
448
+	}
449
+
450
+	private void error(String s) {
451
+		System.out.println(s);
452
+		parentFrame.errorMessage("Serial Error", s);
278 453
 	}
279 454
 
280 455
 	private void extendArray() {

+ 30
- 9
CubeControl/libSerial/serialHelper.c Прегледај датотеку

@@ -81,14 +81,21 @@ JNIEXPORT jstring JNICALL Java_HelperUtility_getThePorts(JNIEnv *env, jclass cla
81 81
 
82 82
 JNIEXPORT jshortArray JNICALL Java_HelperUtility_readDataNative(JNIEnv *env, jclass class, jint length) {
83 83
 	jshortArray arr = (*env)->NewShortArray(env, length);
84
-	int toBeRead = 0, read, i;
84
+	int toBeRead = 0, read, i, error = 0;
85 85
 	char *data = (char *)malloc(length * sizeof(char));
86 86
 	jshort *data2 = (jshort *)malloc(length * sizeof(jshort));
87 87
 
88 88
 	while (length > 0) {
89 89
 		read = serialRead(data + toBeRead, length);
90
-		toBeRead += read;
91
-		length -= read;
90
+		if (read == -1) {
91
+			error++;
92
+			if (error > 10) {
93
+				return (*env)->NewShortArray(env, 0);
94
+			}
95
+		} else {
96
+			toBeRead += read;
97
+			length -= read;
98
+		}
92 99
 	}
93 100
 
94 101
 	for (i = 0; i < (*env)->GetArrayLength(env, arr); i++) {
@@ -98,20 +105,34 @@ JNIEXPORT jshortArray JNICALL Java_HelperUtility_readDataNative(JNIEnv *env, jcl
98 105
 	return arr;
99 106
 }
100 107
 
101
-JNIEXPORT void JNICALL Java_HelperUtility_writeDataNative(JNIEnv *env, jclass class, jshortArray data, jint length) {
102
-	int toWrite = length, written = 0, now, i;
108
+JNIEXPORT jboolean JNICALL Java_HelperUtility_writeDataNative(JNIEnv *env, jclass class, jshortArray data, jint length) {
109
+	int toWrite = length, written = 0, now, i, error = 0;
103 110
 	char *dat = (char *)malloc(length * sizeof(char));
104 111
 	jshort *dat2 = (jshort *)malloc(length * sizeof(jshort));
105 112
 
113
+	// Get unwritten data from argument, place in dat2
114
+	// move from dat2 into dat1
115
+	// write dat1
116
+	// repeat
117
+
106 118
 	while (toWrite > 0) {
107
-		(*env)->GetShortArrayRegion(env, data, written, length, dat2);
108
-		for (i = 0; i < length; i++) {
119
+		(*env)->GetShortArrayRegion(env, data, written, length, dat2); // Unwritten part of data in dat2
120
+		for (i = 0; i < (length - written); i++) {
109 121
 			dat[i] = dat2[i];
110 122
 		}
111 123
 		now = serialWrite(dat, toWrite);
112
-		written += now;
113
-		toWrite -= now;
124
+		if (now == -1) {
125
+			error++;
126
+			if (error > 10) {
127
+				return JNI_FALSE;
128
+			}
129
+		} else {
130
+			written += now;
131
+			toWrite -= now;
132
+		}
114 133
 	}
134
+
135
+	return JNI_TRUE;
115 136
 }
116 137
 
117 138
 JNIEXPORT void JNICALL Java_HelperUtility_closePortNative(JNIEnv * env, jclass class) {

+ 1
- 1
CubeControl/libSerial/serialInterface.h Прегледај датотеку

@@ -53,7 +53,7 @@ JNIEXPORT jshortArray DLL JNICALL Java_HelperUtility_readDataNative
53 53
  * Method:    writeDataNative
54 54
  * Signature: ([SI)V
55 55
  */
56
-JNIEXPORT void DLL JNICALL Java_HelperUtility_writeDataNative
56
+JNIEXPORT jboolean DLL JNICALL Java_HelperUtility_writeDataNative
57 57
   (JNIEnv *, jclass, jshortArray, jint);
58 58
 
59 59
 #ifdef __cplusplus

+ 1
- 1
CubeControl/makefile Прегледај датотеку

@@ -25,7 +25,7 @@ endif
25 25
 
26 26
 # All java files to be compiled
27 27
 # List so it works as target
28
-JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java SerialReadThread.java SerialWriteThread.java SerialHelper.java
28
+JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java
29 29
 
30 30
 # --------------------------------------
31 31
 

+ 37
- 21
UploadTest/main.c Прегледај датотеку

@@ -19,7 +19,7 @@ volatile int keepRunning = 1;
19 19
 
20 20
 int main(int argc, char *argv[]) {
21 21
 	char c;
22
-	int i;
22
+	int i, f;
23 23
 	
24 24
 	if (argc != 2) {
25 25
 		printf("Usage: %s /dev/port\n", argv[0]);
@@ -56,8 +56,8 @@ int main(int argc, char *argv[]) {
56 56
 
57 57
 	readAck();
58 58
 
59
-	printf("\tSending frame count (1)...");
60
-	c = 1;
59
+	printf("\tSending frame count (2)...");
60
+	c = 2;
61 61
 	if (serialWriteTry(&c, 1) != 0) {
62 62
 		printf(" Could not send it!\n");
63 63
 		suicide;
@@ -66,29 +66,45 @@ int main(int argc, char *argv[]) {
66 66
 
67 67
 	readAck();
68 68
 
69
-	printf("\tSending duration (1)...");
70
-	c = 1;
71
-	if (serialWriteTry(&c, 1) != 0) {
72
-		printf(" Could not send it!\n");
73
-		suicide;
74
-	}
75
-	printf(" Success!\n");
76
-
77
-	readAck();
78
-
79
-	printf("\tSending data");
80
-	for(i = 0; i < 64; i++) {
81
-		c = (char)i; // Some test data
69
+	for (f = 0; f < 2; f++) {
70
+		printf("\tSending duration (3)...");
71
+		c = 3;
82 72
 		if (serialWriteTry(&c, 1) != 0) {
83
-			printf(" Error while sending!\n");
73
+			printf(" Could not send it!\n");
84 74
 			suicide;
75
+		}
76
+		printf(" Success!\n");
77
+
78
+		readAck();
79
+
80
+		printf("\tSending data");
81
+		
82
+		if (f == 0) {
83
+			for(i = 0; i < 64; i++) {
84
+				c = (char)i; // Some test data
85
+				if (serialWriteTry(&c, 1) != 0) {
86
+					printf(" Error while sending!\n");
87
+					suicide;
88
+				} else {
89
+					printf(".");
90
+				}
91
+			}
92
+			printf(" Success!\n");
85 93
 		} else {
86
-			printf(".");
94
+			for(i = 0; i < 64; i++) {
95
+				c = (char)63 - i; // Some test data
96
+				if (serialWriteTry(&c, 1) != 0) {
97
+					printf(" Error while sending!\n");
98
+					suicide;
99
+				} else {
100
+					printf(".");
101
+				}
102
+			}
103
+			printf(" Success!\n");
87 104
 		}
88
-	}
89
-	printf(" Success!\n");
90 105
 
91
-	readAck();
106
+		readAck();
107
+	}
92 108
 
93 109
 	printf("\tSending end sequence");
94 110
 	for (i = 0; i < 4; i++) {

Loading…
Откажи
Сачувај