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.

AnimationUtility.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * AnimationUtility.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. import java.io.FileWriter;
  24. import java.io.File;
  25. import java.util.Scanner;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. /**
  29. * A helper class that loads animations from a file or saves them to one.
  30. * @author Thomas Buck
  31. * @author Max Nuding
  32. * @author Felix Bäder
  33. * @version 1.0
  34. */
  35. public class AnimationUtility {
  36. private static String lastError = null;
  37. /**
  38. * Read a file, return ArrayList with all animations in the file.
  39. * @param path Path of file
  40. * @return Populated ArrayList
  41. * @throws Excpetion When something goes wrong with the Scanner...
  42. */
  43. public static ArrayList<Animation> readFile(String path) throws Exception {
  44. Scanner sc = new Scanner(new File(path));
  45. ArrayList<Animation> animations = new ArrayList<Animation>();
  46. do {
  47. Animation tmp = readAnimation(sc);
  48. if (tmp == null) {
  49. return animations;
  50. }
  51. if (sc.hasNextLine()) {
  52. sc.nextLine();
  53. }
  54. animations.add(tmp);
  55. } while (sc.hasNextLine());
  56. return animations;
  57. }
  58. /**
  59. * Write a file with all Animations of an ArrayList
  60. * @param path Path to write to
  61. * @param animations ArrayList with all animations to be saved
  62. * @see AnimationUtility#getLastError() getLastError()
  63. */
  64. public static void writeFile(String path, ArrayList<Animation> animations) {
  65. File f = new File(path);
  66. if (f.exists()) {
  67. try {
  68. f.delete();
  69. } catch (Exception e) {
  70. lastError = e.toString();
  71. return;
  72. }
  73. }
  74. FileWriter output = null;
  75. try {
  76. output = new FileWriter(f);
  77. for (int i = 0; i < animations.size(); i++) {
  78. writeAnimation(animations.get(i), output, (i == (animations.size() - 1)));
  79. }
  80. } catch (Exception e) {
  81. lastError = e.toString();
  82. return;
  83. } finally {
  84. if (output != null) {
  85. try {
  86. output.close();
  87. } catch (Exception e) {
  88. lastError = e.toString();
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Get the last error that occured while writing
  95. * @return Text of the exception that occured
  96. * @see AnimationUtility#writeFile(String, ArrayList) writeFile()
  97. */
  98. public static String getLastError() {
  99. String tmp = lastError;
  100. lastError = null;
  101. return tmp;
  102. }
  103. private static Animation readAnimation(Scanner sc) {
  104. Animation anim = new Animation();
  105. AFrame f = null;
  106. int index = 0;
  107. String tmpSize = sc.nextLine().replaceAll("\\n", "");
  108. if (tmpSize.equals("")) {
  109. return null;
  110. }
  111. Integer tmpSizeAgain = new Integer(tmpSize);
  112. int size = tmpSizeAgain.intValue();
  113. anim.setName(sc.nextLine());
  114. while (size > 0) {
  115. f = readFrame(sc, index);
  116. anim.add(index, f);
  117. index++;
  118. size--;
  119. }
  120. return anim;
  121. }
  122. private static AFrame readFrame(Scanner sc, int index) {
  123. AFrame frame = new AFrame();
  124. frame.setName(sc.nextLine());
  125. short[] d = {};
  126. for (int i = 0; i < 8; i++) {
  127. short[] data = hexConvert(sc.nextLine());
  128. d = concat(data, d);
  129. }
  130. frame.setData(d);
  131. d = hexConvert(sc.nextLine());
  132. frame.setTime(d[0]);
  133. return frame;
  134. }
  135. private static short[] concat(short[] a, short[] b) {
  136. short[] c = new short[a.length + b.length];
  137. System.arraycopy(a, 0, c, 0, a.length);
  138. System.arraycopy(b, 0, c, a.length, b.length);
  139. return c;
  140. }
  141. private static short[] hexConvert(String hex) {
  142. hex = hex.replaceAll("\\n", "");
  143. short[] tmp = new short[hex.length()/2];
  144. for (int i = 0; i < hex.length(); i=i+2){
  145. char[] tmpString = new char[2];
  146. tmpString[0] = hex.charAt(i);
  147. tmpString[1] = hex.charAt(i+1);
  148. String tmpS = String.copyValueOf(tmpString);
  149. if(i == 0){
  150. tmp[0] = Short.parseShort(tmpS, 16);
  151. } else {
  152. tmp[i/2] = Short.parseShort(tmpS, 16);
  153. }
  154. }
  155. return tmp;
  156. }
  157. private static void writeAnimation(Animation anim, FileWriter f, boolean last) throws IOException {
  158. f.write(anim.size() + "\n");
  159. f.write(anim.getName() + "\n");
  160. for (int i = 0; i < anim.size(); i++) {
  161. writeFrame(anim.get(i), f);
  162. }
  163. if (!last) {
  164. f.write("\n");
  165. }
  166. }
  167. private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
  168. f.write(fr.getName() + "\n");
  169. for (int i = 0; i < 8; i++) {
  170. writeLayer(fr.getLayer(i), f);
  171. }
  172. f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
  173. }
  174. private static void writeLayer(short[] l, FileWriter f) throws IOException {
  175. String hex = "";
  176. for (int i = 0; i < l.length; i++) {
  177. hex += Integer.toString( (l[i] & 0xff) + 0x100, 16).substring(1);
  178. }
  179. hex += "\n";
  180. f.write(hex);
  181. }
  182. }