Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

HelperUtility.java 6.3KB

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