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.

cubeWorker.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * cubeWorker.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. /*
  24. * This class handles one animation file. This file can contain
  25. * many animations, but has to be only 1Mbit in size (128*1024 Byte).
  26. */
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.Scanner;
  30. import java.io.FileWriter;
  31. import java.io.File;
  32. import java.io.IOException;
  33. public class cubeWorker {
  34. // --------------------
  35. // Definitions
  36. // --------------------
  37. final int UP = 0;
  38. final int DOWN = 1;
  39. // --------------------
  40. // Fields
  41. // --------------------
  42. private ArrayList<Animation> animations = new ArrayList<Animation>();
  43. private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
  44. private boolean changedState = false;
  45. // --------------------
  46. cubeWorker() {
  47. animations.add(new Animation());
  48. }
  49. cubeWorker(ArrayList<Animation> anims) {
  50. animations = anims;
  51. }
  52. // --------------------
  53. // Misc. Methods
  54. // --------------------
  55. // Returns how many animations are defined
  56. public int numOfAnimations() {
  57. return animations.size();
  58. }
  59. // Returns how many frames are in the current animation
  60. public int numOfFrames(int selectedAnimation) {
  61. return animations.get(selectedAnimation).size();
  62. }
  63. // Tells how many Frames you can add until you reached 1 Mbit...
  64. public int framesRemaining() {
  65. return framesRemaining;
  66. }
  67. // --------------------
  68. // Animation Specific
  69. // --------------------
  70. // Adds a new Animation
  71. // Returns id if ok, -1 if error or not enough space for
  72. // another animation
  73. public int addAnimation() {
  74. changedState = true;
  75. if (framesRemaining <= 0) {
  76. return -1;
  77. } else {
  78. int s = animations.size();
  79. animations.add(s + 1, new Animation());
  80. return s;
  81. }
  82. }
  83. // Removes an animation
  84. public void removeAnimation(int selectedAnimation) {
  85. changedState = true;
  86. animations.remove(selectedAnimation);
  87. selectedAnimation = 0;
  88. }
  89. public String getAnimationName(int selectedAnimation) {
  90. return animations.get(selectedAnimation).getName();
  91. }
  92. public void setAnimationName(String s, int selectedAnimation) {
  93. changedState = true;
  94. animations.get(selectedAnimation).setName(s);
  95. }
  96. public void moveAnimation(int dir, int selectedAnimation) {
  97. changedState = true;
  98. if (dir == UP){
  99. //animation moved up
  100. if (selectedAnimation > 0) {
  101. Animation tmp = animations.get(selectedAnimation);
  102. animations.set(selectedAnimation, animations.get(selectedAnimation - 1));
  103. animations.set(selectedAnimation - 1, tmp);
  104. }
  105. } else if (dir == DOWN){
  106. //animation moved down
  107. if (selectedAnimation < (animations.size() - 1)) {
  108. Animation tmp = animations.get(selectedAnimation);
  109. animations.set(selectedAnimation, animations.get(selectedAnimation + 1));
  110. animations.set(selectedAnimation + 1, tmp);
  111. }
  112. }
  113. }
  114. // --------------------
  115. // Frame Specific
  116. // --------------------
  117. public String getFrameName(int anim, int frame) {
  118. return animations.get(anim).get(frame).getName();
  119. }
  120. public void setFrameName(String s, int anim, int frame) {
  121. changedState = true;
  122. animations.get(anim).get(frame).setName(s);
  123. }
  124. // Adds a Frame to the current animation.
  125. // Returns id if okay, -1 if error
  126. public int addFrame(int anim) {
  127. changedState = true;
  128. if (framesRemaining <= 0) {
  129. return -1;
  130. }
  131. framesRemaining--;
  132. int s = animations.get(anim).size();
  133. animations.get(anim).add(s);
  134. animations.get(anim).get(s).setName("Frame " + (2016 - framesRemaining));
  135. return s;
  136. }
  137. // Remove the frame
  138. public void removeFrame(int anim, int frame) {
  139. changedState = true;
  140. animations.get(anim).remove(frame);
  141. }
  142. // Returns array with 64 bytes with led values
  143. public byte[] getFrame(int anim, int frame) {
  144. return animations.get(anim).get(frame).getData();
  145. }
  146. public void setFrame(byte[] data, int anim, int frame) {
  147. changedState = true;
  148. animations.get(anim).get(frame).setData(data);
  149. }
  150. // Frame duration in 1/24th of a second
  151. public byte getFrameTime(int anim, int frame) {
  152. return animations.get(anim).get(frame).getTime();
  153. }
  154. public void setFrameTime(byte time, int anim, int frame) {
  155. changedState = true;
  156. animations.get(anim).get(frame).setTime(time);
  157. }
  158. public void moveFrame(int dir, int anim, int frame){
  159. changedState = true;
  160. if (dir == UP){
  161. // frame moved up
  162. if (frame > 0) {
  163. AFrame tmp = animations.get(anim).get(frame);
  164. animations.get(anim).set(animations.get(anim).get(frame - 1), frame);
  165. animations.get(anim).set(tmp, frame - 1);
  166. }
  167. } else if (dir == DOWN){
  168. // frame moved down
  169. if (frame < (animations.get(anim).size() - 1)) {
  170. AFrame tmp = animations.get(anim).get(frame);
  171. animations.get(anim).set(animations.get(anim).get(frame + 1), frame);
  172. animations.get(anim).set(tmp, frame + 1);
  173. }
  174. }
  175. }
  176. // --------------------
  177. // File Specific
  178. // --------------------
  179. // Loads an animation file into this object
  180. public int loadState(String path) {
  181. changedState = false;
  182. try {
  183. animations = AnimationUtility.readFile(path);
  184. } catch (Exception e) {
  185. System.out.println(e.toString());
  186. return -1;
  187. }
  188. int size = 0;
  189. for (int i = 0; i < animations.size(); i++) {
  190. size += animations.get(i).size();
  191. }
  192. framesRemaining = 2016 - size;
  193. if (size > 2016) {
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. // Saves the state of this object in an animation file
  199. public int saveState(String path) {
  200. changedState = false;
  201. AnimationUtility.writeFile(path, animations);
  202. if (AnimationUtility.getLastError() != null) {
  203. System.out.println(AnimationUtility.getLastError());
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. // Returns true if last saved state != current state
  209. public boolean changedStateSinceSave() {
  210. return changedState;
  211. }
  212. // --------------------
  213. // Serial Port Specific
  214. // --------------------
  215. // sends state of object to led cube
  216. public int uploadState(String port) {
  217. return 0;
  218. }
  219. // loads all state from the cube into this object
  220. public int downloadState(String port) {
  221. return 0;
  222. }
  223. public String[] getSerialPorts() {
  224. String[] sPorts = { "Select serial port..." }; // Has to be the first entry
  225. return sPorts;
  226. }
  227. // --------------------
  228. }
  229. class AnimationUtility {
  230. private static String lastError = null;
  231. public static ArrayList<Animation> readFile(String path) throws Exception {
  232. Scanner sc = new Scanner(new File(path));
  233. ArrayList<Animation> animations = new ArrayList<Animation>();
  234. do {
  235. animations.add(readAnimation(sc));
  236. } while (sc.hasNextLine());
  237. return animations;
  238. }
  239. public static void writeFile(String path, ArrayList<Animation> animations) {
  240. File f = new File(path);
  241. if (f.exists()) {
  242. try {
  243. f.delete();
  244. } catch (Exception e) {
  245. lastError = e.toString();
  246. return;
  247. }
  248. }
  249. FileWriter output = null;
  250. try {
  251. output = new FileWriter(f);
  252. for (int i = 0; i < animations.size(); i++) {
  253. writeAnimation(animations.get(i), output);
  254. }
  255. } catch (Exception e) {
  256. lastError = e.toString();
  257. return;
  258. } finally {
  259. if (output != null) {
  260. try {
  261. output.close();
  262. } catch (Exception e) {
  263. lastError = e.toString();
  264. }
  265. }
  266. }
  267. }
  268. public static String getLastError() {
  269. String tmp = lastError;
  270. lastError = null;
  271. return tmp;
  272. }
  273. private static Animation readAnimation(Scanner sc) {
  274. Animation anim = new Animation();
  275. AFrame f = null;
  276. int index = 0;
  277. int size = sc.nextInt();
  278. anim.setName(sc.nextLine());
  279. while (size > 0) {
  280. f = readFrame(sc, index);
  281. anim.add(index);
  282. anim.set(f, index);
  283. index++;
  284. size--;
  285. }
  286. return anim;
  287. }
  288. private static AFrame readFrame(Scanner sc, int index) {
  289. AFrame frame = new AFrame();
  290. frame.setName("Frame " + index);
  291. byte[] d = {};
  292. for (int i = 0; i < 8; i++) {
  293. byte[] data = hexConvert(sc.nextLine());
  294. d = concat(data, d);
  295. }
  296. frame.setData(d);
  297. d = hexConvert(sc.nextLine());
  298. frame.setTime(d[0]);
  299. return frame;
  300. }
  301. private static byte[] concat(byte[] a, byte[] b) {
  302. byte[] c = new byte[a.length + b.length];
  303. System.arraycopy(a, 0, c, 0, a.length);
  304. System.arraycopy(b, 0, c, a.length, b.length);
  305. return c;
  306. }
  307. private static byte[] hexConvert(String hex) {
  308. hex = hex.replaceAll("\\n", "");
  309. int length = hex.length();
  310. byte[] data = new byte[length / 2];
  311. for (int i = 0; i < length; i += 2) {
  312. data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
  313. }
  314. return data;
  315. }
  316. private static void writeAnimation(Animation anim, FileWriter f) throws IOException {
  317. f.write(anim.size() + "\n");
  318. f.write(anim.getName() + "\n");
  319. for (int i = 0; i < anim.size(); i++) {
  320. writeFrame(anim.get(i), f);
  321. }
  322. f.write("\n");
  323. }
  324. private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
  325. for (int i = 0; i < 8; i++) {
  326. writeLayer(fr.getLayer(i), f);
  327. }
  328. f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
  329. }
  330. private static void writeLayer(byte[] l, FileWriter f) throws IOException {
  331. String hex = "";
  332. for (int i = 0; i < l.length; i++) {
  333. hex += Integer.toString( (l[i] & 0xff) + 0x100, 16).substring(1);
  334. }
  335. hex += "\n";
  336. f.write(hex);
  337. }
  338. }
  339. class AFrame {
  340. private byte[] data = new byte[64];
  341. private byte duration = 1;
  342. private String name = "Frame";
  343. String getName() {
  344. return name;
  345. }
  346. void setName(String s) {
  347. name = s;
  348. }
  349. void setData(byte[] d) {
  350. data = d;
  351. }
  352. byte[] getData() {
  353. return data;
  354. }
  355. void setTime(byte t) {
  356. duration = t;
  357. }
  358. byte getTime() {
  359. return duration;
  360. }
  361. byte[] getLayer(int i) {
  362. return Arrays.copyOfRange(data, (i * 8), (i * 8) + 8);
  363. }
  364. }
  365. class Animation {
  366. private ArrayList<AFrame> frames = new ArrayList<AFrame>();
  367. private int lastFrameIndex = 0;
  368. private String name = "Animation";
  369. String getName() {
  370. return name;
  371. }
  372. void setName(String s) {
  373. name = s;
  374. }
  375. AFrame get(int i) {
  376. try {
  377. return frames.get(i);
  378. } catch (IndexOutOfBoundsException e) {
  379. System.out.println(e.toString());
  380. return null;
  381. }
  382. }
  383. void set(AFrame f, int i) {
  384. if (lastFrameIndex <= i) {
  385. try {
  386. frames.set(i, f);
  387. } catch (IndexOutOfBoundsException e) {
  388. System.out.println(e.toString());
  389. }
  390. }
  391. }
  392. void remove(int i) {
  393. try {
  394. frames.remove(i);
  395. } catch (IndexOutOfBoundsException e) {
  396. System.out.println(e.toString());
  397. }
  398. }
  399. void add(int i) {
  400. try {
  401. frames.add(i, new AFrame());
  402. lastFrameIndex++;
  403. } catch (IndexOutOfBoundsException e) {
  404. System.out.println(e.toString());
  405. }
  406. }
  407. int size() {
  408. return frames.size();
  409. }
  410. }