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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. throw new Exception("Could not open serial port \"" + serialPort + "\"");
  47. }
  48. this.frame = frame;
  49. }
  50. /**
  51. * Recieve all animations saved in cube.
  52. * @return A cubeWorker populated with the new data or null.
  53. */
  54. public cubeWorker getAnimationsFromCube() {
  55. List<Animation> animations = new ArrayList<Animation>();
  56. int animationCount, frameCount;
  57. short[] data, tmp = new short[1];
  58. // Send download command
  59. tmp[0] = 'g';
  60. if (!writeData(tmp)) {
  61. printErrorMessage("Timout Command");
  62. return null;
  63. }
  64. data = readData(1);
  65. if ((data == null) || (data[0] != OK)) {
  66. printErrorMessage("Response Error");
  67. return null;
  68. }
  69. // Get animation count
  70. data = readData(1);
  71. if (data == null) {
  72. printErrorMessage("Response Error");
  73. return null;
  74. } else {
  75. animationCount = data[0];
  76. }
  77. tmp[0] = OK;
  78. if (!writeData(tmp)) {
  79. printErrorMessage("Timout Response");
  80. return null;
  81. }
  82. // Get animations
  83. for (int a = 0; a < animationCount; a++) {
  84. Animation currentAnim = new Animation();
  85. // Get number of frames
  86. data = readData(1);
  87. if (data == null) {
  88. printErrorMessage("Response Error");
  89. return null;
  90. } else {
  91. frameCount = data[0];
  92. }
  93. tmp[0] = OK;
  94. if (!writeData(tmp)) {
  95. printErrorMessage("Timout Response");
  96. return null;
  97. }
  98. // Get frames
  99. for (int f = 0; f < frameCount; f++) {
  100. AFrame currentFrame = new AFrame();
  101. // Get frame duration
  102. data = readData(1);
  103. if (data == null) {
  104. printErrorMessage("Response Error");
  105. return null;
  106. } else {
  107. currentFrame.setTime(data[0]);
  108. }
  109. tmp[0] = OK;
  110. if (!writeData(tmp)) {
  111. printErrorMessage("Timout Response");
  112. return null;
  113. }
  114. // Get frame data
  115. data = readData(64);
  116. if (data == null) {
  117. printErrorMessage("Response Error");
  118. return null;
  119. } else {
  120. currentFrame.setData(data);
  121. }
  122. tmp[0] = OK;
  123. if (!writeData(tmp)) {
  124. printErrorMessage("Timout Response");
  125. return null;
  126. }
  127. // Add frame to animation
  128. currentAnim.add(f, currentFrame);
  129. }
  130. // Add animation to animations list
  131. animations.add(a, currentAnim);
  132. }
  133. return new cubeWorker(animations, frame);
  134. }
  135. /**
  136. * Send all animations in a cubeWorker to the Cube.
  137. * @param worker cubeWorker that containts data.
  138. * @return 0 on success. -1 on error.
  139. */
  140. public int sendAnimationsToCube(cubeWorker worker) {
  141. short[] data, tmp = new short[1];
  142. // Send upload command
  143. tmp[0] = 's';
  144. if (!writeData(tmp)) {
  145. printErrorMessage("Timout Command");
  146. return -1;
  147. }
  148. data = readData(1);
  149. if ((data == null) || (data[0] != OK)) {
  150. printErrorMessage("Response Error");
  151. return -1;
  152. }
  153. // Send animation count
  154. tmp[0] = (short)worker.numOfAnimations();
  155. if (!writeData(tmp)) {
  156. printErrorMessage("Timeout numOfAnimations");
  157. return -1;
  158. }
  159. data = readData(1);
  160. if ((data == null) || (data[0] != OK)) {
  161. printErrorMessage("Response Error");
  162. return -1;
  163. }
  164. // Send animations
  165. for (int a = 0; a < worker.numOfAnimations(); a++) {
  166. // Send frame count
  167. tmp[0] = (short)worker.numOfFrames(a);
  168. if (!writeData(tmp)) {
  169. printErrorMessage("Timeout numOfFrames");
  170. return -1;
  171. }
  172. data = readData(1);
  173. if ((data == null) || (data[0] != OK)) {
  174. printErrorMessage("Response Error");
  175. return -1;
  176. }
  177. // Send frames
  178. for (int f = 0; f < worker.numOfFrames(a); f++) {
  179. // Frame duration
  180. tmp[0] = worker.getFrameTime(a, f);
  181. if (!writeData(tmp)) {
  182. printErrorMessage("Timeout Frame duration");
  183. return -1;
  184. }
  185. data = readData(1);
  186. if ((data == null) || (data[0] != OK)) {
  187. printErrorMessage("Response Error");
  188. return -1;
  189. }
  190. // Frame data
  191. if (!writeData(worker.getFrame(a, f))) {
  192. printErrorMessage("Timeout Frame");
  193. return -1;
  194. }
  195. data = readData(1);
  196. if ((data == null) || (data[0] != OK)) {
  197. printErrorMessage("Response Error");
  198. return -1;
  199. }
  200. }
  201. }
  202. // Send finish
  203. tmp = new short[4];
  204. tmp[0] = OK;
  205. tmp[1] = OK;
  206. tmp[2] = OK;
  207. tmp[3] = OK;
  208. if (!writeData(tmp)) {
  209. printErrorMessage("Timeout Finish");
  210. return -1;
  211. }
  212. data = readData(1);
  213. if ((data == null) || (data[0] != OK)) {
  214. printErrorMessage("Response Error");
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * Close the serial port again.
  221. */
  222. public void closeSerialPort() {
  223. HelperUtility.closePort();
  224. }
  225. private void printErrorMessage(String s) {
  226. System.out.println("SerialHelper: " + s);
  227. frame.errorMessage("Serial Error", s);
  228. }
  229. private boolean writeData(short[] data) {
  230. // write data. return true if success
  231. long startdate = (new Date()).getTime();
  232. SerialWriteThread t = new SerialWriteThread(data);
  233. t.start();
  234. while (!t.dataWasSent()) {
  235. if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
  236. // More than (length * 1000) milliseconds went by
  237. return false;
  238. }
  239. }
  240. return true;
  241. }
  242. private short[] readData(int length) {
  243. // return data read or null if timeout
  244. long startdate = (new Date()).getTime();
  245. SerialReadThread t = new SerialReadThread(length);
  246. t.start();
  247. while (!t.dataIsReady()) {
  248. if ((new Date()).getTime() >= (startdate + (length * 1000))) {
  249. // More than (length * 1000) milliseconds went by
  250. return null;
  251. }
  252. }
  253. return t.getSerialData();
  254. }
  255. }