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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * AnimationUtility.java
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. import java.io.FileWriter;
  22. import java.io.File;
  23. import java.util.Scanner;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * A helper class that loads animations from a file or saves them to one.
  29. *
  30. * @author Thomas Buck
  31. * @version 1.0
  32. */
  33. public class AnimationUtility {
  34. private static String lastError = null;
  35. /**
  36. * Read a file, return ArrayList with all animations in the file.
  37. *
  38. * @param path Path of file
  39. * @return Populated ArrayList
  40. * @throws Excpetion When something goes wrong with the Scanner...
  41. */
  42. public static Animation[] readFile(String path) throws Exception {
  43. Scanner sc = new Scanner(new File(path));
  44. Animation[] animations = new Animation[0];
  45. do {
  46. Animation tmp = readAnimation(sc);
  47. if (tmp == null) {
  48. return animations;
  49. }
  50. if (sc.hasNextLine()) {
  51. sc.nextLine();
  52. }
  53. animations = extendArray(animations);
  54. animations[animations.length - 1] = tmp;
  55. } while (sc.hasNextLine());
  56. return animations;
  57. }
  58. private static Animation[] extendArray(Animation[] animationArray) {
  59. Animation newArray[] = new Animation[animationArray.length + 1];
  60. for (int i = 0; i < animationArray.length; i++) {
  61. newArray[i] = animationArray[i];
  62. }
  63. return newArray;
  64. }
  65. /**
  66. * Write a file with all Animations of an ArrayList
  67. *
  68. * @param path Path to write to
  69. * @param animations Array with all animations to be saved
  70. * @see AnimationUtility#getLastError() getLastError()
  71. */
  72. public static void writeFile(String path, Animation[] animations) {
  73. File f = new File(path);
  74. if (f.exists()) {
  75. try {
  76. f.delete();
  77. } catch (Exception e) {
  78. lastError = e.toString();
  79. return;
  80. }
  81. }
  82. FileWriter output = null;
  83. try {
  84. output = new FileWriter(f);
  85. for (int i = 0; i < animations.length; i++) {
  86. writeAnimation(animations[i], output, (i == (animations.length - 1)));
  87. }
  88. } catch (Exception e) {
  89. lastError = e.toString();
  90. return;
  91. } finally {
  92. if (output != null) {
  93. try {
  94. output.close();
  95. } catch (Exception e) {
  96. lastError = e.toString();
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Get the last error that occured while writing
  103. *
  104. * @return Text of the exception that occured
  105. */
  106. public static String getLastError() {
  107. String tmp = lastError;
  108. lastError = null;
  109. return tmp;
  110. }
  111. private static Animation readAnimation(Scanner sc) {
  112. Animation anim = new Animation();
  113. AFrame f = null;
  114. int index = 0;
  115. String tmpSize = sc.nextLine().replaceAll("\\n", "");
  116. if (tmpSize.equals("")) {
  117. return null;
  118. }
  119. Integer tmpSizeAgain = new Integer(tmpSize);
  120. int size = tmpSizeAgain.intValue();
  121. anim.setName(sc.nextLine());
  122. while (size > 0) {
  123. f = readFrame(sc, index);
  124. anim.setFrame(f, index);
  125. index++;
  126. size--;
  127. }
  128. return anim;
  129. }
  130. private static AFrame readFrame(Scanner sc, int index) {
  131. AFrame frame = new AFrame();
  132. frame.setName(sc.nextLine());
  133. short[] d = {};
  134. for (int i = 0; i < 8; i++) {
  135. short[] data = hexConvert(sc.nextLine());
  136. d = concat(d, data);
  137. }
  138. frame.setData(d);
  139. d = hexConvert(sc.nextLine());
  140. frame.setTime(d[0]);
  141. return frame;
  142. }
  143. private static short[] concat(short[] a, short[] b) {
  144. short[] c = new short[a.length + b.length];
  145. System.arraycopy(a, 0, c, 0, a.length);
  146. System.arraycopy(b, 0, c, a.length, b.length);
  147. return c;
  148. }
  149. private static short[] hexConvert(String hex) {
  150. hex = hex.replaceAll("\\n", "");
  151. short[] tmp = new short[hex.length() / 2];
  152. for (int i = 0; i < hex.length(); i = i + 2) {
  153. char[] tmpString = new char[2];
  154. tmpString[0] = hex.charAt(i);
  155. tmpString[1] = hex.charAt(i + 1);
  156. String tmpS = String.copyValueOf(tmpString);
  157. if (i == 0) {
  158. tmp[0] = Short.parseShort(tmpS, 16);
  159. } else {
  160. tmp[i / 2] = Short.parseShort(tmpS, 16);
  161. }
  162. }
  163. return tmp;
  164. }
  165. private static void writeAnimation(Animation anim, FileWriter f,
  166. boolean last) throws IOException {
  167. f.write(anim.size() + "\n");
  168. f.write(anim.getName() + "\n");
  169. for (int i = 0; i < anim.size(); i++) {
  170. writeFrame(anim.getFrame(i), f);
  171. }
  172. if (!last) {
  173. f.write("\n");
  174. }
  175. }
  176. private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
  177. f.write(fr.getName() + "\n");
  178. for (int i = 0; i < 8; i++) {
  179. writeLayer(fr.getLayer(i), f);
  180. }
  181. f.write(Integer.toString((fr.getTime() & 0xff) + 0x100, 16)
  182. .substring(1) + "\n");
  183. }
  184. private static void writeLayer(short[] l, FileWriter f) throws IOException {
  185. String hex = "";
  186. for (int i = 0; i < l.length; i++) {
  187. hex += Integer.toString((l[i] & 0xff) + 0x100, 16).substring(1);
  188. }
  189. hex += "\n";
  190. f.write(hex);
  191. }
  192. }