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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(d, data);
  129. }
  130. frame.setData(d);
  131. d = hexConvert(sc.nextLine());
  132. frame.setTime(d[0]);
  133. return frame;
  134. }
  135. /*private static short[] invert(short[] a){
  136. short[] tmp = new short[a.length];
  137. int j = 0;
  138. for(int i = a.length-1; i >= 0; i--){
  139. tmp[j] = a[i];
  140. j++;
  141. }
  142. return tmp;
  143. }*/
  144. private static short[] concat(short[] a, short[] b) {
  145. short[] c = new short[a.length + b.length];
  146. System.arraycopy(a, 0, c, 0, a.length);
  147. System.arraycopy(b, 0, c, a.length, b.length);
  148. return c;
  149. }
  150. private static short[] hexConvert(String hex) {
  151. hex = hex.replaceAll("\\n", "");
  152. short[] tmp = new short[hex.length()/2];
  153. for (int i = 0; i < hex.length(); i=i+2){
  154. char[] tmpString = new char[2];
  155. tmpString[0] = hex.charAt(i);
  156. tmpString[1] = hex.charAt(i+1);
  157. String tmpS = String.copyValueOf(tmpString);
  158. if(i == 0){
  159. tmp[0] = Short.parseShort(tmpS, 16);
  160. } else {
  161. tmp[i/2] = Short.parseShort(tmpS, 16);
  162. }
  163. }
  164. return tmp;
  165. }
  166. private static void writeAnimation(Animation anim, FileWriter f, 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.get(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).substring(1) + "\n");
  182. }
  183. private static void writeLayer(short[] l, FileWriter f) throws IOException {
  184. String hex = "";
  185. for (int i = 0; i < l.length; i++) {
  186. hex += Integer.toString( (l[i] & 0xff) + 0x100, 16).substring(1);
  187. }
  188. hex += "\n";
  189. f.write(hex);
  190. }
  191. }