Added Java JNI wrapper.

This commit is contained in:
Micke Prag 2008-03-29 18:42:39 +00:00
parent cff8a15b60
commit 4bc36dd343
4 changed files with 262 additions and 0 deletions

View file

@ -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();
}
}

60
bindings/java/tellstick.c Normal file
View file

@ -0,0 +1,60 @@
/*tellstick.c*/
#include "tellstick.h"
#include <stdio.h>
#include <TellUsbD101.h>
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);
}

77
bindings/java/tellstick.h Normal file
View file

@ -0,0 +1,77 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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

View file

@ -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");
}
}