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

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