Simple single-color 8x8x8 LED Cube with AVRs
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AnimationUtility.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 Animation[] readFile(String path) throws Exception {
  47. Scanner sc = new Scanner(new File(path));
  48. Animation[] animations = new Animation[0];
  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 = extendArray(animations);
  58. animations[animations.length - 1] = tmp;
  59. } while (sc.hasNextLine());
  60. return animations;
  61. }
  62. private static Animation[] extendArray(Animation[] animationArray) {
  63. Animation newArray[] = new Animation[animationArray.length + 1];
  64. for (int i = 0; i < animationArray.length; i++) {
  65. newArray[i] = animationArray[i];
  66. }
  67. return newArray;
  68. }
  69. /**
  70. * Write a file with all Animations of an ArrayList
  71. *
  72. * @param path Path to write to
  73. * @param animations Array with all animations to be saved
  74. * @see AnimationUtility#getLastError() getLastError()
  75. */
  76. public static void writeFile(String path, Animation[] animations) {
  77. File f = new File(path);
  78. if (f.exists()) {
  79. try {
  80. f.delete();
  81. } catch (Exception e) {
  82. lastError = e.toString();
  83. return;
  84. }
  85. }
  86. FileWriter output = null;
  87. try {
  88. output = new FileWriter(f);
  89. for (int i = 0; i < animations.length; i++) {
  90. writeAnimation(animations[i], output, (i == (animations.length - 1)));
  91. }
  92. } catch (Exception e) {
  93. lastError = e.toString();
  94. return;
  95. } finally {
  96. if (output != null) {
  97. try {
  98. output.close();
  99. } catch (Exception e) {
  100. lastError = e.toString();
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Get the last error that occured while writing
  107. *
  108. * @return Text of the exception that occured
  109. */
  110. public static String getLastError() {
  111. String tmp = lastError;
  112. lastError = null;
  113. return tmp;
  114. }
  115. private static Animation readAnimation(Scanner sc) {
  116. Animation anim = new Animation();
  117. AFrame f = null;
  118. int index = 0;
  119. String tmpSize = sc.nextLine().replaceAll("\\n", "");
  120. if (tmpSize.equals("")) {
  121. return null;
  122. }
  123. Integer tmpSizeAgain = new Integer(tmpSize);
  124. int size = tmpSizeAgain.intValue();
  125. anim.setName(sc.nextLine());
  126. while (size > 0) {
  127. f = readFrame(sc, index);
  128. anim.setFrame(f, index);
  129. index++;
  130. size--;
  131. }
  132. return anim;
  133. }
  134. private static AFrame readFrame(Scanner sc, int index) {
  135. AFrame frame = new AFrame();
  136. frame.setName(sc.nextLine());
  137. short[] d = {};
  138. for (int i = 0; i < 8; i++) {
  139. short[] data = hexConvert(sc.nextLine());
  140. d = concat(d, data);
  141. }
  142. frame.setData(d);
  143. d = hexConvert(sc.nextLine());
  144. frame.setTime(d[0]);
  145. return frame;
  146. }
  147. /*
  148. * private static short[] invert(short[] a){ short[] tmp = new
  149. * short[a.length]; int j = 0; for(int i = a.length-1; i >= 0; i--){ tmp[j]
  150. * = a[i]; j++; } return tmp; }
  151. */
  152. private static short[] concat(short[] a, short[] b) {
  153. short[] c = new short[a.length + b.length];
  154. System.arraycopy(a, 0, c, 0, a.length);
  155. System.arraycopy(b, 0, c, a.length, b.length);
  156. return c;
  157. }
  158. private static short[] hexConvert(String hex) {
  159. hex = hex.replaceAll("\\n", "");
  160. short[] tmp = new short[hex.length() / 2];
  161. for (int i = 0; i < hex.length(); i = i + 2) {
  162. char[] tmpString = new char[2];
  163. tmpString[0] = hex.charAt(i);
  164. tmpString[1] = hex.charAt(i + 1);
  165. String tmpS = String.copyValueOf(tmpString);
  166. if (i == 0) {
  167. tmp[0] = Short.parseShort(tmpS, 16);
  168. } else {
  169. tmp[i / 2] = Short.parseShort(tmpS, 16);
  170. }
  171. }
  172. return tmp;
  173. }
  174. private static void writeAnimation(Animation anim, FileWriter f,
  175. boolean last) throws IOException {
  176. f.write(anim.size() + "\n");
  177. f.write(anim.getName() + "\n");
  178. for (int i = 0; i < anim.size(); i++) {
  179. writeFrame(anim.getFrame(i), f);
  180. }
  181. if (!last) {
  182. f.write("\n");
  183. }
  184. }
  185. private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
  186. f.write(fr.getName() + "\n");
  187. for (int i = 0; i < 8; i++) {
  188. writeLayer(fr.getLayer(i), f);
  189. }
  190. f.write(Integer.toString((fr.getTime() & 0xff) + 0x100, 16)
  191. .substring(1) + "\n");
  192. }
  193. private static void writeLayer(short[] l, FileWriter f) throws IOException {
  194. String hex = "";
  195. for (int i = 0; i < l.length; i++) {
  196. hex += Integer.toString((l[i] & 0xff) + 0x100, 16).substring(1);
  197. }
  198. hex += "\n";
  199. f.write(hex);
  200. }
  201. }