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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. private final int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
  43. private boolean changedState = false;
  44. private Frame parentFrame;
  45. private Animation[] animations = new Animation[1];
  46. /**
  47. * Creates a worker with one animation, containing an empty frame.
  48. */
  49. public cubeWorker(Frame parent) {
  50. animations[0] = new Animation();
  51. animations[0].setName("Animation 1");
  52. parentFrame = parent;
  53. }
  54. public cubeWorker(Animation[] anims, Frame parent) {
  55. animations = anims;
  56. parentFrame = parent;
  57. }
  58. /**
  59. * Returns number of frames in this cubeWorker.
  60. *
  61. * @return number of frames.
  62. */
  63. public int completeNumOfFrames() {
  64. int c = 0;
  65. for (int i = 0; i < size(); i++) {
  66. c += getAnimation(i).size();
  67. }
  68. return c;
  69. }
  70. // --------------------
  71. // Misc. Methods
  72. // --------------------
  73. /**
  74. * Get the number of animations in this worker.
  75. *
  76. * @return number of animations
  77. */
  78. public int size() {
  79. return animations.length;
  80. }
  81. /**
  82. * Get the number of frames you can add until the Cubes memory is full.
  83. *
  84. * @return number of frames remaining
  85. */
  86. public int memoryRemaining() {
  87. return framesRemaining - completeNumOfFrames();
  88. }
  89. /**
  90. * Add an animation. It has an initial empty frame
  91. *
  92. * @return Index of new animation, or -1 if not enough space remaining.
  93. */
  94. public int addAnimation() {
  95. changedState = true;
  96. if (memoryRemaining() <= 0) {
  97. return -1;
  98. } else {
  99. extendArray();
  100. animations[animations.length - 1] = new Animation();
  101. return animations.length - 1;
  102. }
  103. }
  104. /**
  105. * Remove an animation.
  106. *
  107. * @param i the animation you want to delete
  108. */
  109. public void removeAnimation(int i) {
  110. changedState = true;
  111. shiftOver(i);
  112. shrinkArray();
  113. }
  114. /**
  115. * Move an animation up.
  116. * @param i the animation you want to move
  117. */
  118. public void moveAnimationUp(int i) {
  119. if (i > 0) {
  120. Animation tmp = animations[i];
  121. animations[i] = animations[i - 1];
  122. animations[i - 1] = tmp;
  123. }
  124. }
  125. /**
  126. * Move an animation down.
  127. * @param i the animation you want to move
  128. */
  129. public void moveAnimationDown(int i) {
  130. if (i < (animations.length - 1)) {
  131. Animation tmp = animations[i];
  132. animations[i] = animations[i + 1];
  133. animations[i + 1] = tmp;
  134. }
  135. }
  136. public Animation getAnimation(int i) {
  137. if (i < animations.length) {
  138. return animations[i];
  139. } else {
  140. return null;
  141. }
  142. }
  143. public void setAnimation(Animation a, int i) {
  144. changedState = true;
  145. if (i < animations.length) {
  146. animations[i] = a;
  147. }
  148. }
  149. /**
  150. * Loads an animation file into this worker.
  151. *
  152. * @param path Path of file to load
  153. * @return 0 on success, -1 on error.
  154. */
  155. public int loadState(String path) {
  156. changedState = false;
  157. try {
  158. animations = AnimationUtility.readFile(path);
  159. } catch (Exception e) {
  160. System.out.println("Did not load!");
  161. e.printStackTrace(System.out);
  162. return -1;
  163. }
  164. int size = 0;
  165. for (int i = 0; i < animations.length; i++) {
  166. size += animations[i].size();
  167. }
  168. if (size > framesRemaining) {
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. /**
  174. * Save the state of this object into a file.
  175. *
  176. * @param path Path to save file in
  177. * @return 0 on success, -1 on error
  178. */
  179. public int saveState(String path) {
  180. changedState = false;
  181. AnimationUtility.writeFile(path, animations);
  182. if (AnimationUtility.getLastError() != null) {
  183. System.out.println(AnimationUtility.getLastError());
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. /**
  189. * Check if something changed after loading/saving.
  190. *
  191. * @return TRUE if something changed, FALSE otherwise
  192. */
  193. public boolean changedStateSinceSave() {
  194. return changedState;
  195. }
  196. /**
  197. * Send all animations to the cube.
  198. *
  199. * @param port Name of serial port to use
  200. * @return 0 on success, -1 on error
  201. */
  202. public int cubeSendState(String port) {
  203. try {
  204. SerialHelper sh = new SerialHelper(port, parentFrame);
  205. int ret = sh.sendAnimationsToCube(this);
  206. sh.closeSerialPort();
  207. return ret;
  208. } catch (Exception e) {
  209. return -1;
  210. }
  211. }
  212. /**
  213. * Get the array of animations in this worker.
  214. * @return animation array
  215. */
  216. public Animation[] getAnimationArray() {
  217. return animations;
  218. }
  219. /**
  220. * Get all animations from the cube, place it in this object
  221. *
  222. * @param port Name of serial port to use
  223. * @return 0 on success, -1 on error
  224. */
  225. public int cubeGetState(String port) {
  226. try {
  227. SerialHelper sh = new SerialHelper(port, parentFrame);
  228. cubeWorker ret = sh.getAnimationsFromCube();
  229. sh.closeSerialPort();
  230. if (ret == null) {
  231. return -1;
  232. } else {
  233. changedState = true;
  234. animations = ret.getAnimationArray();
  235. return 0;
  236. }
  237. } catch (Exception e) {
  238. return -1;
  239. }
  240. }
  241. /**
  242. * Try to speak with the cube.
  243. *
  244. * @return TRUE if cube responds
  245. * @param port Name of serial port
  246. */
  247. public boolean cubeProbeConnected(String port) {
  248. try {
  249. SerialHelper sh = new SerialHelper(port, parentFrame);
  250. boolean response = sh.probeForCube();
  251. sh.closeSerialPort();
  252. return response;
  253. } catch (Exception e) {
  254. return false;
  255. }
  256. }
  257. private void extendArray() {
  258. Animation newArray[] = new Animation[animations.length + 1];
  259. for (int i = 0; i < animations.length; i++) {
  260. newArray[i] = animations[i];
  261. }
  262. animations = newArray;
  263. }
  264. private void shrinkArray() {
  265. Animation newArray[] = new Animation[animations.length - 1];
  266. for (int i = 0; i < newArray.length; i++) {
  267. newArray[i] = animations[i];
  268. }
  269. animations = newArray;
  270. }
  271. private void shiftOver(int toForget) {
  272. for (int i = (toForget + 1); i < animations.length; i++) {
  273. animations[i - 1] = animations[i];
  274. }
  275. }
  276. }