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

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