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

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