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

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