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.

HelperUtility.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * HelperUtility.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. * Helper class which runs our native library, which is loaded from inside the Jar.
  25. * @author Thomas Buck
  26. * @author Max Nuding
  27. * @author Felix Bäder
  28. * @version 1.0
  29. */
  30. import java.io.*;
  31. import java.nio.channels.*;
  32. import java.nio.*;
  33. import java.util.StringTokenizer;
  34. public class HelperUtility {
  35. // Load libraries, copy from Jar if needed
  36. // Mostly from:
  37. // http://stackoverflow.com/questions/1611357/how-to-make-a-jar-file-that-include-dll-files
  38. static {
  39. // System.out.println("Loading Serial Library...");
  40. loadFromJar();
  41. // System.out.println("Loaded!");
  42. }
  43. /**
  44. * When packaged into JAR extracts DLLs, places these into
  45. */
  46. private static void loadFromJar() {
  47. // we need to put DLL to temp dir
  48. String os = System.getProperty("os.name").toLowerCase();
  49. String path = "CC_";
  50. if (os.indexOf("windows") > -1) {
  51. loadLib(path, "Serial.dll");
  52. } else if (os.indexOf("mac") > -1) {
  53. loadLib(path, "libSerial.jnilib");
  54. } else {
  55. loadLib(path, "libSerial.so");
  56. }
  57. }
  58. /**
  59. * Puts library to temp dir and loads to memory
  60. * @param path Put in front of file name
  61. * @param Complete name of file to load (w/ lib & .dll)
  62. */
  63. private static void loadLib(String path, String name) {
  64. try {
  65. // have to use a stream
  66. InputStream in = HelperUtility.class.getResourceAsStream(name);
  67. File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + name);
  68. OutputStream out = new FileOutputStream(fileOut);
  69. ReadableByteChannel inChannel = Channels.newChannel(in);
  70. WritableByteChannel outChannel = Channels.newChannel(out);
  71. fastChannelCopy(inChannel, outChannel);
  72. inChannel.close();
  73. outChannel.close();
  74. path = fileOut.getPath();
  75. try {
  76. System.load(path);
  77. } catch (UnsatisfiedLinkError e) {
  78. System.out.println("ERROR: Library does not exist!");
  79. return;
  80. } catch (SecurityException e) {
  81. System.out.println("ERROR: Not allowed to load Library!");
  82. return;
  83. } catch (NullPointerException e) {
  84. System.out.println("ERROR: Library name is null!");
  85. return;
  86. } catch (Exception e) {
  87. System.out.println("ERROR: " + e.toString());
  88. return;
  89. }
  90. System.out.println("Loaded Serial Library at \"" + path + "\"");
  91. return;
  92. } catch (Exception e) {
  93. System.out.println("ERROR: Failed to load Serial Library:");
  94. e.printStackTrace();
  95. return;
  96. }
  97. }
  98. /**
  99. * Get the names of all available serial ports.
  100. *
  101. * @return Array of port names. First entry is "No serial ports!" if no
  102. * others
  103. */
  104. // Get ports as single String from getPortsOS() and put them in an array
  105. public static String[] getPorts() {
  106. String portLines = getPortsOS();
  107. if (portLines == null) {
  108. String[] ports = { "No serial ports!" };
  109. return ports;
  110. } else {
  111. StringTokenizer sT = new StringTokenizer(portLines, "\n");
  112. int size = sT.countTokens();
  113. String[] ports = new String[size];
  114. for (int i = 0; i < size; i++) {
  115. ports[i] = sT.nextToken();
  116. }
  117. return ports;
  118. }
  119. }
  120. /**
  121. * Get all the existing serial port names
  122. *
  123. * @return List of port names. \n between entries
  124. */
  125. private static String getPortsOS() {
  126. String os = System.getProperty("os.name").toLowerCase();
  127. try {
  128. if (os.indexOf("windows") > -1) {
  129. return getThePorts("COM");
  130. } else if (os.indexOf("mac") > -1) {
  131. return getThePorts("tty.");
  132. } else {
  133. return getThePorts("tty");
  134. }
  135. } catch (UnsatisfiedLinkError e) {
  136. System.out.println("ERROR: Library not loaded! (getPorts)");
  137. return "Serial Library Error!\n";
  138. }
  139. }
  140. private static native String getThePorts(String search);
  141. /**
  142. * Open Connection to a port
  143. *
  144. * @return TRUE if successful
  145. * @param name
  146. * Port to open
  147. */
  148. public static boolean openPort(String name) {
  149. try {
  150. return openPortNative(name);
  151. } catch (UnsatisfiedLinkError e) {
  152. System.out.println("ERROR: Library not loaded! (openPort)");
  153. return false;
  154. }
  155. }
  156. private static native boolean openPortNative(String name);
  157. /**
  158. * Close Connection to port
  159. */
  160. public static void closePort() {
  161. try {
  162. closePortNative();
  163. } catch (UnsatisfiedLinkError e) {
  164. System.out.println("ERROR: Library not loaded! (closePort)");
  165. }
  166. }
  167. private static native void closePortNative();
  168. /**
  169. * Read data from Cube
  170. *
  171. * @param length
  172. * Amount of data to read
  173. * @return Data read
  174. */
  175. public static short[] readData(int length) {
  176. try {
  177. return readDataNative(length);
  178. } catch (UnsatisfiedLinkError e) {
  179. System.out.println("ERROR: Library not loaded! (readData)");
  180. return null;
  181. }
  182. }
  183. private static native short[] readDataNative(int length);
  184. /**
  185. * Write data to Cube
  186. *
  187. * @param data
  188. * Data to write
  189. * @param length
  190. * Length of data
  191. */
  192. public static void writeData(short[] data, int length) {
  193. try {
  194. writeDataNative(data, length);
  195. } catch (UnsatisfiedLinkError e) {
  196. System.out.println("ERROR: Library not loaded! (writeData)");
  197. }
  198. }
  199. private static native void writeDataNative(short[] data, int length);
  200. // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
  201. private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
  202. ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
  203. while (src.read(buffer) != -1) {
  204. // prepare the buffer to be drained
  205. buffer.flip();
  206. // write to the channel, may block
  207. dest.write(buffer);
  208. // If partial transfer, shift remainder down
  209. // If buffer is empty, same as doing clear()
  210. buffer.compact();
  211. }
  212. // EOF will leave buffer in fill state
  213. buffer.flip();
  214. // make sure the buffer is fully drained.
  215. while (buffer.hasRemaining()) {
  216. dest.write(buffer);
  217. }
  218. }
  219. }