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

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