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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 sending probe for Cube");
  60. return false;
  61. }
  62. data = readData(1);
  63. if ((data == null) || (data[0] != OK)) {
  64. printErrorMessage("No response from cube!");
  65. if (data == null) {
  66. System.out.println("Probe was null!");
  67. } else {
  68. System.out.println("Probe was " + data[0]);
  69. }
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * Recieve all animations saved in cube.
  76. * @return A cubeWorker populated with the new data or null.
  77. */
  78. public cubeWorker getAnimationsFromCube() {
  79. Animation[] animations;
  80. int animationCount, frameCount;
  81. short[] data, tmp = new short[1];
  82. // Send download command
  83. tmp[0] = 'g';
  84. if (!writeData(tmp)) {
  85. printErrorMessage("Timout sending Command");
  86. return null;
  87. }
  88. data = readData(1);
  89. if ((data == null) || (data[0] != OK)) {
  90. printErrorMessage("Response Error");
  91. if (data == null) {
  92. System.out.println("Download was null!");
  93. } else {
  94. System.out.println("Download was " + data[0]);
  95. }
  96. return null;
  97. }
  98. // Get animation count
  99. data = readData(1);
  100. if (data == null) {
  101. printErrorMessage("Response Error.");
  102. System.out.println("Did not get animation count!");
  103. return null;
  104. } else {
  105. animationCount = data[0];
  106. }
  107. tmp[0] = OK;
  108. if (!writeData(tmp)) {
  109. printErrorMessage("Timout acknowledging animation count!");
  110. return null;
  111. }
  112. animations = new Animation[animationCount];
  113. // Get animations
  114. for (int a = 0; a < animationCount; a++) {
  115. Animation currentAnim = new Animation();
  116. // Get number of frames
  117. data = readData(1);
  118. if (data == null) {
  119. printErrorMessage("Response Error");
  120. System.out.println("Did not get frame count!");
  121. return null;
  122. } else {
  123. frameCount = data[0];
  124. }
  125. tmp[0] = OK;
  126. if (!writeData(tmp)) {
  127. printErrorMessage("Timout sending response Frame Count.");
  128. return null;
  129. }
  130. // Get frames
  131. for (int f = 0; f < frameCount; f++) {
  132. AFrame currentFrame = new AFrame();
  133. // Get frame duration
  134. data = readData(1);
  135. if (data == null) {
  136. printErrorMessage("Response Error");
  137. System.out.println("Did not get frame duration!");
  138. return null;
  139. } else {
  140. currentFrame.setTime(data[0]);
  141. }
  142. tmp[0] = OK;
  143. if (!writeData(tmp)) {
  144. printErrorMessage("Timout sending response Frame Duration");
  145. return null;
  146. }
  147. // Get frame data
  148. data = readData(64);
  149. if (data == null) {
  150. printErrorMessage("Response Error");
  151. System.out.println("Did not get frame data!");
  152. return null;
  153. } else {
  154. currentFrame.setData(data);
  155. }
  156. tmp[0] = OK;
  157. if (!writeData(tmp)) {
  158. printErrorMessage("Timout sending response Frame Data");
  159. return null;
  160. }
  161. // Add frame to animation
  162. currentAnim.setFrame(currentFrame, f);
  163. }
  164. // Add animation to animations list
  165. animations[a] = currentAnim;
  166. }
  167. return new cubeWorker(animations, frame);
  168. }
  169. /**
  170. * Send all animations in a cubeWorker to the Cube.
  171. * @param worker cubeWorker that containts data.
  172. * @return 0 on success. -1 on error.
  173. */
  174. public int sendAnimationsToCube(cubeWorker worker) {
  175. short[] data, tmp = new short[1];
  176. // Send upload command
  177. tmp[0] = 's';
  178. if (!writeData(tmp)) {
  179. printErrorMessage("Timout sending Command");
  180. return -1;
  181. }
  182. data = readData(1);
  183. if ((data == null) || (data[0] != OK)) {
  184. printErrorMessage("Response Error Command");
  185. if (data == null) {
  186. System.out.println("Response Command was null!");
  187. } else {
  188. System.out.println("Response Command was " + data[0]);
  189. }
  190. return -1;
  191. }
  192. // Send animation count
  193. tmp[0] = (short)worker.size();
  194. if (!writeData(tmp)) {
  195. printErrorMessage("Timeout sending numOfAnimations");
  196. return -1;
  197. }
  198. data = readData(1);
  199. if ((data == null) || (data[0] != OK)) {
  200. printErrorMessage("Response Error");
  201. if (data == null) {
  202. System.out.println("Response Count was null!");
  203. } else {
  204. System.out.println("Response Count was " + data[0]);
  205. }
  206. return -1;
  207. }
  208. // Send animations
  209. for (int a = 0; a < worker.size(); a++) {
  210. // Send frame count
  211. tmp[0] = (short)worker.getAnimation(a).size();
  212. if (!writeData(tmp)) {
  213. printErrorMessage("Timeout sending numOfFrames");
  214. return -1;
  215. }
  216. data = readData(1);
  217. if ((data == null) || (data[0] != OK)) {
  218. printErrorMessage("Response Error");
  219. if (data == null) {
  220. System.out.println("Frame Count was null!");
  221. } else {
  222. System.out.println("Frame Count was " + data[0]);
  223. }
  224. return -1;
  225. }
  226. // Send frames
  227. for (int f = 0; f < worker.getAnimation(a).size(); f++) {
  228. // Frame duration
  229. tmp[0] = worker.getAnimation(a).getFrame(f).getTime();
  230. if (!writeData(tmp)) {
  231. printErrorMessage("Timeout sending Frame duration");
  232. return -1;
  233. }
  234. data = readData(1);
  235. if ((data == null) || (data[0] != OK)) {
  236. printErrorMessage("Response Error");
  237. if (data == null) {
  238. System.out.println("Duration was null!");
  239. } else {
  240. System.out.println("Duration was " + data[0]);
  241. }
  242. return -1;
  243. }
  244. // Frame data
  245. if (!writeData(worker.getAnimation(a).getFrame(f).getData())) {
  246. printErrorMessage("Timeout sending Frame");
  247. return -1;
  248. }
  249. data = readData(1);
  250. if ((data == null) || (data[0] != OK)) {
  251. printErrorMessage("Response Error");
  252. if (data == null) {
  253. System.out.println("Datawas null!");
  254. } else {
  255. System.out.println("Data was " + data[0]);
  256. }
  257. return -1;
  258. }
  259. }
  260. }
  261. // Send finish
  262. tmp = new short[4];
  263. tmp[0] = OK;
  264. tmp[1] = OK;
  265. tmp[2] = OK;
  266. tmp[3] = OK;
  267. if (!writeData(tmp)) {
  268. printErrorMessage("Timeout sending Finish");
  269. return -1;
  270. }
  271. data = readData(1);
  272. if ((data == null) || (data[0] != OK)) {
  273. printErrorMessage("Response Error");
  274. if (data == null) {
  275. System.out.println("Finish was null!");
  276. } else {
  277. System.out.println("Finish was " + data[0]);
  278. }
  279. return -1;
  280. }
  281. return 0;
  282. }
  283. /**
  284. * Close the serial port again.
  285. */
  286. public void closeSerialPort() {
  287. HelperUtility.closePort();
  288. }
  289. private void printErrorMessage(String s) {
  290. System.out.println("SerialHelper: " + s);
  291. frame.errorMessage("Serial Error", s);
  292. }
  293. private boolean writeData(short[] data) {
  294. // write data. return true if success
  295. long startdate = (new Date()).getTime();
  296. SerialWriteThread t = new SerialWriteThread(data);
  297. t.start();
  298. while (!t.dataWasSent()) {
  299. if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
  300. // More than (length * 1000) milliseconds went by
  301. // => 1 second per byte
  302. return false;
  303. }
  304. }
  305. return true;
  306. }
  307. private short[] readData(int length) {
  308. // return data read or null if timeout
  309. long startdate = (new Date()).getTime();
  310. SerialReadThread t = new SerialReadThread(length);
  311. t.start();
  312. while (!t.dataIsReady()) {
  313. if ((new Date()).getTime() >= (startdate + (length * 1000))) {
  314. // More than (length * 1000) milliseconds went by
  315. // => 1 second per byte
  316. return null;
  317. }
  318. }
  319. return t.getSerialData();
  320. }
  321. }