diff --git a/bindings/java/example/test.java b/bindings/java/example/test.java new file mode 100644 index 00000000..068ab743 --- /dev/null +++ b/bindings/java/example/test.java @@ -0,0 +1,98 @@ +import java.io.*; + +class test +{ + tellstick TS = new tellstick(); + + public void run() { + try { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + + int nbrDevices = TS.devGetNumberOfDevices(); + for (int i = 0; i < nbrDevices; i++) { + int id = TS.devGetDeviceId(i); + String deviceName = TS.devGetName(id); + System.out.println(id + "\t" + deviceName); + } + + System.out.print("Enter a device: "); + int device = Integer.parseInt(in.readLine()); + process(device); + + } catch (IOException e) { + } + } + + private void process(int device) { + try { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + int methods = TS.devMethods(device); + if ((methods & TS.TELLSTICK_TURNON) > 0) { + System.out.println(TS.TELLSTICK_TURNON + "\tTurn on"); + } + if ((methods & TS.TELLSTICK_TURNOFF) > 0) { + System.out.println(TS.TELLSTICK_TURNOFF + "\tTurn off"); + } + if ((methods & TS.TELLSTICK_BELL) > 0) { + System.out.println(TS.TELLSTICK_BELL + "\tBell"); + } + if ((methods & TS.TELLSTICK_DIM) > 0) { + System.out.println(TS.TELLSTICK_DIM + "\tDim"); + } + System.out.println("What do you want to do?"); + int action = Integer.parseInt(in.readLine()); + if ( (action & methods) == 0 ) { + System.out.println("The device doesn't support this method"); + return; + } + + if ((action & TS.TELLSTICK_TURNON) > 0) { + turnOn(device); + } else if ((action & TS.TELLSTICK_TURNOFF) > 0) { + turnOff(device); + } else if ((action & TS.TELLSTICK_BELL) > 0) { + bell(device); + } else if ((action & TS.TELLSTICK_DIM) > 0) { + dim(device); + } + + } catch (IOException e) { + } + } + + private void turnOn(int device) { + System.out.println("Turning on " + TS.devGetName(device)); + TS.devTurnOn(device); + } + + private void turnOff(int device) { + System.out.println("Turning off " + TS.devGetName(device)); + TS.devTurnOff(device); + } + + private void bell(int device) { + System.out.println("Sending bell to " + TS.devGetName(device)); + TS.devBell(device); + } + + private void dim(int device) { + try { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + System.out.print("Select level (0-255): "); + int level = Integer.parseInt(in.readLine()); + if (level >= 256 || level < 0) { + System.out.println("Out of range"); + } + System.out.println("Dim " + TS.devGetName(device)); + TS.devDim(device, level); + } catch (IOException e) { + } + } + + public static void main(String[] args) + { + test t = new test(); + t.run(); + } + +} \ No newline at end of file diff --git a/bindings/java/tellstick.c b/bindings/java/tellstick.c new file mode 100644 index 00000000..29b633dc --- /dev/null +++ b/bindings/java/tellstick.c @@ -0,0 +1,60 @@ +/*tellstick.c*/ +#include "tellstick.h" +#include +#include + +JNIEXPORT jboolean JNICALL +Java_tellstick_devTurnOn(JNIEnv *, jobject, jint intDeviceId) +{ + return (jboolean) devTurnOn( (int)intDeviceId); +} + + +JNIEXPORT jboolean JNICALL +Java_tellstick_devTurnOff(JNIEnv *, jobject, jint intDeviceId) +{ + return (jboolean) devTurnOff( (int)intDeviceId); +} + + +JNIEXPORT jboolean JNICALL +Java_tellstick_devBell(JNIEnv *, jobject, jint intDeviceId) +{ + return (jboolean) devBell( (int)intDeviceId); +} + + +JNIEXPORT jboolean JNICALL +Java_tellstick_devDim(JNIEnv *, jobject, jint intDeviceId, jint level) +{ + return (jboolean) devDim( (int)intDeviceId, (unsigned char) level); +} + + +JNIEXPORT jint JNICALL +Java_tellstick_devMethods(JNIEnv *, jobject, jint intDeviceId) +{ + return (jint) devMethods( (int)intDeviceId ); +} + + +JNIEXPORT jint JNICALL +Java_tellstick_devGetNumberOfDevices(JNIEnv *, jobject) +{ + return (jint)devGetNumberOfDevices(); +} + + +JNIEXPORT jint JNICALL +Java_tellstick_devGetDeviceId(JNIEnv *, jobject, jint intDeviceIndex) +{ + return (jint)devGetDeviceId( (int)intDeviceIndex ); +} + + +JNIEXPORT jstring JNICALL +Java_tellstick_devGetName(JNIEnv *env, jobject, jint intDeviceId) +{ + const char *name = devGetName( (int)intDeviceId ); + return env->NewStringUTF(name); +} diff --git a/bindings/java/tellstick.h b/bindings/java/tellstick.h new file mode 100644 index 00000000..cde08d2e --- /dev/null +++ b/bindings/java/tellstick.h @@ -0,0 +1,77 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class tellstick */ + +#ifndef _Included_tellstick +#define _Included_tellstick +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: tellstick + * Method: devTurnOn + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_tellstick_devTurnOn + (JNIEnv *, jobject, jint); + +/* + * Class: tellstick + * Method: devTurnOff + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_tellstick_devTurnOff + (JNIEnv *, jobject, jint); + +/* + * Class: tellstick + * Method: devBell + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_tellstick_devBell + (JNIEnv *, jobject, jint); + +/* + * Class: tellstick + * Method: devDim + * Signature: (II)Z + */ +JNIEXPORT jboolean JNICALL Java_tellstick_devDim + (JNIEnv *, jobject, jint, jint); + +/* + * Class: tellstick + * Method: devMethods + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_tellstick_devMethods + (JNIEnv *, jobject, jint); + +/* + * Class: tellstick + * Method: devGetNumberOfDevices + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_tellstick_devGetNumberOfDevices + (JNIEnv *, jobject); + +/* + * Class: tellstick + * Method: devGetDeviceId + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_tellstick_devGetDeviceId + (JNIEnv *, jobject, jint); + +/* + * Class: tellstick + * Method: devGetName + * Signature: (I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_tellstick_devGetName + (JNIEnv *, jobject, jint); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/bindings/java/tellstick.java b/bindings/java/tellstick.java new file mode 100644 index 00000000..c3325db9 --- /dev/null +++ b/bindings/java/tellstick.java @@ -0,0 +1,27 @@ +//firstJNI.java + +public class tellstick +{ + public final int TELLSTICK_TURNON = 1; + public final int TELLSTICK_TURNOFF = 2; + public final int TELLSTICK_BELL = 4; + public final int TELLSTICK_DIM = 16; + + public native boolean devTurnOn(int intDeviceId); + public native boolean devTurnOff(int intDeviceId); + public native boolean devBell(int intDeviceId); + public native boolean devDim(int intDeviceId, int level); + public native int devMethods(int intDeviceId); + + public native int devGetNumberOfDevices(); + public native int devGetDeviceId(int intDeviceIndex); + + public native String devGetName(int intDeviceId); + + static { + System.loadLibrary("tellstickJNI"); + } + + +} +