|
@@ -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();
|