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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. public class SerialHelper {
  33. private final short OK = 0x42;
  34. private final short ERROR = 0x23;
  35. /**
  36. * Create new SerialHelper.
  37. * @param serialPort Name of serial port to use.
  38. * @throws Exception Could not open serial port.
  39. */
  40. public SerialHelper(String serialPort) throws Exception {
  41. if (HelperUtility.openPort(serialPort) == false) {
  42. throw new Exception("Could not open serial port \"" + serialPort + "\"");
  43. }
  44. }
  45. /**
  46. * Recieve all animations saved in cube.
  47. * @return A cubeWorker populated with the new data or null.
  48. */
  49. public cubeWorker getAnimationsFromCube() {
  50. return null;
  51. }
  52. /**
  53. * Send all animations in a cubeWorker to the Cube.
  54. * @param worker cubeWorker that containts data.
  55. */
  56. public void sendAnimationsToCube(cubeWorker worker) {
  57. short[] data;
  58. short[] tmp = new short[1];
  59. // Send animation count
  60. tmp[0] = (short)worker.numOfAnimations();
  61. if (!writeData(tmp)) {
  62. System.out.println("SerialHelper: Timeout numOfAnimations");
  63. return;
  64. }
  65. data = readData(1);
  66. if ((data != null) && (data[0] != OK)) {
  67. System.out.println("SerialHelper: Response Error");
  68. return;
  69. }
  70. // Send animations
  71. for (int a = 0; a < worker.numOfAnimations(); a++) {
  72. // Send frame count
  73. tmp[0] = (short)worker.numOfFrames(a);
  74. if (!writeData(tmp)) {
  75. System.out.println("SerialHelper: Timeout numOfFrames");
  76. return;
  77. }
  78. data = readData(1);
  79. if ((data != null) && (data[0] != OK)) {
  80. System.out.println("SerialHelper: Response Error");
  81. return;
  82. }
  83. // Send frames
  84. for (int f = 0; f < worker.numOfFrames(a); f++) {
  85. // Frame duration
  86. tmp[0] = worker.getFrameTime(a, f);
  87. if (!writeData(tmp)) {
  88. System.out.println("SerialHelper: Timeout Frame duration");
  89. return;
  90. }
  91. data = readData(1);
  92. if ((data != null) && (data[0] != OK)) {
  93. System.out.println("SerialHelper: Response Error");
  94. return;
  95. }
  96. // Frame data
  97. if (!writeData(worker.getFrame(a, f))) {
  98. System.out.println("SerialHelper: Timeout Frame");
  99. return;
  100. }
  101. data = readData(1);
  102. if ((data != null) && (data[0] != OK)) {
  103. System.out.println("SerialHelper: Response Error");
  104. return;
  105. }
  106. }
  107. }
  108. // Send finish
  109. tmp = new short[4];
  110. tmp[0] = OK;
  111. tmp[1] = OK;
  112. tmp[2] = OK;
  113. tmp[3] = OK;
  114. if (!writeData(tmp)) {
  115. System.out.println("SerialHelper: Timeout Finish");
  116. return;
  117. }
  118. data = readData(1);
  119. if ((data != null) && (data[0] != OK)) {
  120. System.out.println("SerialHelper: Response Error");
  121. return;
  122. }
  123. }
  124. /**
  125. * Close the serial port again.
  126. */
  127. public void closeSerialPort() {
  128. HelperUtility.closePort();
  129. }
  130. private boolean writeData(short[] data) {
  131. // write data. return true if success
  132. long startdate = (new Date()).getTime();
  133. SerialWriteThread t = new SerialWriteThread(data);
  134. t.start();
  135. while (!t.dataWasSent()) {
  136. if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
  137. // More than (length * 1000) milliseconds went by
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. private short[] readData(int length) {
  144. // return data read or null if timeout
  145. long startdate = (new Date()).getTime();
  146. SerialReadThread t = new SerialReadThread(length);
  147. t.start();
  148. while (!t.dataIsReady()) {
  149. if ((new Date()).getTime() >= (startdate + (length * 1000))) {
  150. // More than (length * 1000) milliseconds went by
  151. return null;
  152. }
  153. }
  154. return t.getSerialData();
  155. }
  156. }