Browse Source

Worked at cubeWorker...

Thomas Buck 12 years ago
parent
commit
5a0b59907f
2 changed files with 184 additions and 48 deletions
  1. 182
    46
      Cube Control/cubeWorker.java
  2. 2
    2
      Cube Control/makefile

+ 182
- 46
Cube Control/cubeWorker.java View File

@@ -26,6 +26,8 @@
26 26
  * many animations, but has to be only 1Mbit in size (128*1024 Byte).
27 27
  */
28 28
 
29
+import java.util.ArrayList;
30
+
29 31
 public class cubeWorker {
30 32
 
31 33
 // --------------------
@@ -33,17 +35,23 @@ public class cubeWorker {
33 35
 // --------------------
34 36
 
35 37
   final int UP = 0;
36
-    final int DOWN = 1;
38
+  final int DOWN = 1;
37 39
 
38 40
 // --------------------
39 41
 // Fields
40 42
 // --------------------
41 43
 
44
+	private ArrayList<Animation> animations = new ArrayList<Animation>();
45
+	private int selectedAnimation = 0;
46
+	private int selectedFrame = 0;
47
+	private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
48
+	private boolean changedState = false;
49
+
42 50
 // --------------------
43 51
 
44 52
   cubeWorker() {
45
-
46
-    }
53
+	animations.add(new Animation());
54
+  }
47 55
 
48 56
 // --------------------
49 57
 // Misc. Methods
@@ -51,20 +59,17 @@ public class cubeWorker {
51 59
 
52 60
   // Returns how many animations are defined
53 61
     public int numOfAnimations() {
54
-
55
-           return 3;
62
+           return animations.size();
56 63
     }   
57 64
     
58 65
     // Returns how many frames are in the current animation
59 66
     public int numOfFrames() {
60
-
61
-           return 3;
67
+           return animations.get(selectedAnimation).size();
62 68
     }
63 69
 
64 70
   // Tells how many Frames you can add until you reached 1 Mbit...
65 71
     public int framesRemaining() {
66
-
67
-           return 0;
72
+           return framesRemaining;
68 73
     }
69 74
 
70 75
 
@@ -75,40 +80,60 @@ public class cubeWorker {
75 80
   // Selects an animation on wich the animation specific functions operate
76 81
     // Returns -1 if it does not exist, else its index
77 82
     public int selectAnimation(int index) {
78
-           System.out.println("Animation " + index + " selected.");
79
-           
80
-       return index;
83
+		if (animations.size() <= index) {
84
+			return -1;
85
+		} else {
86
+			selectedAnimation = index;
87
+			return index;
88
+		}
81 89
     }
82 90
 
83 91
   // Adds a new Animation
84 92
     // Returns id if ok, -1 if error or not enough space for
85 93
     // another animation
86 94
     public int addAnimation() {
87
-
88
-           return -1;
95
+		changedState = true;
96
+		if (framesRemaining <= 0) {
97
+        	return -1;
98
+		} else {
99
+			int s = animations.size();
100
+			animations.add(s + 1, new Animation());
101
+			return s;
102
+		}
89 103
     }
90 104
     
91 105
     // Removes an animation
92 106
     public void removeAnimation() {
93
-
107
+		changedState = true;
108
+		animations.remove(selectedAnimation);
109
+		selectedAnimation = 0;
94 110
     }
95 111
     
96 112
   public String getAnimationName() {
97
-
98
-           return "TestAnim";
113
+    	return animations.get(selectedAnimation).getName();
99 114
     }
100 115
 
101 116
   public void setAnimationName(String s) {
102
-
117
+	    changedState = true;
118
+		animations.get(selectedAnimation).setName(s);
103 119
     }
104 120
 
105 121
   public void moveAnimation(int dir) {
122
+	changedState = true;
106 123
     if (dir == UP){
107 124
         //animation moved up
108
-    
125
+    	if (selectedAnimation > 0) {
126
+			Animation tmp = animations.get(selectedAnimation);
127
+			animations.set(selectedAnimation, animations.get(selectedAnimation - 1));
128
+			animations.set(selectedAnimation - 1, tmp);
129
+		}
109 130
     } else if (dir == DOWN){
110 131
       //animation moved down
111
-
132
+		if (selectedAnimation < (animations.size() - 1)) {
133
+			Animation tmp = animations.get(selectedAnimation);
134
+			animations.set(selectedAnimation, animations.get(selectedAnimation + 1));
135
+			animations.set(selectedAnimation + 1, tmp);
136
+		}
112 137
     }
113 138
   }
114 139
 
@@ -119,49 +144,81 @@ public class cubeWorker {
119 144
   // Selects an animation on wich the frame specific functions operate
120 145
     // Returns -1 if it does not exist, else its index
121 146
   public int selectFrame(int index) {
122
-    System.out.println("Frame " + index + " selected.");
123
-
124
-    return index;
147
+	if (animations.get(selectedAnimation).size() <= index) {
148
+		return -1;
149
+	} else {
150
+		selectedFrame = index;
151
+		return index;
152
+	}
125 153
   }
126 154
 
127 155
   public String getFrameName() {
128
-
129
-           return "Test";
156
+    	return animations.get(selectedAnimation).get(selectedFrame).getName();
130 157
     }
131 158
 
132 159
     public void setFrameName(String s) {
133
-
160
+		changedState = true;
161
+		animations.get(selectedAnimation).get(selectedFrame).setName(s);
134 162
     }
135 163
     
136 164
     // Adds a Frame to the current animation.
137 165
     // Returns id if okay, -1 if error
138 166
     public int addFrame() {
139
-
140
-           return -1;
167
+		changedState = true;
168
+		if (framesRemaining <= 0) {
169
+			return -1;
170
+		}
171
+		framesRemaining--;
172
+		int s = animations.get(selectedAnimation).size();
173
+		animations.get(selectedAnimation).add(s);
174
+		return s;
141 175
     }
142 176
     
143 177
     // Remove the frame
144 178
     public void removeFrame() {
145
-
179
+		changedState = true;
180
+		animations.get(selectedAnimation).remove(selectedFrame);
181
+		selectedFrame = 0;
146 182
     }
147 183
     
148 184
     // Returns array with 64 bytes with led values
149 185
     public byte[] getFrame() {
150
-
151
-           return null;
152
-    }
186
+		return animations.get(selectedAnimation).get(selectedFrame).getData();
187
+	}
153 188
     
154 189
     public void setFrame(byte[] data) {
155
-
190
+		changedState = true;
191
+		animations.get(selectedAnimation).get(selectedFrame).setData(data);
156 192
     }
157 193
 
194
+	// Frame duration in 1/24th of a second
195
+	public byte getFrameTime() {
196
+		return animations.get(selectedAnimation).get(selectedFrame).getTime();
197
+	}
198
+
199
+	public void setFrameTime(byte time) {
200
+		changedState = true;
201
+		animations.get(selectedAnimation).get(selectedFrame).setTime(time);
202
+	}
203
+
158 204
   public void moveFrame(int dir){
205
+	changedState = true;
159 206
     if (dir == UP){
160 207
         // frame moved up
161
-      
208
+      	if (selectedFrame > 0) {
209
+			Frame tmp = animations.get(selectedAnimation).get(selectedFrame);
210
+			animations.get(selectedAnimation).set(animations.get(selectedAnimation).get(selectedFrame - 1), selectedFrame);
211
+			animations.get(selectedAnimation).set(tmp, selectedFrame - 1);
212
+			selectedFrame--;
213
+		}
162 214
     } else if (dir == DOWN){
163 215
       // frame moved down
164
-
216
+		if (selectedFrame < (animations.get(selectedAnimation).size() - 1)) {
217
+			Frame tmp = animations.get(selectedAnimation).get(selectedFrame);
218
+			animations.get(selectedAnimation).set(animations.get(selectedAnimation).get(selectedFrame + 1), selectedFrame);
219
+			animations.get(selectedAnimation).set(tmp, selectedFrame + 1);
220
+			selectedFrame++;
221
+		}
165 222
     }
166 223
   }
167 224
 
@@ -171,28 +228,23 @@ public class cubeWorker {
171 228
 
172 229
     // Loads an animation file into this object
173 230
     public int loadState(String path) {
231
+			changedState = false;
174 232
 
175
-           return 0;
233
+    		return 0;
176 234
     }
177 235
     
178 236
     // Saves the state of this object in an animation file
179 237
     public int saveState(String path) {
180
-           System.out.println("Saving to " + path);
238
+           changedState = false;
239
+		   System.out.println("Saving to " + path);
240
+
181 241
            return 0;
182 242
     }
183 243
 
184 244
   // Returns true if last saved state != current state
185 245
     public boolean changedStateSinceSave() {
186
-
187
-           return true;
246
+           return changedState;
188 247
     }
189
-// --------------------
190
-// File Specific
191
-// --------------------
192
-
193
-   public byte[] getLayer(int index){
194
-          return null;
195
-   }
196 248
 
197 249
 // --------------------
198 250
 // Serial Port Specific
@@ -219,3 +271,87 @@ public class cubeWorker {
219 271
 // --------------------
220 272
 
221 273
 }
274
+
275
+class Frame {
276
+	private byte[] data = new byte[64];
277
+	private byte duration = 1;
278
+	private String name = "Frame";
279
+	
280
+	String getName() {
281
+		return name;
282
+	}
283
+
284
+	void setName(String s) {
285
+		name = s;
286
+	}
287
+
288
+	void setData(byte[] d) {
289
+		data = d;
290
+	}
291
+
292
+	byte[] getData() {
293
+		return data;
294
+	}
295
+
296
+	void setTime(byte t) {
297
+		duration = t;
298
+	}
299
+
300
+	byte getTime() {
301
+		return duration;
302
+	}
303
+}
304
+
305
+class Animation {
306
+	private ArrayList<Frame> frames = new ArrayList<Frame>();
307
+	private int lastFrameIndex = 0;
308
+	private String name = "Animation";
309
+
310
+	String getName() {
311
+		return name;
312
+	}
313
+
314
+	void setName(String s) {
315
+		name = s;
316
+	}
317
+
318
+	Frame get(int i) {
319
+		try {
320
+			return frames.get(i);
321
+		} catch (IndexOutOfBoundsException e) {
322
+			System.out.println(e.toString());
323
+			return null;
324
+		}
325
+	}
326
+
327
+	void set(Frame f, int i) {
328
+		if (lastFrameIndex <= i) {
329
+			try {
330
+				frames.set(i, f);
331
+			} catch (IndexOutOfBoundsException e) {
332
+				System.out.println(e.toString());
333
+			}
334
+		}
335
+	}
336
+
337
+	void remove(int i) {
338
+		try {
339
+			frames.remove(i);
340
+		} catch (IndexOutOfBoundsException e) {
341
+			System.out.println(e.toString());
342
+		}
343
+	}
344
+
345
+	void add(int i) {
346
+		try {
347
+			frames.add(i, new Frame());
348
+			lastFrameIndex++;
349
+		} catch (IndexOutOfBoundsException e)  {
350
+			System.out.println(e.toString());
351
+		}
352
+	}
353
+
354
+	int size() {
355
+		return frames.size();
356
+	}
357
+}

+ 2
- 2
Cube Control/makefile View File

@@ -6,9 +6,9 @@ TARGET = unix
6 6
 JAVAFILES = cubeWorker.java layerEditFrame.java frame.java
7 7
 
8 8
 ifeq ($(TARGET),win)
9
-CLASSES = cubeWorker.class layerEditFrame.class layerEditFrame$$1.class layerEditFrame$$2.class layerEditFrame$$3.class layerEditFrame$$4.class frame.class frame$$1.class frame$$2.class frame$$3.class frame$$4.class frame$$5.class frame$$6.class frame$$7.class frame$$8.class frame$$9.class frame$$10.class frame$$11.class frame$$12.class frame$$13.class frame$$14.class frame$$15.class frame$$16.class frame$$17.class frame$$18.class frame$$19.class frame$$20.class frame$$21.class frame$$22.class MyListSelectionListener.class LEDoff.png LEDon.png
9
+CLASSES = cubeWorker.class layerEditFrame.class layerEditFrame$$1.class layerEditFrame$$2.class layerEditFrame$$3.class layerEditFrame$$4.class frame.class frame$$1.class frame$$2.class frame$$3.class frame$$4.class frame$$5.class frame$$6.class frame$$7.class frame$$8.class frame$$9.class frame$$10.class frame$$11.class frame$$12.class frame$$13.class frame$$14.class frame$$15.class frame$$16.class frame$$17.class frame$$18.class frame$$19.class frame$$20.class frame$$21.class frame$$22.class MyListSelectionListener.class Frame.class Animation.class LEDoff.png LEDon.png
10 10
 else
11
-CLASSES = 'cubeWorker.class' 'layerEditFrame.class' 'layerEditFrame$$1.class' 'layerEditFrame$$2.class' 'layerEditFrame$$3.class' 'layerEditFrame$$4.class' 'frame.class' 'frame$$1.class' 'frame$$2.class' 'frame$$3.class' 'frame$$4.class' 'frame$$5.class' 'frame$$6.class' 'frame$$7.class' 'frame$$8.class' 'frame$$9.class' 'frame$$10.class' 'frame$$11.class' 'frame$$12.class' 'frame$$13.class' 'frame$$14.class' 'frame$$15.class' 'frame$$16.class' 'frame$$17.class' 'frame$$18.class' 'frame$$19.class' 'frame$$20.class' 'frame$$21.class' 'frame$$22.class' 'MyListSelectionListener.class' 'LEDoff.png' 'LEDon.png'
11
+CLASSES = 'cubeWorker.class' 'layerEditFrame.class' 'layerEditFrame$$1.class' 'layerEditFrame$$2.class' 'layerEditFrame$$3.class' 'layerEditFrame$$4.class' 'frame.class' 'frame$$1.class' 'frame$$2.class' 'frame$$3.class' 'frame$$4.class' 'frame$$5.class' 'frame$$6.class' 'frame$$7.class' 'frame$$8.class' 'frame$$9.class' 'frame$$10.class' 'frame$$11.class' 'frame$$12.class' 'frame$$13.class' 'frame$$14.class' 'frame$$15.class' 'frame$$16.class' 'frame$$17.class' 'frame$$18.class' 'frame$$19.class' 'frame$$20.class' 'frame$$21.class' 'frame$$22.class' 'MyListSelectionListener.class' 'Frame.class' 'Animation.class' 'LEDoff.png' 'LEDon.png'
12 12
 endif
13 13
 
14 14
 all: build clean

Loading…
Cancel
Save