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 13KB

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