Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AnimationUtility.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. private static short[] concat(short[] a, short[] b) {
  148. short[] c = new short[a.length + b.length];
  149. System.arraycopy(a, 0, c, 0, a.length);
  150. System.arraycopy(b, 0, c, a.length, b.length);
  151. return c;
  152. }
  153. private static short[] hexConvert(String hex) {
  154. hex = hex.replaceAll("\\n", "");
  155. short[] tmp = new short[hex.length() / 2];
  156. for (int i = 0; i < hex.length(); i = i + 2) {
  157. char[] tmpString = new char[2];
  158. tmpString[0] = hex.charAt(i);
  159. tmpString[1] = hex.charAt(i + 1);
  160. String tmpS = String.copyValueOf(tmpString);
  161. if (i == 0) {
  162. tmp[0] = Short.parseShort(tmpS, 16);
  163. } else {
  164. tmp[i / 2] = Short.parseShort(tmpS, 16);
  165. }
  166. }
  167. return tmp;
  168. }
  169. private static void writeAnimation(Animation anim, FileWriter f,
  170. boolean last) throws IOException {
  171. f.write(anim.size() + "\n");
  172. f.write(anim.getName() + "\n");
  173. for (int i = 0; i < anim.size(); i++) {
  174. writeFrame(anim.getFrame(i), f);
  175. }
  176. if (!last) {
  177. f.write("\n");
  178. }
  179. }
  180. private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
  181. f.write(fr.getName() + "\n");
  182. for (int i = 0; i < 8; i++) {
  183. writeLayer(fr.getLayer(i), f);
  184. }
  185. f.write(Integer.toString((fr.getTime() & 0xff) + 0x100, 16)
  186. .substring(1) + "\n");
  187. }
  188. private static void writeLayer(short[] l, FileWriter f) throws IOException {
  189. String hex = "";
  190. for (int i = 0; i < l.length; i++) {
  191. hex += Integer.toString((l[i] & 0xff) + 0x100, 16).substring(1);
  192. }
  193. hex += "\n";
  194. f.write(hex);
  195. }
  196. }