Bläddra i källkod

Library in jar now

Thomas Buck 13 år sedan
förälder
incheckning
40aa3b36cf
3 ändrade filer med 79 tillägg och 15 borttagningar
  1. 66
    2
      CubeControl/HelperUtility.java
  2. 9
    9
      CubeControl/makefile
  3. 4
    4
      README.md

+ 66
- 2
CubeControl/HelperUtility.java Visa fil

@@ -22,17 +22,81 @@
22 22
  */
23 23
 
24 24
 /**
25
- * Helper class which runs our native library.
25
+ * Helper class which runs our native library, which is loaded from inside the Jar.
26 26
  * @author Thomas Buck
27 27
  * @author Max Nuding
28 28
  * @author Felix Bäder
29 29
  * @version 1.0
30 30
  */
31 31
 
32
+import java.io.*;
33
+import java.nio.channels.*;
34
+import java.nio.*;
35
+
32 36
 public class HelperUtility {
33 37
 
38
+	// Load libraries, copy from Jar if needed
39
+	// Mostly from:
40
+	// http://stackoverflow.com/questions/1611357/how-to-make-a-jar-file-that-include-dll-files
34 41
 	static {
35
-		System.loadLibrary("Serial");
42
+		// System.out.println("Loading Serial Library...");
43
+		loadFromJar();
44
+	}
45
+
46
+	/**
47
+	 * When packaged into JAR extracts DLLs, places these into
48
+	 */
49
+	private static void loadFromJar() {
50
+		// we need to put DLL to temp dir
51
+		String os = System.getProperty("os.name").toLowerCase();
52
+		String path = "CC_";
53
+		if (os.indexOf("windows") > -1) {
54
+			loadLib(path, "Serial.dll");
55
+		} else if (os.indexOf("mac") > -1) {
56
+			loadLib(path, "libSerial.jnilib");
57
+		}
58
+	}
59
+
60
+	/**
61
+	 * Puts library to temp dir and loads to memory
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
+			// System.out.println("Input Stream: " + in);
68
+			File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + name);
69
+			OutputStream out = new FileOutputStream(fileOut);
70
+			ReadableByteChannel inChannel = Channels.newChannel(in);
71
+			WritableByteChannel outChannel = Channels.newChannel(out);
72
+			fastChannelCopy(inChannel, outChannel);
73
+			inChannel.close();
74
+			outChannel.close();
75
+			System.load(fileOut.toString());
76
+		} catch (Exception e) {
77
+			System.out.println("Failed to load Serial Library:");
78
+			e.printStackTrace();
79
+		}
80
+	}
81
+
82
+	// http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
83
+	public static void fastChannelCopy(ReadableByteChannel src, 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
+		}
36 100
 	}
37 101
 
38 102
 	/**

+ 9
- 9
CubeControl/makefile Visa fil

@@ -3,27 +3,28 @@ CC = gcc
3 3
 # Path to jni.h
4 4
 ifdef SystemRoot
5 5
 HEADERPATH = C:/Program\ Files/Java/jdk1.6.0_29/include
6
-$(RM) = del
6
+RM = del
7
+INJAR = *.class *.png Serial.dll
8
+PLATFORM = Win
7 9
 else
8 10
 HEADERPATH = /System/Library/Frameworks/JavaVM.framework/Headers
9
-$(RM) = rm -f
11
+RM = rm -f
12
+INJAR = *.class *.png libSerial.jnilib
13
+PLATFORM = Mac
10 14
 endif
11 15
 
12 16
 # All java files to be compiled
13 17
 # List so it works as target
14 18
 JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java
15 19
 
16
-# Files that go in the jar
17
-INJAR = *.class *.png
18
-
19 20
 # --------------------------------------
20 21
 
21
-all: libSerial CubeControl.jar
22
+all: libSerial CubeControl.jar clean
22 23
 
23 24
 doc: doc/index.html
24 25
 
25
-CubeControl.jar: HelperUtility.class manifest.txt
26
-	jar -cmf manifest.txt "CubeControl.jar" $(INJAR)
26
+CubeControl.jar: HelperUtility.class manifest.txt libSerial
27
+	jar -cmf manifest.txt "CubeControl$(PLATFORM).jar" $(INJAR)
27 28
 
28 29
 serialInterface.h: HelperUtility.class
29 30
 	javah -o serialInterface.h HelperUtility
@@ -54,7 +55,6 @@ clean:
54 55
 	$(RM) *.class
55 56
 	$(RM) *.h
56 57
 	$(RM) *.o
57
-	$(RM) *.jar
58 58
 ifdef SystemRoot
59 59
 	$(RM) *.dll
60 60
 else

+ 4
- 4
README.md Visa fil

@@ -13,10 +13,10 @@ It will use [Peter Fleury's UART and TWI Library](http://homepage.hispeed.ch/pet
13 13
 
14 14
 ## Cube Control
15 15
 
16
-We also build a software to create and load animations into the cube. This software is written in Java and C and is Compatible between Windows and Unix.
17
-It's source is in the "Cube Control" directory. It has it's own makefile, in which you can specify the Target OS. "unix" means any Unix like OS (Linux, OS X...), "win" means Windows.
18
-You obviously need a working JDK and a C Compiler Environment (we use gcc).
16
+We also build a software to create and load animations into the cube. This software is written in Java and C and should work on Windows and Unix.
17
+It's source is in the "Cube Control" directory.
19 18
 
20 19
 ## Build instructions
21 20
 
22
-Theres a global makefile in the top folder. If you run it, it will (probably) create CubeControl.jar, CubeFirmware.hex and a OS-dependent serial library (Serial.dll or libSerial.jnilib...). If not, you should take a look at the makefile in CubeControl. Hard-Coded include directorys are probably different than on your system...
21
+Theres a global makefile in the top folder. If you run it, it will (probably) create CubeControl.jar, CubeFirmware.hex and a OS-dependent serial library (Serial.dll or libSerial.jnilib...). If not, you should take a look at the makefile in CubeControl. Hard-Coded include directorys are probably different than on your system... You obviously need a working JDK and a C Compiler Environment (we use gcc).
22
+CubeControls makefile will autodetect a Windows Host and compile a Windows Version accordingly. If it is not on Windows, it will compile a Mac Version. Unix is currently not supported by the makefile, but should work if compiled manually.

Laddar…
Avbryt
Spara