Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HelperUtility.java 6.4KB

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