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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. */
  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. // System.out.println("Input Stream: " + in);
  66. File fileOut = new File(System.getProperty("java.io.tmpdir") + "/"
  67. + 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. System.load(fileOut.toString());
  75. System.out.println("Loaded Serial Library!");
  76. } catch (Exception e) {
  77. System.out.println("Failed to load Serial Library:");
  78. e.printStackTrace();
  79. System.exit(1);
  80. }
  81. }
  82. // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
  83. private static void fastChannelCopy(ReadableByteChannel src,
  84. WritableByteChannel dest) throws IOException {
  85. ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
  86. while (src.read(buffer) != -1) {
  87. // prepare the buffer to be drained
  88. buffer.flip();
  89. // write to the channel, may block
  90. dest.write(buffer);
  91. // If partial transfer, shift remainder down
  92. // If buffer is empty, same as doing clear()
  93. buffer.compact();
  94. }
  95. // EOF will leave buffer in fill state
  96. buffer.flip();
  97. // make sure the buffer is fully drained.
  98. while (buffer.hasRemaining()) {
  99. dest.write(buffer);
  100. }
  101. }
  102. /**
  103. * Get the names of all available serial ports.
  104. *
  105. * @return Array of port names. First entry is "No serial ports!" if no
  106. * others
  107. */
  108. public static String[] getPorts() {
  109. String portLines = getPortsOS();
  110. if (portLines == null) {
  111. String[] ports = { "Select serial port..." };
  112. return ports;
  113. } else {
  114. StringTokenizer sT = new StringTokenizer(portLines, "\n");
  115. int size = sT.countTokens();
  116. String[] ports = new String[size];
  117. for (int i = 0; i < size; i++) {
  118. ports[i] = sT.nextToken();
  119. }
  120. return ports;
  121. }
  122. }
  123. /**
  124. * Get all the existing serial port names
  125. *
  126. * @return List of port names. \n between entries
  127. */
  128. private static String getPortsOS() {
  129. String os = System.getProperty("os.name").toLowerCase();
  130. try {
  131. if (os.indexOf("windows") > -1) {
  132. return getThePorts("COM");
  133. } else if (os.indexOf("mac") > -1) {
  134. return getThePorts("tty.");
  135. } else {
  136. return getThePorts("tty");
  137. }
  138. } catch (Exception e) {
  139. // Unsatisfied linker error:
  140. // Serial.dll was probably not found
  141. System.out.println("Exception: " + e.toString());
  142. return null;
  143. }
  144. }
  145. private static native String getThePorts(String search);
  146. /**
  147. * Open Connection to a port
  148. *
  149. * @return TRUE if successful
  150. * @param name
  151. * Port to open
  152. */
  153. public static native boolean openPort(String name);
  154. /**
  155. * Close Connection to port
  156. */
  157. public static native void closePort();
  158. /**
  159. * Read data from Cube
  160. *
  161. * @param length
  162. * Amount of data to read
  163. * @return Data read
  164. */
  165. public static native short[] readData(int length);
  166. /**
  167. * Write data to Cube
  168. *
  169. * @param data
  170. * Data to write
  171. * @param length
  172. * Length of data
  173. */
  174. public static native void writeData(short[] data, int length);
  175. }