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

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