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

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.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. Collections.swap(animations, selectedAnimation, selectedAnimation - 1);
  146. }
  147. } else if (dir == DOWN){
  148. //animation moved down
  149. if (selectedAnimation < (animations.size() - 1)) {
  150. Collections.swap(animations, selectedAnimation, selectedAnimation + 1);
  151. }
  152. }
  153. }
  154. // --------------------
  155. // Frame Specific
  156. // --------------------
  157. /**
  158. * Get the name of a frame.
  159. * @param anim Animation the frame is in
  160. * @param frame Index of the frame
  161. */
  162. public String getFrameName(int anim, int frame) {
  163. return animations.get(anim).get(frame).getName();
  164. }
  165. /**
  166. * Set the name of a frame.
  167. * @param s New name
  168. * @param anim Animation Index
  169. * @param frame Frame Index
  170. */
  171. public void setFrameName(String s, int anim, int frame) {
  172. changedState = true;
  173. animations.get(anim).get(frame).setName(s);
  174. }
  175. /**
  176. * Add a Frame to an animation.
  177. * @return Index of new Frame or -1 if not enough space
  178. * @param anim Animation Index
  179. */
  180. public int addFrame(int anim) {
  181. changedState = true;
  182. if (framesRemaining <= 0) {
  183. return -1;
  184. }
  185. framesRemaining--;
  186. int s = animations.get(anim).size();
  187. animations.get(anim).add(s);
  188. animations.get(anim).get(s).setName("Frame " + animations.get(anim).size());
  189. return s;
  190. }
  191. /**
  192. * Remove a frame.
  193. * @param anim Animation Index
  194. * @param frame Frame you want to remove
  195. */
  196. public void removeFrame(int anim, int frame) {
  197. changedState = true;
  198. animations.get(anim).remove(frame);
  199. }
  200. /**
  201. * Get the data of a frame.
  202. * @param anim Animation Index
  203. * @param frame Frame Index
  204. * @return 64 byte array with data (8 bits per byte => 512 bits)
  205. */
  206. public short[] getFrame(int anim, int frame) {
  207. return animations.get(anim).get(frame).getData();
  208. }
  209. /**
  210. * Set the data of a frame
  211. * @param data 64 byte array with data
  212. * @param anim Animation Index
  213. * @param frame Frame Index
  214. * @see cubeWorker#getFrame(int, int) getFrame()
  215. */
  216. public void setFrame(short[] data, int anim, int frame) {
  217. changedState = true;
  218. animations.get(anim).get(frame).setData(data);
  219. }
  220. /**
  221. * Get frame duration.
  222. * @param anim Animation Index
  223. * @param frame Frame Index
  224. * @return Duration. 0 means 1/24th of a second.
  225. */
  226. public short getFrameTime(int anim, int frame) {
  227. return animations.get(anim).get(frame).getTime();
  228. }
  229. /**
  230. * Set the frames duration.
  231. * @param time New duration
  232. * @param anim Animation Index
  233. * @param frame Frame Index
  234. * @see cubeWorker#getFrameTime(int, int) getFrameTime()
  235. */
  236. public void setFrameTime(short time, int anim, int frame) {
  237. changedState = true;
  238. animations.get(anim).get(frame).setTime(time);
  239. }
  240. /**
  241. * Move a frame.
  242. * @param dir Direction to move. Use UP and DOWN from cubeWorker
  243. * @param anim Animation Index
  244. * @param frame Frame Index
  245. * @see cubeWorker#moveAnimation(int, int) moveAnimation()
  246. */
  247. public void moveFrame(int dir, int anim, int frame){
  248. changedState = true;
  249. if (dir == UP){
  250. // frame moved up
  251. if (frame > 0) {
  252. Collections.swap(animations.get(anim).frames, frame, frame - 1);
  253. }
  254. } else if (dir == DOWN){
  255. // frame moved down
  256. if (frame < (animations.get(anim).size() - 1)) {
  257. Collections.swap(animations.get(anim).frames, frame, frame + 1);
  258. }
  259. }
  260. }
  261. // --------------------
  262. // File Specific
  263. // --------------------
  264. /**
  265. * Loads an animation file into this worker.
  266. * @param path Path of file to load
  267. * @return 0 on success, -1 on error.
  268. */
  269. public int loadState(String path) {
  270. changedState = false;
  271. try {
  272. animations = AnimationUtility.readFile(path);
  273. } catch (Exception e) {
  274. System.out.println("Did not load!");
  275. e.printStackTrace(System.out);
  276. return -1;
  277. }
  278. int size = 0;
  279. for (int i = 0; i < animations.size(); i++) {
  280. size += animations.get(i).size();
  281. }
  282. framesRemaining = 2016 - size;
  283. if (size > 2016) {
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * Save the state of this object into a file.
  290. * @param path Path to save file in
  291. * @return 0 on success, -1 on error
  292. */
  293. public int saveState(String path) {
  294. changedState = false;
  295. AnimationUtility.writeFile(path, animations);
  296. if (AnimationUtility.getLastError() != null) {
  297. System.out.println(AnimationUtility.getLastError());
  298. return -1;
  299. }
  300. return 0;
  301. }
  302. /**
  303. * Check if something changed after loading/saving.
  304. * @return TRUE if something changed, FALSE otherwise
  305. */
  306. public boolean changedStateSinceSave() {
  307. return changedState;
  308. }
  309. // --------------------
  310. // Serial Port Specific
  311. // --------------------
  312. /**
  313. * Send all animations to the cube.
  314. * @param port Name of serial port to use
  315. * @return 0 on success, -1 on error
  316. */
  317. public int uploadState(String port) {
  318. return -1;
  319. }
  320. /**
  321. * Get all animations from the cube, place it in this object
  322. * @param port Name of serial port to use
  323. * @return 0 on success, -1 on error
  324. */
  325. public int downloadState(String port) {
  326. return -1;
  327. }
  328. /**
  329. * Try to speak with the cube.
  330. * @return TRUE if cube responds
  331. * @param port Name of serial port
  332. */
  333. public boolean probeCubeConnected(String port) {
  334. return false;
  335. }
  336. /**
  337. * Get the names of all available serial ports.
  338. * @return Array of port names. First entry is always "Select serial port..."
  339. */
  340. public String[] getSerialPorts() {
  341. String[] ports = {"Select serial port..."};
  342. String helperName;
  343. if ((System.getProperty("os.name").toLowerCase()).indexOf("win") >= 0) {
  344. helperName = "serialHelper.exe";
  345. } else {
  346. helperName = "serialHelper";
  347. }
  348. String[] arg = {"p"};
  349. String portLines = HelperUtility.runHelper(arg);
  350. // System.out.println("Output: " + portLines);
  351. if (portLines == null) {
  352. return ports;
  353. }
  354. StringTokenizer sT = new StringTokenizer(portLines, "\n");
  355. int size = sT.countTokens() + 1;
  356. ports = new String[size];
  357. ports[0] = "Select serial port...";
  358. for (int i = 1; i < size; i++) {
  359. ports[i] = sT.nextToken();
  360. }
  361. return ports;
  362. }
  363. // --------------------
  364. }