Simple single-color 8x8x8 LED Cube with AVRs
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cubeWorker.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. public class cubeWorker {
  31. // --------------------
  32. // Definitions
  33. // --------------------
  34. final int UP = 0;
  35. final int DOWN = 1;
  36. // --------------------
  37. // Fields
  38. // --------------------
  39. private ArrayList<Animation> animations = new ArrayList<Animation>();
  40. private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
  41. private boolean changedState = false;
  42. // --------------------
  43. cubeWorker() {
  44. animations.add(new Animation());
  45. animations.get(0).setName("Animation 1");
  46. animations.get(0).add(0);
  47. animations.get(0).get(0).setName("Frame 1");
  48. framesRemaining--;
  49. }
  50. cubeWorker(ArrayList<Animation> anims) {
  51. animations = anims;
  52. }
  53. // --------------------
  54. // Misc. Methods
  55. // --------------------
  56. // Returns how many animations are defined
  57. public int numOfAnimations() {
  58. return animations.size();
  59. }
  60. // Returns how many frames are in the current animation
  61. public int numOfFrames(int selectedAnimation) {
  62. return animations.get(selectedAnimation).size();
  63. }
  64. // Tells how many Frames you can add until you reached 1 Mbit...
  65. public int framesRemaining() {
  66. return framesRemaining;
  67. }
  68. // --------------------
  69. // Animation Specific
  70. // --------------------
  71. // Adds a new Animation
  72. // Returns id if ok, -1 if error or not enough space for
  73. // another animation
  74. public int addAnimation() {
  75. changedState = true;
  76. if (framesRemaining <= 0) {
  77. return -1;
  78. } else {
  79. int s = animations.size();
  80. animations.add(s, new Animation());
  81. animations.get(s).setName("Animation " + animations.size());
  82. return s;
  83. }
  84. }
  85. // Removes an animation
  86. public void removeAnimation(int selectedAnimation) {
  87. changedState = true;
  88. animations.remove(selectedAnimation);
  89. selectedAnimation = 0;
  90. }
  91. public String getAnimationName(int selectedAnimation) {
  92. return animations.get(selectedAnimation).getName();
  93. }
  94. public void setAnimationName(String s, int selectedAnimation) {
  95. changedState = true;
  96. animations.get(selectedAnimation).setName(s);
  97. }
  98. public void moveAnimation(int dir, int selectedAnimation) {
  99. changedState = true;
  100. if (dir == UP){
  101. //animation moved up
  102. if (selectedAnimation > 0) {
  103. Collections.swap(animations, selectedAnimation, selectedAnimation - 1);
  104. }
  105. } else if (dir == DOWN){
  106. //animation moved down
  107. if (selectedAnimation < (animations.size() - 1)) {
  108. Collections.swap(animations, selectedAnimation, selectedAnimation + 1);
  109. }
  110. }
  111. }
  112. // --------------------
  113. // Frame Specific
  114. // --------------------
  115. public String getFrameName(int anim, int frame) {
  116. return animations.get(anim).get(frame).getName();
  117. }
  118. public void setFrameName(String s, int anim, int frame) {
  119. changedState = true;
  120. animations.get(anim).get(frame).setName(s);
  121. }
  122. // Adds a Frame to the current animation.
  123. // Returns id if okay, -1 if error
  124. public int addFrame(int anim) {
  125. changedState = true;
  126. if (framesRemaining <= 0) {
  127. return -1;
  128. }
  129. framesRemaining--;
  130. int s = animations.get(anim).size();
  131. animations.get(anim).add(s);
  132. animations.get(anim).get(s).setName("Frame " + animations.get(anim).size());
  133. return s;
  134. }
  135. // Remove the frame
  136. public void removeFrame(int anim, int frame) {
  137. changedState = true;
  138. animations.get(anim).remove(frame);
  139. }
  140. // Returns array with 64 bytes with led values
  141. public short[] getFrame(int anim, int frame) {
  142. return animations.get(anim).get(frame).getData();
  143. }
  144. public void setFrame(short[] data, int anim, int frame) {
  145. changedState = true;
  146. animations.get(anim).get(frame).setData(data);
  147. }
  148. // Frame duration in 1/24th of a second
  149. public short getFrameTime(int anim, int frame) {
  150. return animations.get(anim).get(frame).getTime();
  151. }
  152. public void setFrameTime(short time, int anim, int frame) {
  153. changedState = true;
  154. animations.get(anim).get(frame).setTime(time);
  155. }
  156. public void moveFrame(int dir, int anim, int frame){
  157. changedState = true;
  158. if (dir == UP){
  159. // frame moved up
  160. if (frame > 0) {
  161. Collections.swap(animations.get(anim).frames, frame, frame - 1);
  162. }
  163. } else if (dir == DOWN){
  164. // frame moved down
  165. if (frame < (animations.get(anim).size() - 1)) {
  166. Collections.swap(animations.get(anim).frames, frame, frame + 1);
  167. }
  168. }
  169. }
  170. // --------------------
  171. // File Specific
  172. // --------------------
  173. // Loads an animation file into this object
  174. public int loadState(String path) {
  175. changedState = false;
  176. try {
  177. animations = AnimationUtility.readFile(path);
  178. } catch (Exception e) {
  179. System.out.println("Did not load!");
  180. e.printStackTrace(System.out);
  181. return -1;
  182. }
  183. int size = 0;
  184. for (int i = 0; i < animations.size(); i++) {
  185. size += animations.get(i).size();
  186. }
  187. framesRemaining = 2016 - size;
  188. if (size > 2016) {
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. // Saves the state of this object in an animation file
  194. public int saveState(String path) {
  195. changedState = false;
  196. AnimationUtility.writeFile(path, animations);
  197. if (AnimationUtility.getLastError() != null) {
  198. System.out.println(AnimationUtility.getLastError());
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. // Returns true if last saved state != current state
  204. public boolean changedStateSinceSave() {
  205. return changedState;
  206. }
  207. // --------------------
  208. // Serial Port Specific
  209. // --------------------
  210. // sends state of object to led cube
  211. public int uploadState(String port) {
  212. return 0;
  213. }
  214. // loads all state from the cube into this object
  215. public int downloadState(String port) {
  216. return 0;
  217. }
  218. public String[] getSerialPorts() {
  219. String[] ports = {"Select serial port..."};
  220. String helperName;
  221. if ((System.getProperty("os.name").toLowerCase()).indexOf("win") >= 0) {
  222. helperName = "serialHelper.exe";
  223. } else {
  224. helperName = "serialHelper";
  225. }
  226. String[] arg = {"p"};
  227. String portLines = HelperUtility.runHelper(arg);
  228. // System.out.println("Output: " + portLines);
  229. if (portLines == null) {
  230. return ports;
  231. }
  232. StringTokenizer sT = new StringTokenizer(portLines, "\n");
  233. int size = sT.countTokens() + 1;
  234. ports = new String[size];
  235. ports[0] = "Select serial port...";
  236. for (int i = 1; i < size; i++) {
  237. ports[i] = sT.nextToken();
  238. }
  239. return ports;
  240. }
  241. // --------------------
  242. }