Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SerialHelper.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. List<Animation> animations = new ArrayList<Animation>();
  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. // Get animations
  102. for (int a = 0; a < animationCount; a++) {
  103. Animation currentAnim = new Animation();
  104. // Get number of frames
  105. data = readData(1);
  106. if (data == null) {
  107. printErrorMessage("Response Error");
  108. return null;
  109. } else {
  110. frameCount = data[0];
  111. }
  112. tmp[0] = OK;
  113. if (!writeData(tmp)) {
  114. printErrorMessage("Timout Response");
  115. return null;
  116. }
  117. // Get frames
  118. for (int f = 0; f < frameCount; f++) {
  119. AFrame currentFrame = new AFrame();
  120. // Get frame duration
  121. data = readData(1);
  122. if (data == null) {
  123. printErrorMessage("Response Error");
  124. return null;
  125. } else {
  126. currentFrame.setTime(data[0]);
  127. }
  128. tmp[0] = OK;
  129. if (!writeData(tmp)) {
  130. printErrorMessage("Timout Response");
  131. return null;
  132. }
  133. // Get frame data
  134. data = readData(64);
  135. if (data == null) {
  136. printErrorMessage("Response Error");
  137. return null;
  138. } else {
  139. currentFrame.setData(data);
  140. }
  141. tmp[0] = OK;
  142. if (!writeData(tmp)) {
  143. printErrorMessage("Timout Response");
  144. return null;
  145. }
  146. // Add frame to animation
  147. currentAnim.add(f, currentFrame);
  148. }
  149. // Add animation to animations list
  150. animations.add(a, currentAnim);
  151. }
  152. return new cubeWorker(animations, frame);
  153. }
  154. /**
  155. * Send all animations in a cubeWorker to the Cube.
  156. * @param worker cubeWorker that containts data.
  157. * @return 0 on success. -1 on error.
  158. */
  159. public int sendAnimationsToCube(cubeWorker worker) {
  160. short[] data, tmp = new short[1];
  161. // Send upload command
  162. tmp[0] = 's';
  163. if (!writeData(tmp)) {
  164. printErrorMessage("Timout Command");
  165. return -1;
  166. }
  167. data = readData(1);
  168. if ((data == null) || (data[0] != OK)) {
  169. printErrorMessage("Response Error");
  170. return -1;
  171. }
  172. // Send animation count
  173. tmp[0] = (short)worker.numOfAnimations();
  174. if (!writeData(tmp)) {
  175. printErrorMessage("Timeout numOfAnimations");
  176. return -1;
  177. }
  178. data = readData(1);
  179. if ((data == null) || (data[0] != OK)) {
  180. printErrorMessage("Response Error");
  181. return -1;
  182. }
  183. // Send animations
  184. for (int a = 0; a < worker.numOfAnimations(); a++) {
  185. // Send frame count
  186. tmp[0] = (short)worker.numOfFrames(a);
  187. if (!writeData(tmp)) {
  188. printErrorMessage("Timeout numOfFrames");
  189. return -1;
  190. }
  191. data = readData(1);
  192. if ((data == null) || (data[0] != OK)) {
  193. printErrorMessage("Response Error");
  194. return -1;
  195. }
  196. // Send frames
  197. for (int f = 0; f < worker.numOfFrames(a); f++) {
  198. // Frame duration
  199. tmp[0] = worker.getFrameTime(a, f);
  200. if (!writeData(tmp)) {
  201. printErrorMessage("Timeout Frame duration");
  202. return -1;
  203. }
  204. data = readData(1);
  205. if ((data == null) || (data[0] != OK)) {
  206. printErrorMessage("Response Error");
  207. return -1;
  208. }
  209. // Frame data
  210. if (!writeData(worker.getFrame(a, f))) {
  211. printErrorMessage("Timeout Frame");
  212. return -1;
  213. }
  214. data = readData(1);
  215. if ((data == null) || (data[0] != OK)) {
  216. printErrorMessage("Response Error");
  217. return -1;
  218. }
  219. }
  220. }
  221. // Send finish
  222. tmp = new short[4];
  223. tmp[0] = OK;
  224. tmp[1] = OK;
  225. tmp[2] = OK;
  226. tmp[3] = OK;
  227. if (!writeData(tmp)) {
  228. printErrorMessage("Timeout Finish");
  229. return -1;
  230. }
  231. data = readData(1);
  232. if ((data == null) || (data[0] != OK)) {
  233. printErrorMessage("Response Error");
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. /**
  239. * Close the serial port again.
  240. */
  241. public void closeSerialPort() {
  242. HelperUtility.closePort();
  243. }
  244. private void printErrorMessage(String s) {
  245. System.out.println("SerialHelper: " + s);
  246. frame.errorMessage("Serial Error", s);
  247. }
  248. private boolean writeData(short[] data) {
  249. // write data. return true if success
  250. long startdate = (new Date()).getTime();
  251. SerialWriteThread t = new SerialWriteThread(data);
  252. t.start();
  253. while (!t.dataWasSent()) {
  254. if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
  255. // More than (length * 1000) milliseconds went by
  256. return false;
  257. }
  258. }
  259. return true;
  260. }
  261. private short[] readData(int length) {
  262. // return data read or null if timeout
  263. long startdate = (new Date()).getTime();
  264. SerialReadThread t = new SerialReadThread(length);
  265. t.start();
  266. while (!t.dataIsReady()) {
  267. if ((new Date()).getTime() >= (startdate + (length * 1000))) {
  268. // More than (length * 1000) milliseconds went by
  269. return null;
  270. }
  271. }
  272. return t.getSerialData();
  273. }
  274. }