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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.List;
  29. import java.util.StringTokenizer;
  30. /**
  31. * This class holds all Data of the Application. Additionally it performs the
  32. * transmission of animation data to/from the cube and saves/loads animations
  33. * in/from a file.
  34. *
  35. * @author Thomas Buck
  36. * @author Felix Bäder
  37. * @author Max Nuding
  38. * @version 1.0
  39. */
  40. public class cubeWorker {
  41. // --------------------
  42. // Definitions
  43. // --------------------
  44. final int UP = 0;
  45. final int DOWN = 1;
  46. // --------------------
  47. // Fields
  48. // --------------------
  49. private List<Animation> animations = new ArrayList<Animation>();
  50. private final int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
  51. private boolean changedState = false;
  52. private Frame parentFrame;
  53. // --------------------
  54. /**
  55. * Creates a worker with one animation, containing an empty frame.
  56. */
  57. public cubeWorker(Frame parent) {
  58. animations.add(new Animation());
  59. animations.get(0).setName("Animation 1");
  60. animations.get(0).add(0);
  61. animations.get(0).get(0).setName("Frame 1");
  62. parentFrame = parent;
  63. }
  64. /**
  65. * Creates a worker from the given animation list
  66. *
  67. * @param anims List of animations
  68. */
  69. public cubeWorker(List<Animation> anims, Frame parent) {
  70. animations = anims;
  71. parentFrame = parent;
  72. }
  73. /**
  74. * Returns number of frames in this cubeWorker.
  75. *
  76. * @return number of frames.
  77. */
  78. public int completeNumOfFrames() {
  79. int count = 0;
  80. for (int i = 0; i < numOfAnimations(); i++) {
  81. count += numOfFrames(i);
  82. }
  83. return count;
  84. }
  85. // --------------------
  86. // Misc. Methods
  87. // --------------------
  88. /**
  89. * Get the number of animations in this worker.
  90. *
  91. * @return number of animations
  92. */
  93. public int numOfAnimations() {
  94. return animations.size();
  95. }
  96. /**
  97. * Get the number of frames in an animation.
  98. *
  99. * @param selectedAnimation the animation you want to check
  100. * @return number of frames in this animation
  101. */
  102. public int numOfFrames(int selectedAnimation) {
  103. return animations.get(selectedAnimation).size();
  104. }
  105. /**
  106. * Get the number of frames you can add until the Cubes memory is full.
  107. *
  108. * @return number of frames remaining
  109. */
  110. public int framesRemaining() {
  111. return framesRemaining - completeNumOfFrames();
  112. }
  113. // --------------------
  114. // Animation Specific
  115. // --------------------
  116. /**
  117. * Add an animation.
  118. *
  119. * @return Index of new animation, or -1 if not enough space remaining.
  120. */
  121. public int addAnimation() {
  122. changedState = true;
  123. if (framesRemaining() <= 0) {
  124. return -1;
  125. } else {
  126. int s = animations.size();
  127. animations.add(s, new Animation());
  128. animations.get(s).setName("Animation " + animations.size());
  129. return s;
  130. }
  131. }
  132. /**
  133. * Remove an animation.
  134. *
  135. * @param selectedAnimation the animation you want to delete
  136. */
  137. public void removeAnimation(int selectedAnimation) {
  138. changedState = true;
  139. animations.remove(selectedAnimation);
  140. }
  141. /**
  142. * Get the name of an animation
  143. *
  144. * @return The name
  145. * @param selectedAnimation The animation you want to get the name from
  146. */
  147. public String getAnimationName(int selectedAnimation) {
  148. return animations.get(selectedAnimation).getName();
  149. }
  150. /**
  151. * Set the name of an animation  @param s New name
  152. *
  153. * @param selectedAnimation Index of the animation you want to change
  154. */
  155. public void setAnimationName(String s, int selectedAnimation) {
  156. changedState = true;
  157. animations.get(selectedAnimation).setName(s);
  158. }
  159. /**
  160. * Move an animation UP or DOWN.
  161. *
  162. * @param dir Direction. Use UP and DOWN defined in cubeWorker
  163. * @param selectedAnimation Animation you want to move
  164. */
  165. public void moveAnimation(int dir, int selectedAnimation) {
  166. changedState = true;
  167. if (dir == UP) {
  168. // animation moved up
  169. if (selectedAnimation > 0) {
  170. Animation tmp = animations.get(selectedAnimation);
  171. animations.set(selectedAnimation,
  172. animations.get(selectedAnimation - 1));
  173. animations.set(selectedAnimation - 1, tmp);
  174. }
  175. } else if (dir == DOWN) {
  176. // animation moved down
  177. if (selectedAnimation < (animations.size() - 1)) {
  178. Animation tmp = animations.get(selectedAnimation);
  179. animations.set(selectedAnimation,
  180. animations.get(selectedAnimation + 1));
  181. animations.set(selectedAnimation + 1, tmp);
  182. }
  183. }
  184. }
  185. // --------------------
  186. // Frame Specific
  187. // --------------------
  188. /**
  189. * Get the name of a frame.
  190. *
  191. * @param anim Animation the frame is in
  192. * @param frame Index of the frame
  193. */
  194. public String getFrameName(int anim, int frame) {
  195. return animations.get(anim).get(frame).getName();
  196. }
  197. /**
  198. * Set the name of a frame.
  199. *
  200. * @param s New name
  201. * @param anim Animation Index
  202. * @param frame Frame Index
  203. */
  204. public void setFrameName(String s, int anim, int frame) {
  205. changedState = true;
  206. animations.get(anim).get(frame).setName(s);
  207. }
  208. /**
  209. * Add a Frame to an animation.
  210. *
  211. * @return Index of new Frame or -1 if not enough space
  212. * @param anim Animation Index
  213. */
  214. public int addFrame(int anim) {
  215. changedState = true;
  216. if (framesRemaining() <= 0) {
  217. return -1;
  218. }
  219. int s = animations.get(anim).size();
  220. animations.get(anim).add(s);
  221. animations.get(anim).get(s)
  222. .setName("Frame " + animations.get(anim).size());
  223. return s;
  224. }
  225. /**
  226. * Remove a frame.
  227. *
  228. * @param anim Animation Index
  229. * @param frame Frame you want to remove
  230. */
  231. public void removeFrame(int anim, int frame) {
  232. changedState = true;
  233. animations.get(anim).remove(frame);
  234. }
  235. /**
  236. * Get the data of a frame.
  237. *
  238. * @param anim Animation Index  @param frame Frame Index
  239. * @return 64 byte array with data (8 bits per byte => 512 bits)
  240. */
  241. public short[] getFrame(int anim, int frame) {
  242. return animations.get(anim).get(frame).getData();
  243. }
  244. /**
  245. * Set the data of a frame
  246. *
  247. * @param data 64 byte array with data
  248. * @param anim Animation Index
  249. * @param frame Frame Index
  250. * @see cubeWorker#getFrame(int, int) getFrame()
  251. */
  252. public void setFrame(short[] data, int anim, int frame) {
  253. changedState = true;
  254. animations.get(anim).get(frame).setData(data);
  255. }
  256. /**
  257. * Get frame duration.
  258. *
  259. * @param anim Animation Index
  260. * @param frame Frame Index
  261. * @return Duration. 0 means 1/24th of a second.
  262. */
  263. public short getFrameTime(int anim, int frame) {
  264. return animations.get(anim).get(frame).getTime();
  265. }
  266. /**
  267. * Set the frames duration.
  268. *
  269. * @param time New duration
  270. * @param anim Animation Index
  271. * @param frame Frame Index
  272. * @see cubeWorker#getFrameTime(int, int) getFrameTime()
  273. */
  274. public void setFrameTime(short time, int anim, int frame) {
  275. changedState = true;
  276. animations.get(anim).get(frame).setTime(time);
  277. }
  278. /**
  279. * Move a frame.
  280. *
  281. * @param dir Direction to move. Use UP and DOWN from cubeWorker
  282. * @param anim Animation Index
  283. * @param frame Frame Index
  284. * @see cubeWorker#moveAnimation(int, int) moveAnimation()
  285. */
  286. public void moveFrame(int dir, int anim, int frame) {
  287. changedState = true;
  288. if (dir == UP) {
  289. // frame moved up
  290. if (frame > 0) {
  291. AFrame tmp = animations.get(anim).frames.get(frame);
  292. animations.get(anim).frames.set(frame,
  293. animations.get(anim).frames.get(frame - 1));
  294. animations.get(anim).frames.set(frame - 1, tmp);
  295. }
  296. } else if (dir == DOWN) {
  297. // frame moved down
  298. if (frame < (animations.get(anim).size() - 1)) {
  299. AFrame tmp = animations.get(anim).frames.get(frame);
  300. animations.get(anim).frames.set(frame,
  301. animations.get(anim).frames.get(frame + 1));
  302. animations.get(anim).frames.set(frame + 1, tmp);
  303. }
  304. }
  305. }
  306. // --------------------
  307. // File Specific
  308. // --------------------
  309. /**
  310. * Loads an animation file into this worker.
  311. *
  312. * @param path Path of file to load
  313. * @return 0 on success, -1 on error.
  314. */
  315. public int loadState(String path) {
  316. changedState = false;
  317. try {
  318. animations = AnimationUtility.readFile(path);
  319. } catch (Exception e) {
  320. System.out.println("Did not load!");
  321. e.printStackTrace(System.out);
  322. return -1;
  323. }
  324. int size = 0;
  325. for (int i = 0; i < animations.size(); i++) {
  326. size += animations.get(i).size();
  327. }
  328. if (size > 2016) {
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. /**
  334. * Save the state of this object into a file.
  335. *
  336. * @param path Path to save file in
  337. * @return 0 on success, -1 on error
  338. */
  339. public int saveState(String path) {
  340. changedState = false;
  341. AnimationUtility.writeFile(path, animations);
  342. if (AnimationUtility.getLastError() != null) {
  343. System.out.println(AnimationUtility.getLastError());
  344. return -1;
  345. }
  346. return 0;
  347. }
  348. /**
  349. * Check if something changed after loading/saving.
  350. *
  351. * @return TRUE if something changed, FALSE otherwise
  352. */
  353. public boolean changedStateSinceSave() {
  354. return changedState;
  355. }
  356. // --------------------
  357. // Serial Port Specific
  358. // --------------------
  359. /**
  360. * Send all animations to the cube.
  361. *
  362. * @param port Name of serial port to use
  363. * @return 0 on success, -1 on error
  364. */
  365. public int uploadState(String port) {
  366. try {
  367. SerialHelper sh = new SerialHelper(port, parentFrame);
  368. int ret = sh.sendAnimationsToCube(this);
  369. sh.closeSerialPort();
  370. return ret;
  371. } catch (Exception e) {
  372. System.out.println("Serial Exception: " + e.getMessage());
  373. return -1;
  374. }
  375. }
  376. /**
  377. * Get all animations from the cube, place it in this object
  378. *
  379. * @param port Name of serial port to use
  380. * @return 0 on success, -1 on error
  381. */
  382. public int downloadState(String port) {
  383. try {
  384. SerialHelper sh = new SerialHelper(port, parentFrame);
  385. cubeWorker ret = sh.getAnimationsFromCube();
  386. sh.closeSerialPort();
  387. if (ret == null) {
  388. return -1;
  389. } else {
  390. changedState = true;
  391. animations = ret.getAnimationList();
  392. return 0;
  393. }
  394. } catch (Exception e) {
  395. System.out.println("Serial Exception: " + e.getMessage());
  396. return -1;
  397. }
  398. }
  399. /**
  400. * Get the list used internally to store all the animations.
  401. * @return The list.
  402. */
  403. public List<Animation> getAnimationList() {
  404. return animations;
  405. }
  406. /**
  407. * Try to speak with the cube.
  408. *
  409. * @return TRUE if cube responds
  410. * @param port Name of serial port
  411. */
  412. public boolean probeCubeConnected(String port) {
  413. return false;
  414. }
  415. /**
  416. * Get the names of all available serial ports.
  417. *
  418. * @return Array of port names. First entry is "Select serial port..." if no
  419. * others
  420. */
  421. public String[] getSerialPorts() {
  422. String[] ports = { "Select serial port..." };
  423. String portLines = HelperUtility.getPorts();
  424. if (portLines == null) {
  425. return ports;
  426. }
  427. StringTokenizer sT = new StringTokenizer(portLines, "\n");
  428. int size = sT.countTokens();
  429. ports = new String[size];
  430. for (int i = 0; i < size; i++) {
  431. ports[i] = sT.nextToken();
  432. }
  433. return ports;
  434. }
  435. // --------------------
  436. }