Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cubeWorker.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * cubeWorker.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. * This class handles one animation file. This file can contain
  25. * many animations, but has to be only 1Mbit in size (128*1024 Byte).
  26. */
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.Scanner;
  30. import java.util.Collections;
  31. import java.io.FileWriter;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
  35. public class cubeWorker {
  36. // --------------------
  37. // Definitions
  38. // --------------------
  39. final int UP = 0;
  40. final int DOWN = 1;
  41. // --------------------
  42. // Fields
  43. // --------------------
  44. private ArrayList<Animation> animations = new ArrayList<Animation>();
  45. private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
  46. private boolean changedState = false;
  47. // --------------------
  48. cubeWorker() {
  49. animations.add(new Animation());
  50. animations.get(0).setName("Animation 1");
  51. animations.get(0).add(0);
  52. animations.get(0).get(0).setName("Frame 1");
  53. framesRemaining--;
  54. }
  55. cubeWorker(ArrayList<Animation> anims) {
  56. animations = anims;
  57. }
  58. // --------------------
  59. // Misc. Methods
  60. // --------------------
  61. // Returns how many animations are defined
  62. public int numOfAnimations() {
  63. return animations.size();
  64. }
  65. // Returns how many frames are in the current animation
  66. public int numOfFrames(int selectedAnimation) {
  67. return animations.get(selectedAnimation).size();
  68. }
  69. // Tells how many Frames you can add until you reached 1 Mbit...
  70. public int framesRemaining() {
  71. return framesRemaining;
  72. }
  73. // --------------------
  74. // Animation Specific
  75. // --------------------
  76. // Adds a new Animation
  77. // Returns id if ok, -1 if error or not enough space for
  78. // another animation
  79. public int addAnimation() {
  80. changedState = true;
  81. if (framesRemaining <= 0) {
  82. return -1;
  83. } else {
  84. int s = animations.size();
  85. animations.add(s, new Animation());
  86. animations.get(s).setName("Animation " + animations.size());
  87. return s;
  88. }
  89. }
  90. // Removes an animation
  91. public void removeAnimation(int selectedAnimation) {
  92. changedState = true;
  93. animations.remove(selectedAnimation);
  94. selectedAnimation = 0;
  95. }
  96. public String getAnimationName(int selectedAnimation) {
  97. return animations.get(selectedAnimation).getName();
  98. }
  99. public void setAnimationName(String s, int selectedAnimation) {
  100. changedState = true;
  101. animations.get(selectedAnimation).setName(s);
  102. }
  103. public void moveAnimation(int dir, int selectedAnimation) {
  104. changedState = true;
  105. if (dir == UP){
  106. //animation moved up
  107. if (selectedAnimation > 0) {
  108. Collections.swap(animations, selectedAnimation, selectedAnimation - 1);
  109. }
  110. } else if (dir == DOWN){
  111. //animation moved down
  112. if (selectedAnimation < (animations.size() - 1)) {
  113. Collections.swap(animations, selectedAnimation, selectedAnimation + 1);
  114. }
  115. }
  116. }
  117. // --------------------
  118. // Frame Specific
  119. // --------------------
  120. public String getFrameName(int anim, int frame) {
  121. return animations.get(anim).get(frame).getName();
  122. }
  123. public void setFrameName(String s, int anim, int frame) {
  124. changedState = true;
  125. animations.get(anim).get(frame).setName(s);
  126. }
  127. // Adds a Frame to the current animation.
  128. // Returns id if okay, -1 if error
  129. public int addFrame(int anim) {
  130. changedState = true;
  131. if (framesRemaining <= 0) {
  132. return -1;
  133. }
  134. framesRemaining--;
  135. int s = animations.get(anim).size();
  136. animations.get(anim).add(s);
  137. animations.get(anim).get(s).setName("Frame " + animations.get(anim).size());
  138. return s;
  139. }
  140. // Remove the frame
  141. public void removeFrame(int anim, int frame) {
  142. changedState = true;
  143. animations.get(anim).remove(frame);
  144. }
  145. // Returns array with 64 bytes with led values
  146. public byte[] getFrame(int anim, int frame) {
  147. return animations.get(anim).get(frame).getData();
  148. }
  149. public void setFrame(byte[] data, int anim, int frame) {
  150. changedState = true;
  151. animations.get(anim).get(frame).setData(data);
  152. }
  153. // Frame duration in 1/24th of a second
  154. public byte getFrameTime(int anim, int frame) {
  155. return animations.get(anim).get(frame).getTime();
  156. }
  157. public void setFrameTime(byte time, int anim, int frame) {
  158. changedState = true;
  159. animations.get(anim).get(frame).setTime(time);
  160. }
  161. public void moveFrame(int dir, int anim, int frame){
  162. changedState = true;
  163. if (dir == UP){
  164. // frame moved up
  165. if (frame > 0) {
  166. Collections.swap(animations.get(anim).frames, frame, frame - 1);
  167. }
  168. } else if (dir == DOWN){
  169. // frame moved down
  170. if (frame < (animations.get(anim).size() - 1)) {
  171. Collections.swap(animations.get(anim).frames, frame, frame + 1);
  172. }
  173. }
  174. }
  175. // --------------------
  176. // File Specific
  177. // --------------------
  178. // Loads an animation file into this object
  179. public int loadState(String path) {
  180. changedState = false;
  181. try {
  182. animations = AnimationUtility.readFile(path);
  183. } catch (Exception e) {
  184. System.out.println("Did not load!");
  185. e.printStackTrace(System.out);
  186. return -1;
  187. }
  188. int size = 0;
  189. for (int i = 0; i < animations.size(); i++) {
  190. size += animations.get(i).size();
  191. }
  192. framesRemaining = 2016 - size;
  193. if (size > 2016) {
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. // Saves the state of this object in an animation file
  199. public int saveState(String path) {
  200. changedState = false;
  201. AnimationUtility.writeFile(path, animations);
  202. if (AnimationUtility.getLastError() != null) {
  203. System.out.println(AnimationUtility.getLastError());
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. // Returns true if last saved state != current state
  209. public boolean changedStateSinceSave() {
  210. return changedState;
  211. }
  212. // --------------------
  213. // Serial Port Specific
  214. // --------------------
  215. // sends state of object to led cube
  216. public int uploadState(String port) {
  217. return 0;
  218. }
  219. // loads all state from the cube into this object
  220. public int downloadState(String port) {
  221. return 0;
  222. }
  223. public String[] getSerialPorts() {
  224. String[] sPorts = { "Select serial port..." }; // Has to be the first entry
  225. return sPorts;
  226. }
  227. // --------------------
  228. }
  229. class AnimationUtility {
  230. private static String lastError = null;
  231. public static ArrayList<Animation> readFile(String path) throws Exception {
  232. Scanner sc = new Scanner(new File(path));
  233. ArrayList<Animation> animations = new ArrayList<Animation>();
  234. do {
  235. Animation tmp = readAnimation(sc);
  236. if (tmp == null) {
  237. return animations;
  238. }
  239. if (sc.hasNextLine()) {
  240. sc.nextLine();
  241. }
  242. animations.add(tmp);
  243. } while (sc.hasNextLine());
  244. return animations;
  245. }
  246. public static void writeFile(String path, ArrayList<Animation> animations) {
  247. File f = new File(path);
  248. if (f.exists()) {
  249. try {
  250. f.delete();
  251. } catch (Exception e) {
  252. lastError = e.toString();
  253. return;
  254. }
  255. }
  256. FileWriter output = null;
  257. try {
  258. output = new FileWriter(f);
  259. for (int i = 0; i < animations.size(); i++) {
  260. writeAnimation(animations.get(i), output, (i == (animations.size() - 1)));
  261. }
  262. } catch (Exception e) {
  263. lastError = e.toString();
  264. return;
  265. } finally {
  266. if (output != null) {
  267. try {
  268. output.close();
  269. } catch (Exception e) {
  270. lastError = e.toString();
  271. }
  272. }
  273. }
  274. }
  275. public static String getLastError() {
  276. String tmp = lastError;
  277. lastError = null;
  278. return tmp;
  279. }
  280. private static Animation readAnimation(Scanner sc) {
  281. Animation anim = new Animation();
  282. AFrame f = null;
  283. int index = 0;
  284. String tmpSize = sc.nextLine().replaceAll("\\n", "");
  285. if (tmpSize.equals("")) {
  286. return null;
  287. }
  288. Integer tmpSizeAgain = new Integer(tmpSize);
  289. int size = tmpSizeAgain.intValue();
  290. anim.setName(sc.nextLine());
  291. while (size > 0) {
  292. f = readFrame(sc, index);
  293. anim.add(index, f);
  294. index++;
  295. size--;
  296. }
  297. return anim;
  298. }
  299. private static AFrame readFrame(Scanner sc, int index) {
  300. AFrame frame = new AFrame();
  301. frame.setName(sc.nextLine());
  302. byte[] d = {};
  303. for (int i = 0; i < 8; i++) {
  304. byte[] data = hexConvert(sc.nextLine());
  305. d = concat(data, d);
  306. }
  307. frame.setData(d);
  308. d = hexConvert(sc.nextLine());
  309. frame.setTime(d[0]);
  310. return frame;
  311. }
  312. private static byte[] concat(byte[] a, byte[] b) {
  313. byte[] c = new byte[a.length + b.length];
  314. System.arraycopy(a, 0, c, 0, a.length);
  315. System.arraycopy(b, 0, c, a.length, b.length);
  316. return c;
  317. }
  318. private static byte[] hexConvert(String hex) {
  319. hex = hex.replaceAll("\\n", "");
  320. HexBinaryAdapter a = new HexBinaryAdapter();
  321. return a.unmarshal(hex);
  322. }
  323. private static void writeAnimation(Animation anim, FileWriter f, boolean last) throws IOException {
  324. f.write(anim.size() + "\n");
  325. f.write(anim.getName() + "\n");
  326. for (int i = 0; i < anim.size(); i++) {
  327. writeFrame(anim.get(i), f);
  328. }
  329. if (!last) {
  330. f.write("\n");
  331. }
  332. }
  333. private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
  334. f.write(fr.getName() + "\n");
  335. for (int i = 0; i < 8; i++) {
  336. writeLayer(fr.getLayer(i), f);
  337. }
  338. f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
  339. }
  340. private static void writeLayer(byte[] l, FileWriter f) throws IOException {
  341. String hex = "";
  342. for (int i = 0; i < l.length; i++) {
  343. hex += Integer.toString( (l[i] & 0xff) + 0x100, 16).substring(1);
  344. }
  345. hex += "\n";
  346. f.write(hex);
  347. }
  348. }
  349. class AFrame {
  350. private byte[] data = new byte[64];
  351. private byte duration = 1;
  352. private String name = "Frame";
  353. String getName() {
  354. return name;
  355. }
  356. void setName(String s) {
  357. name = s;
  358. }
  359. void setData(byte[] d) {
  360. data = d;
  361. }
  362. byte[] getData() {
  363. data[0] = 3;
  364. return data;
  365. }
  366. void setTime(byte t) {
  367. duration = t;
  368. }
  369. byte getTime() {
  370. return duration;
  371. }
  372. byte[] getLayer(int i) {
  373. return Arrays.copyOfRange(data, (i * 8), (i * 8) + 8);
  374. }
  375. }
  376. class Animation {
  377. ArrayList<AFrame> frames = new ArrayList<AFrame>();
  378. private int lastFrameIndex = 0;
  379. private String name = "Animation";
  380. String getName() {
  381. return name;
  382. }
  383. void setName(String s) {
  384. name = s;
  385. }
  386. AFrame get(int i) {
  387. try {
  388. return frames.get(i);
  389. } catch (IndexOutOfBoundsException e) {
  390. System.out.println(e.toString());
  391. return null;
  392. }
  393. }
  394. void set(AFrame f, int i) {
  395. if (lastFrameIndex <= i) {
  396. try {
  397. frames.set(i, f);
  398. } catch (IndexOutOfBoundsException e) {
  399. System.out.println(e.toString());
  400. }
  401. }
  402. }
  403. void remove(int i) {
  404. try {
  405. frames.remove(i);
  406. } catch (IndexOutOfBoundsException e) {
  407. System.out.println(e.toString());
  408. }
  409. }
  410. void add(int i) {
  411. try {
  412. frames.add(i, new AFrame());
  413. lastFrameIndex++;
  414. } catch (IndexOutOfBoundsException e) {
  415. System.out.println(e.toString());
  416. }
  417. }
  418. void add(int i, AFrame f) {
  419. try {
  420. frames.add(i, f);
  421. lastFrameIndex++;
  422. } catch (IndexOutOfBoundsException e) {
  423. System.out.println(e.toString());
  424. }
  425. }
  426. int size() {
  427. return frames.size();
  428. }
  429. }