Simple single-color 8x8x8 LED Cube with AVRs
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cubeWorker.java 7.1KB

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