Simple single-color 8x8x8 LED Cube with AVRs
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SerialHelper.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * SerialHelper.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. * Implement commands of cube. You can only open one serial port.
  25. * If you want to communicate with another port, close this one first!
  26. * @author Thomas Buck
  27. * @author Max Nuding
  28. * @author Felix Bäder
  29. * @version 1.0
  30. */
  31. import java.util.Date;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. public class SerialHelper {
  35. private final short OK = 0x42;
  36. private final short ERROR = 0x23;
  37. private Frame frame;
  38. /**
  39. * Create new SerialHelper.
  40. * @param serialPort Name of serial port to use.
  41. * @param frame Frame to show error messages
  42. * @throws Exception Could not open serial port.
  43. */
  44. public SerialHelper(String serialPort, Frame frame) throws Exception {
  45. if (HelperUtility.openPort(serialPort) == false) {
  46. printErrorMessage("Could not open serial port \"" + serialPort + "\"");
  47. throw new Exception("Could not open serial port \"" + serialPort + "\"");
  48. }
  49. this.frame = frame;
  50. }
  51. /**
  52. * Poll to check if the cube is there...
  53. * @return TRUE if cube is connected.
  54. */
  55. public boolean probeForCube() {
  56. short[] data = new short[1];
  57. data[0] = OK;
  58. if (!writeData(data)) {
  59. printErrorMessage("Timeout Probing for Cube");
  60. return false;
  61. }
  62. data = readData(1);
  63. if ((data == null) || (data[0] != OK)) {
  64. printErrorMessage("No response from cube!");
  65. return false;
  66. }
  67. return true;
  68. }
  69. /**
  70. * Recieve all animations saved in cube.
  71. * @return A cubeWorker populated with the new data or null.
  72. */
  73. public cubeWorker getAnimationsFromCube() {
  74. Animation[] animations;
  75. int animationCount, frameCount;
  76. short[] data, tmp = new short[1];
  77. // Send download command
  78. tmp[0] = 'g';
  79. if (!writeData(tmp)) {
  80. printErrorMessage("Timout Command");
  81. return null;
  82. }
  83. data = readData(1);
  84. if ((data == null) || (data[0] != OK)) {
  85. printErrorMessage("Response Error");
  86. return null;
  87. }
  88. // Get animation count
  89. data = readData(1);
  90. if (data == null) {
  91. printErrorMessage("Response Error");
  92. return null;
  93. } else {
  94. animationCount = data[0];
  95. }
  96. tmp[0] = OK;
  97. if (!writeData(tmp)) {
  98. printErrorMessage("Timout Response");
  99. return null;
  100. }
  101. animations = new Animation[animationCount];
  102. // Get animations
  103. for (int a = 0; a < animationCount; a++) {
  104. Animation currentAnim = new Animation();
  105. // Get number of frames
  106. data = readData(1);
  107. if (data == null) {
  108. printErrorMessage("Response Error");
  109. return null;
  110. } else {
  111. frameCount = data[0];
  112. }
  113. tmp[0] = OK;
  114. if (!writeData(tmp)) {
  115. printErrorMessage("Timout Response");
  116. return null;
  117. }
  118. // Get frames
  119. for (int f = 0; f < frameCount; f++) {
  120. AFrame currentFrame = new AFrame();
  121. // Get frame duration
  122. data = readData(1);
  123. if (data == null) {
  124. printErrorMessage("Response Error");
  125. return null;
  126. } else {
  127. currentFrame.setTime(data[0]);
  128. }
  129. tmp[0] = OK;
  130. if (!writeData(tmp)) {
  131. printErrorMessage("Timout Response");
  132. return null;
  133. }
  134. // Get frame data
  135. data = readData(64);
  136. if (data == null) {
  137. printErrorMessage("Response Error");
  138. return null;
  139. } else {
  140. currentFrame.setData(data);
  141. }
  142. tmp[0] = OK;
  143. if (!writeData(tmp)) {
  144. printErrorMessage("Timout Response");
  145. return null;
  146. }
  147. // Add frame to animation
  148. currentAnim.setFrame(currentFrame, f);
  149. }
  150. // Add animation to animations list
  151. animations[a] = currentAnim;
  152. }
  153. return new cubeWorker(animations, frame);
  154. }
  155. /**
  156. * Send all animations in a cubeWorker to the Cube.
  157. * @param worker cubeWorker that containts data.
  158. * @return 0 on success. -1 on error.
  159. */
  160. public int sendAnimationsToCube(cubeWorker worker) {
  161. short[] data, tmp = new short[1];
  162. // Send upload command
  163. tmp[0] = 's';
  164. if (!writeData(tmp)) {
  165. printErrorMessage("Timout Command");
  166. return -1;
  167. }
  168. data = readData(1);
  169. if ((data == null) || (data[0] != OK)) {
  170. printErrorMessage("Response Error");
  171. return -1;
  172. }
  173. // Send animation count
  174. tmp[0] = (short)worker.size();
  175. if (!writeData(tmp)) {
  176. printErrorMessage("Timeout numOfAnimations");
  177. return -1;
  178. }
  179. data = readData(1);
  180. if ((data == null) || (data[0] != OK)) {
  181. printErrorMessage("Response Error");
  182. return -1;
  183. }
  184. // Send animations
  185. for (int a = 0; a < worker.size(); a++) {
  186. // Send frame count
  187. tmp[0] = (short)worker.getAnimation(a).size();
  188. if (!writeData(tmp)) {
  189. printErrorMessage("Timeout numOfFrames");
  190. return -1;
  191. }
  192. data = readData(1);
  193. if ((data == null) || (data[0] != OK)) {
  194. printErrorMessage("Response Error");
  195. return -1;
  196. }
  197. // Send frames
  198. for (int f = 0; f < worker.getAnimation(a).size(); f++) {
  199. // Frame duration
  200. tmp[0] = worker.getAnimation(a).getFrame(f).getTime();
  201. if (!writeData(tmp)) {
  202. printErrorMessage("Timeout Frame duration");
  203. return -1;
  204. }
  205. data = readData(1);
  206. if ((data == null) || (data[0] != OK)) {
  207. printErrorMessage("Response Error");
  208. return -1;
  209. }
  210. // Frame data
  211. if (!writeData(worker.getAnimation(a).getFrame(f).getData())) {
  212. printErrorMessage("Timeout Frame");
  213. return -1;
  214. }
  215. data = readData(1);
  216. if ((data == null) || (data[0] != OK)) {
  217. printErrorMessage("Response Error");
  218. return -1;
  219. }
  220. }
  221. }
  222. // Send finish
  223. tmp = new short[4];
  224. tmp[0] = OK;
  225. tmp[1] = OK;
  226. tmp[2] = OK;
  227. tmp[3] = OK;
  228. if (!writeData(tmp)) {
  229. printErrorMessage("Timeout Finish");
  230. return -1;
  231. }
  232. data = readData(1);
  233. if ((data == null) || (data[0] != OK)) {
  234. printErrorMessage("Response Error");
  235. return -1;
  236. }
  237. return 0;
  238. }
  239. /**
  240. * Close the serial port again.
  241. */
  242. public void closeSerialPort() {
  243. HelperUtility.closePort();
  244. }
  245. private void printErrorMessage(String s) {
  246. System.out.println("SerialHelper: " + s);
  247. frame.errorMessage("Serial Error", s);
  248. }
  249. private boolean writeData(short[] data) {
  250. // write data. return true if success
  251. long startdate = (new Date()).getTime();
  252. SerialWriteThread t = new SerialWriteThread(data);
  253. t.start();
  254. while (!t.dataWasSent()) {
  255. if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
  256. // More than (length * 1000) milliseconds went by
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. private short[] readData(int length) {
  263. // return data read or null if timeout
  264. long startdate = (new Date()).getTime();
  265. SerialReadThread t = new SerialReadThread(length);
  266. t.start();
  267. while (!t.dataIsReady()) {
  268. if ((new Date()).getTime() >= (startdate + (length * 1000))) {
  269. // More than (length * 1000) milliseconds went by
  270. return null;
  271. }
  272. }
  273. return t.getSerialData();
  274. }
  275. }