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

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