Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cubeWorker.java 12KB

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