Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HelperUtility.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. }
  87. System.out.println("Loaded Serial Library at \"" + path + "\"");
  88. } catch (Exception e) {
  89. System.out.println("ERROR: Failed to load Serial Library:");
  90. e.printStackTrace();
  91. }
  92. }
  93. // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
  94. private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
  95. ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
  96. while (src.read(buffer) != -1) {
  97. // prepare the buffer to be drained
  98. buffer.flip();
  99. // write to the channel, may block
  100. dest.write(buffer);
  101. // If partial transfer, shift remainder down
  102. // If buffer is empty, same as doing clear()
  103. buffer.compact();
  104. }
  105. // EOF will leave buffer in fill state
  106. buffer.flip();
  107. // make sure the buffer is fully drained.
  108. while (buffer.hasRemaining()) {
  109. dest.write(buffer);
  110. }
  111. }
  112. /**
  113. * Get the names of all available serial ports.
  114. *
  115. * @return Array of port names. First entry is "No serial ports!" if no
  116. * others
  117. */
  118. public static String[] getPorts() {
  119. String portLines = getPortsOS();
  120. if (portLines == null) {
  121. String[] ports = { "Select serial port..." };
  122. return ports;
  123. } else {
  124. StringTokenizer sT = new StringTokenizer(portLines, "\n");
  125. int size = sT.countTokens();
  126. String[] ports = new String[size];
  127. for (int i = 0; i < size; i++) {
  128. ports[i] = sT.nextToken();
  129. }
  130. return ports;
  131. }
  132. }
  133. /**
  134. * Get all the existing serial port names
  135. *
  136. * @return List of port names. \n between entries
  137. */
  138. private static String getPortsOS() {
  139. String os = System.getProperty("os.name").toLowerCase();
  140. try {
  141. if (os.indexOf("windows") > -1) {
  142. return getThePorts("COM");
  143. } else if (os.indexOf("mac") > -1) {
  144. return getThePorts("tty.");
  145. } else {
  146. return getThePorts("tty");
  147. }
  148. } catch (Exception e) {
  149. System.out.println("Exception: " + e.toString());
  150. return null;
  151. } catch (UnsatisfiedLinkError e) {
  152. System.out.println("ERROR: Library not loaded! (getPorts)");
  153. // System.out.println(e.toString());
  154. // Show error message because this is called in the initializer.
  155. Frame.errorMessageStat("Could not load Serial Library!\nNo Serial functionality available!");
  156. return null;
  157. }
  158. }
  159. private static native String getThePorts(String search);
  160. /**
  161. * Open Connection to a port
  162. *
  163. * @return TRUE if successful
  164. * @param name
  165. * Port to open
  166. */
  167. public static boolean openPort(String name) {
  168. try {
  169. return openPortNative(name);
  170. } catch (UnsatisfiedLinkError e) {
  171. System.out.println("ERROR: Library not loaded! (openPort)");
  172. // System.out.println(e.toString());
  173. return false;
  174. }
  175. }
  176. private static native boolean openPortNative(String name);
  177. /**
  178. * Close Connection to port
  179. */
  180. public static void closePort() {
  181. try {
  182. closePortNative();
  183. } catch (UnsatisfiedLinkError e) {
  184. System.out.println("ERROR: Library not loaded! (closePort)");
  185. // System.out.println(e.toString());
  186. }
  187. }
  188. private static native void closePortNative();
  189. /**
  190. * Read data from Cube
  191. *
  192. * @param length
  193. * Amount of data to read
  194. * @return Data read
  195. */
  196. public static short[] readData(int length) {
  197. try {
  198. return readDataNative(length);
  199. } catch (UnsatisfiedLinkError e) {
  200. System.out.println("ERROR: Library not loaded! (readData)");
  201. // System.out.println(e.toString());
  202. return null;
  203. }
  204. }
  205. private static native short[] readDataNative(int length);
  206. /**
  207. * Write data to Cube
  208. *
  209. * @param data
  210. * Data to write
  211. * @param length
  212. * Length of data
  213. */
  214. public static void writeData(short[] data, int length) {
  215. try {
  216. writeDataNative(data, length);
  217. } catch (UnsatisfiedLinkError e) {
  218. System.out.println("ERROR: Library not loaded! (writeData)");
  219. // System.out.println(e.toString());
  220. }
  221. }
  222. private static native void writeDataNative(short[] data, int length);
  223. }