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