Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HelperUtility.java 4.2KB

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