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

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