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

HelperUtility.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. public class HelperUtility {
  34. // Load libraries, copy from Jar if needed
  35. // Mostly from:
  36. // http://stackoverflow.com/questions/1611357/how-to-make-a-jar-file-that-include-dll-files
  37. static {
  38. // System.out.println("Loading Serial Library...");
  39. loadFromJar();
  40. // System.out.println("Loaded!");
  41. }
  42. /**
  43. * When packaged into JAR extracts DLLs, places these into
  44. */
  45. private static void loadFromJar() {
  46. // we need to put DLL to temp dir
  47. String os = System.getProperty("os.name").toLowerCase();
  48. String path = "CC_";
  49. if (os.indexOf("windows") > -1) {
  50. loadLib(path, "Serial.dll");
  51. } else if (os.indexOf("mac") > -1) {
  52. loadLib(path, "libSerial.jnilib");
  53. } else {
  54. loadLib(path, "libSerial.so");
  55. }
  56. }
  57. /**
  58. * Puts library to temp dir and loads to memory
  59. */
  60. private static void loadLib(String path, String name) {
  61. try {
  62. // have to use a stream
  63. InputStream in = HelperUtility.class.getResourceAsStream(name);
  64. // System.out.println("Input Stream: " + in);
  65. File fileOut = new File(System.getProperty("java.io.tmpdir") + "/"
  66. + path + name);
  67. OutputStream out = new FileOutputStream(fileOut);
  68. ReadableByteChannel inChannel = Channels.newChannel(in);
  69. WritableByteChannel outChannel = Channels.newChannel(out);
  70. fastChannelCopy(inChannel, outChannel);
  71. inChannel.close();
  72. outChannel.close();
  73. System.load(fileOut.toString());
  74. System.out.println("Loaded Serial Library from " + fileOut.toString());
  75. } catch (Exception e) {
  76. System.out.println("Failed to load Serial Library:");
  77. e.printStackTrace();
  78. System.exit(1);
  79. }
  80. }
  81. // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
  82. private static void fastChannelCopy(ReadableByteChannel src,
  83. WritableByteChannel dest) throws IOException {
  84. ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
  85. while (src.read(buffer) != -1) {
  86. // prepare the buffer to be drained
  87. buffer.flip();
  88. // write to the channel, may block
  89. dest.write(buffer);
  90. // If partial transfer, shift remainder down
  91. // If buffer is empty, same as doing clear()
  92. buffer.compact();
  93. }
  94. // EOF will leave buffer in fill state
  95. buffer.flip();
  96. // make sure the buffer is fully drained.
  97. while (buffer.hasRemaining()) {
  98. dest.write(buffer);
  99. }
  100. }
  101. /**
  102. * Get all the existing serial port names
  103. *
  104. * @return List of port names. \n between entries
  105. */
  106. public static String getPorts() {
  107. String os = System.getProperty("os.name").toLowerCase();
  108. try {
  109. if (os.indexOf("windows") > -1) {
  110. return getThePorts("COM");
  111. } else if (os.indexOf("max") > -1) {
  112. return getThePorts("tty.");
  113. } else {
  114. return getThePorts("tty");
  115. }
  116. } catch (Exception e) {
  117. // Unsatisfied linker error:
  118. // Serial.dll was probably not found
  119. System.out.println("Exception: " + e.toString());
  120. } finally {
  121. return null;
  122. }
  123. }
  124. private static native String getThePorts(String search);
  125. /**
  126. * Open Connection to a port
  127. *
  128. * @return TRUE if successful
  129. * @param name
  130. * Port to open
  131. */
  132. public static native boolean openPort(String name);
  133. /**
  134. * Close Connection to port
  135. */
  136. public static native void closePort();
  137. /**
  138. * Read data from Cube
  139. *
  140. * @param length
  141. * Amount of data to read
  142. * @return Data read
  143. */
  144. public static native short[] readData(int length);
  145. /**
  146. * Write data to Cube
  147. *
  148. * @param data
  149. * Data to write
  150. * @param length
  151. * Length of data
  152. */
  153. public static native void writeData(short[] data, int length);
  154. }