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

cubeWorker.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. public class cubeWorker {
  29. // --------------------
  30. // Definitions
  31. // --------------------
  32. final int UP = 0;
  33. final int DOWN = 1;
  34. // --------------------
  35. // Fields
  36. // --------------------
  37. private ArrayList<Animation> animations = new ArrayList<Animation>();
  38. private int selectedAnimation = 0;
  39. private int selectedFrame = 0;
  40. private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
  41. private boolean changedState = false;
  42. // --------------------
  43. cubeWorker() {
  44. animations.add(new Animation());
  45. }
  46. // --------------------
  47. // Misc. Methods
  48. // --------------------
  49. // Returns how many animations are defined
  50. public int numOfAnimations() {
  51. return animations.size();
  52. }
  53. // Returns how many frames are in the current animation
  54. public int numOfFrames() {
  55. return animations.get(selectedAnimation).size();
  56. }
  57. // Tells how many Frames you can add until you reached 1 Mbit...
  58. public int framesRemaining() {
  59. return framesRemaining;
  60. }
  61. // --------------------
  62. // Animation Specific
  63. // --------------------
  64. // Selects an animation on wich the animation specific functions operate
  65. // Returns -1 if it does not exist, else its index
  66. public int selectAnimation(int index) {
  67. if (animations.size() <= index) {
  68. return -1;
  69. } else {
  70. selectedAnimation = index;
  71. return index;
  72. }
  73. }
  74. // Adds a new Animation
  75. // Returns id if ok, -1 if error or not enough space for
  76. // another animation
  77. public int addAnimation() {
  78. changedState = true;
  79. if (framesRemaining <= 0) {
  80. return -1;
  81. } else {
  82. int s = animations.size();
  83. animations.add(s + 1, new Animation());
  84. return s;
  85. }
  86. }
  87. // Removes an animation
  88. public void removeAnimation() {
  89. changedState = true;
  90. animations.remove(selectedAnimation);
  91. selectedAnimation = 0;
  92. }
  93. public String getAnimationName() {
  94. return animations.get(selectedAnimation).getName();
  95. }
  96. public void setAnimationName(String s) {
  97. changedState = true;
  98. animations.get(selectedAnimation).setName(s);
  99. }
  100. public void moveAnimation(int dir) {
  101. changedState = true;
  102. if (dir == UP){
  103. //animation moved up
  104. if (selectedAnimation > 0) {
  105. Animation tmp = animations.get(selectedAnimation);
  106. animations.set(selectedAnimation, animations.get(selectedAnimation - 1));
  107. animations.set(selectedAnimation - 1, tmp);
  108. }
  109. } else if (dir == DOWN){
  110. //animation moved down
  111. if (selectedAnimation < (animations.size() - 1)) {
  112. Animation tmp = animations.get(selectedAnimation);
  113. animations.set(selectedAnimation, animations.get(selectedAnimation + 1));
  114. animations.set(selectedAnimation + 1, tmp);
  115. }
  116. }
  117. }
  118. // --------------------
  119. // Frame Specific
  120. // --------------------
  121. // Selects an animation on wich the frame specific functions operate
  122. // Returns -1 if it does not exist, else its index
  123. public int selectFrame(int index) {
  124. if (animations.get(selectedAnimation).size() <= index) {
  125. return -1;
  126. } else {
  127. selectedFrame = index;
  128. return index;
  129. }
  130. }
  131. public String getFrameName() {
  132. return animations.get(selectedAnimation).get(selectedFrame).getName();
  133. }
  134. public void setFrameName(String s) {
  135. changedState = true;
  136. animations.get(selectedAnimation).get(selectedFrame).setName(s);
  137. }
  138. // Adds a Frame to the current animation.
  139. // Returns id if okay, -1 if error
  140. public int addFrame() {
  141. changedState = true;
  142. if (framesRemaining <= 0) {
  143. return -1;
  144. }
  145. framesRemaining--;
  146. int s = animations.get(selectedAnimation).size();
  147. animations.get(selectedAnimation).add(s);
  148. return s;
  149. }
  150. // Remove the frame
  151. public void removeFrame() {
  152. changedState = true;
  153. animations.get(selectedAnimation).remove(selectedFrame);
  154. selectedFrame = 0;
  155. }
  156. // Returns array with 64 bytes with led values
  157. public byte[] getFrame() {
  158. return animations.get(selectedAnimation).get(selectedFrame).getData();
  159. }
  160. public void setFrame(byte[] data) {
  161. changedState = true;
  162. animations.get(selectedAnimation).get(selectedFrame).setData(data);
  163. }
  164. // Frame duration in 1/24th of a second
  165. public byte getFrameTime() {
  166. return animations.get(selectedAnimation).get(selectedFrame).getTime();
  167. }
  168. public void setFrameTime(byte time) {
  169. changedState = true;
  170. animations.get(selectedAnimation).get(selectedFrame).setTime(time);
  171. }
  172. public void moveFrame(int dir){
  173. changedState = true;
  174. if (dir == UP){
  175. // frame moved up
  176. if (selectedFrame > 0) {
  177. Frame tmp = animations.get(selectedAnimation).get(selectedFrame);
  178. animations.get(selectedAnimation).set(animations.get(selectedAnimation).get(selectedFrame - 1), selectedFrame);
  179. animations.get(selectedAnimation).set(tmp, selectedFrame - 1);
  180. selectedFrame--;
  181. }
  182. } else if (dir == DOWN){
  183. // frame moved down
  184. if (selectedFrame < (animations.get(selectedAnimation).size() - 1)) {
  185. Frame tmp = animations.get(selectedAnimation).get(selectedFrame);
  186. animations.get(selectedAnimation).set(animations.get(selectedAnimation).get(selectedFrame + 1), selectedFrame);
  187. animations.get(selectedAnimation).set(tmp, selectedFrame + 1);
  188. selectedFrame++;
  189. }
  190. }
  191. }
  192. // --------------------
  193. // File Specific
  194. // --------------------
  195. // Loads an animation file into this object
  196. public int loadState(String path) {
  197. changedState = false;
  198. return 0;
  199. }
  200. // Saves the state of this object in an animation file
  201. public int saveState(String path) {
  202. changedState = false;
  203. System.out.println("Saving to " + path);
  204. return 0;
  205. }
  206. // Returns true if last saved state != current state
  207. public boolean changedStateSinceSave() {
  208. return changedState;
  209. }
  210. // --------------------
  211. // Serial Port Specific
  212. // --------------------
  213. // sends state of object to led cube
  214. public int uploadState(String port) {
  215. return 0;
  216. }
  217. // loads all state from the cube into this object
  218. public int downloadState(String port) {
  219. return 0;
  220. }
  221. public String[] getSerialPorts() {
  222. String[] sPorts = { "Select serial port..." }; // Has to be the first entry
  223. return sPorts;
  224. }
  225. // --------------------
  226. }
  227. class Frame {
  228. private byte[] data = new byte[64];
  229. private byte duration = 1;
  230. private String name = "Frame";
  231. String getName() {
  232. return name;
  233. }
  234. void setName(String s) {
  235. name = s;
  236. }
  237. void setData(byte[] d) {
  238. data = d;
  239. }
  240. byte[] getData() {
  241. return data;
  242. }
  243. void setTime(byte t) {
  244. duration = t;
  245. }
  246. byte getTime() {
  247. return duration;
  248. }
  249. }
  250. class Animation {
  251. private ArrayList<Frame> frames = new ArrayList<Frame>();
  252. private int lastFrameIndex = 0;
  253. private String name = "Animation";
  254. String getName() {
  255. return name;
  256. }
  257. void setName(String s) {
  258. name = s;
  259. }
  260. Frame get(int i) {
  261. try {
  262. return frames.get(i);
  263. } catch (IndexOutOfBoundsException e) {
  264. System.out.println(e.toString());
  265. return null;
  266. }
  267. }
  268. void set(Frame f, int i) {
  269. if (lastFrameIndex <= i) {
  270. try {
  271. frames.set(i, f);
  272. } catch (IndexOutOfBoundsException e) {
  273. System.out.println(e.toString());
  274. }
  275. }
  276. }
  277. void remove(int i) {
  278. try {
  279. frames.remove(i);
  280. } catch (IndexOutOfBoundsException e) {
  281. System.out.println(e.toString());
  282. }
  283. }
  284. void add(int i) {
  285. try {
  286. frames.add(i, new Frame());
  287. lastFrameIndex++;
  288. } catch (IndexOutOfBoundsException e) {
  289. System.out.println(e.toString());
  290. }
  291. }
  292. int size() {
  293. return frames.size();
  294. }
  295. }