Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HelperUtility.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  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. }
  53. }
  54. /**
  55. * Puts library to temp dir and loads to memory
  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. // System.out.println("Input Stream: " + in);
  62. File fileOut = new File(System.getProperty("java.io.tmpdir") + "/"
  63. + path + name);
  64. OutputStream out = new FileOutputStream(fileOut);
  65. ReadableByteChannel inChannel = Channels.newChannel(in);
  66. WritableByteChannel outChannel = Channels.newChannel(out);
  67. fastChannelCopy(inChannel, outChannel);
  68. inChannel.close();
  69. outChannel.close();
  70. System.load(fileOut.toString());
  71. } catch (Exception e) {
  72. System.out.println("Failed to load Serial Library:");
  73. e.printStackTrace();
  74. }
  75. }
  76. // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
  77. public static void fastChannelCopy(ReadableByteChannel src,
  78. WritableByteChannel dest) throws IOException {
  79. ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
  80. while (src.read(buffer) != -1) {
  81. // prepare the buffer to be drained
  82. buffer.flip();
  83. // write to the channel, may block
  84. dest.write(buffer);
  85. // If partial transfer, shift remainder down
  86. // If buffer is empty, same as doing clear()
  87. buffer.compact();
  88. }
  89. // EOF will leave buffer in fill state
  90. buffer.flip();
  91. // make sure the buffer is fully drained.
  92. while (buffer.hasRemaining()) {
  93. dest.write(buffer);
  94. }
  95. }
  96. /**
  97. * Get all the existing serial port names
  98. *
  99. * @return List of port names. \n between entries
  100. */
  101. public static String getPorts() {
  102. String os = System.getProperty("os.name").toLowerCase();
  103. if (os.indexOf("windows") > -1) {
  104. return getThePorts("COM");
  105. } else if (os.indexOf("linux") > -1) {
  106. return getThePorts("tty");
  107. } else if (os.indexOf("mac") > -1) {
  108. return getThePorts("tty.");
  109. }
  110. // Not linux, windows or mac?
  111. return getThePorts("wtf?");
  112. }
  113. private static native String getThePorts(String search);
  114. /**
  115. * Open Connection to a port
  116. *
  117. * @return TRUE if successful
  118. * @param name
  119. * Port to open
  120. */
  121. public static native boolean openPort(String name);
  122. /**
  123. * Close Connection to port
  124. */
  125. public static native void closePort();
  126. /**
  127. * Read data from Cube
  128. *
  129. * @param length
  130. * Amount of data to read
  131. * @return Data read
  132. */
  133. public static native short[] readData(int length);
  134. /**
  135. * Write data to Cube
  136. *
  137. * @param data
  138. * Data to write
  139. * @param length
  140. * Length of data
  141. */
  142. public static native void writeData(short[] data, int length);
  143. }