Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cubeWorker.java 11KB

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