Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider b5005e8f rédigé par Pierre LINDENBAUM's avatar Pierre LINDENBAUM :speech_balloon:
Parcourir les fichiers

min / max

parent 1d4706ac
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,7 +5,7 @@ dist/mycomedi.jar: ...@@ -5,7 +5,7 @@ dist/mycomedi.jar:
rm -rf _tmp rm -rf _tmp
mkdir -p $(dir $@) _tmp mkdir -p $(dir $@) _tmp
javac -d _tmp -sourcepath src/main/java src/main/java/com/github/lindenb/mycomedi/Test.java javac -d _tmp -sourcepath src/main/java src/main/java/com/github/lindenb/mycomedi/Test.java
javah -cp _tmp -d src/main/jni com.github.lindenb.mycomedi.jni.ComediJNI javah -cp _tmp -d src/main/jni com.github.lindenb.mycomedi.ComediJNI
(cd src/main/jni && make) (cd src/main/jni && make)
jar cvf $@ -C _tmp . jar cvf $@ -C _tmp .
rm -rf _tmp rm -rf _tmp
......
package com.github.lindenb.mycomedi.jni; package com.github.lindenb.mycomedi;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -18,7 +18,18 @@ public static void init() { ...@@ -18,7 +18,18 @@ public static void init() {
} }
} }
} }
// constants
public native static int AREF_GROUND();
public native static int COMEDI_OOR_NAN();
public native static long comedi_open(final String s); native static long comedi_open(final String s);
public native static void comedi_close(final long ptr); native static void comedi_close(final long ptr);
native static void comedi_perror(final String s);
native static long comedi_data_read(final long device,int subdev, int chan,int range,int aref);
native static int comedi_set_global_oor_behavior(int v);
native static long comedi_get_range(final long device, int subdev,int chan,int range);
native static long comedi_get_maxdata(final long device, int subdev,int chan);
native static long range_info_min(final long ptr);
native static long range_info_max(final long ptr);
} }
package com.github.lindenb.mycomedi; package com.github.lindenb.mycomedi;
import com.github.lindenb.mycomedi.jni.ComediJNI;
import java.util.logging.Logger; import java.util.logging.Logger;
class Device { class Device {
private static final Logger LOG= Logger.getLogger("Device"); private static final Logger LOG= Logger.getLogger("Device");
private final String name; private final String name;
private long _ptr; private long _ptr;
...@@ -13,6 +14,7 @@ public Device() ...@@ -13,6 +14,7 @@ public Device()
} }
public Device(final String name) { public Device(final String name) {
ComediJNI.init();
this.name=name; this.name=name;
LOG.info("Opening "+name); LOG.info("Opening "+name);
this._ptr = ComediJNI.comedi_open(name); this._ptr = ComediJNI.comedi_open(name);
...@@ -23,6 +25,22 @@ public void close() ...@@ -23,6 +25,22 @@ public void close()
ComediJNI.comedi_close(this._ptr); ComediJNI.comedi_close(this._ptr);
this._ptr=0L; this._ptr=0L;
} }
@Override
public String toString() {return this.name;} public long readData(int subdev, int chan, int range,int aref)
{
return ComediJNI.comedi_data_read(this._ptr, subdev, chan,range,aref);
}
public Range getRange(int subdev, int chan, int range)
{
return new Range(ComediJNI.comedi_get_range(this._ptr, subdev, chan,range));
}
public long getMaxData(int subdev, int chan)
{
return ComediJNI.comedi_get_maxdata(this._ptr, subdev, chan);
}
@Override
public String toString() {return this.name;}
} }
package com.github.lindenb.mycomedi;
import java.util.logging.Logger;
class Range {
private static final Logger LOG= Logger.getLogger("Range");
private long _ptr;
Range(final long ptr)
{
this._ptr = ptr;
}
public long getMin() {return ComediJNI.range_info_min(_ptr);}
public long getMax() {return ComediJNI.range_info_max(_ptr);}
@Override
public String toString() { return ""+getMin()+" "+getMax();}
}
package com.github.lindenb.mycomedi; package com.github.lindenb.mycomedi;
import com.github.lindenb.mycomedi.jni.ComediJNI; import com.github.lindenb.mycomedi.ComediJNI;
import java.util.logging.Logger; import java.util.logging.Logger;
public class Test { public class Test {
...@@ -8,8 +8,21 @@ private static final Logger LOG= Logger.getLogger("Test"); ...@@ -8,8 +8,21 @@ private static final Logger LOG= Logger.getLogger("Test");
public static void main(final String[] args) { public static void main(final String[] args) {
ComediJNI.init(); ComediJNI.init();
int subdev = 0; /* change this to your input subdevice */
int chan = 0; /* change this to your channel */
int range = 0; /* more on this later */
final int aref = ComediJNI.AREF_GROUND(); /* more on this later */
Device device = new Device(); Device device = new Device();
device.readData(subdev,chan,range,aref);
ComediJNI.comedi_set_global_oor_behavior(ComediJNI.COMEDI_OOR_NAN());
Range range_info = device.getRange(subdev, chan, range);
LOG.info(""+range_info);
long maxdata = device.getMaxData(subdev, chan);
LOG.info("read data");
device.close(); device.close();
} }
} }
...@@ -4,5 +4,5 @@ JAVA_HOME?=/home/pci-6221/packages/jdk1.8.0_131 ...@@ -4,5 +4,5 @@ JAVA_HOME?=/home/pci-6221/packages/jdk1.8.0_131
libmycomedi.so: mycomedi.o libmycomedi.so: mycomedi.o
$(CC) $(CFLAGS) -W -shared $< $(LIBS) -o $@ $(CC) $(CFLAGS) -W -shared $< $(LIBS) -o $@
mycomedi.o : mycomedi.c mycomedi.o : mycomedi.c com_github_lindenb_mycomedi_ComediJNI.h
$(CC) $(CFLAGS) -o $@ -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -c $< $(LIBS) $(CC) $(CFLAGS) -o $@ -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -c $< $(LIBS)
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_github_lindenb_mycomedi_ComediJNI */
#ifndef _Included_com_github_lindenb_mycomedi_ComediJNI
#define _Included_com_github_lindenb_mycomedi_ComediJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: AREF_GROUND
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_AREF_1GROUND
(JNIEnv *, jclass);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: COMEDI_OOR_NAN
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_COMEDI_1OOR_1NAN
(JNIEnv *, jclass);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_open
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1open
(JNIEnv *, jclass, jstring);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_close
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1close
(JNIEnv *, jclass, jlong);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_perror
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1perror
(JNIEnv *, jclass, jstring);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_data_read
* Signature: (JIIII)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1data_1read
(JNIEnv *, jclass, jlong, jint, jint, jint, jint);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_set_global_oor_behavior
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1set_1global_1oor_1behavior
(JNIEnv *, jclass, jint);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_get_range
* Signature: (JIII)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1get_1range
(JNIEnv *, jclass, jlong, jint, jint, jint);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: comedi_get_maxdata
* Signature: (JII)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1get_1maxdata
(JNIEnv *, jclass, jlong, jint, jint);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: range_info_min
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_range_1info_1min
(JNIEnv *, jclass, jlong);
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: range_info_max
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_range_1info_1max
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}
#endif
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_github_lindenb_mycomedi_jni_ComediJNI */
#ifndef _Included_com_github_lindenb_mycomedi_jni_ComediJNI
#define _Included_com_github_lindenb_mycomedi_jni_ComediJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_github_lindenb_mycomedi_jni_ComediJNI
* Method: comedi_open
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1open
(JNIEnv *, jclass, jstring);
/*
* Class: com_github_lindenb_mycomedi_jni_ComediJNI
* Method: comedi_close
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1close
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}
#endif
#endif
...@@ -2,11 +2,38 @@ ...@@ -2,11 +2,38 @@
#include <stdlib.h> #include <stdlib.h>
#include <comedilib.h> #include <comedilib.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h>
#include <math.h> #include <math.h>
#include "com_github_lindenb_mycomedi_jni_ComediJNI.h" #include "com_github_lindenb_mycomedi_ComediJNI.h"
#define MSG_SIZE 1024
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1open static jint throwException(JNIEnv* pEnv, const char* szClassName, const char* szFmt, va_list va_args) {
char szMsg[MSG_SIZE];
vsnprintf(szMsg, MSG_SIZE, szFmt, va_args);
jclass exClass = (*pEnv)->FindClass(pEnv,szClassName);
return (*pEnv)->ThrowNew(pEnv,exClass, szMsg);
}
static jint throwIllegalArgumentException(JNIEnv* pEnv, const char* szFmt, ...) {
va_list va_args;
va_start(va_args, szFmt);
jint ret = throwException(pEnv, "java/lang/IllegalArgumentException", szFmt, va_args);
va_end(va_args);
return ret;
}
JNIEXPORT jint JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_AREF_1GROUND(JNIEnv * env, jclass clazz)
{
return (jint)AREF_GROUND;
}
JNIEXPORT jint JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_COMEDI_1OOR_1NAN(JNIEnv *env, jclass clazz)
{
return (jint)COMEDI_OOR_NAN;
}
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1open
(JNIEnv* env, jclass clazz , jstring deviceName) (JNIEnv* env, jclass clazz , jstring deviceName)
{ {
comedi_t *device; comedi_t *device;
...@@ -22,7 +49,7 @@ JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1o ...@@ -22,7 +49,7 @@ JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1o
JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1close JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1close
(JNIEnv* env, jclass clazz, jlong ptr) (JNIEnv* env, jclass clazz, jlong ptr)
{ {
if(ptr==0L) return; if(ptr==0L) return;
...@@ -30,3 +57,116 @@ JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1cl ...@@ -30,3 +57,116 @@ JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_jni_ComediJNI_comedi_1cl
comedi_close(device); comedi_close(device);
} }
JNIEXPORT void JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1perror
(JNIEnv *env, jclass clazz, jstring msg)
{
if( msg == NULL) return;
const char *message = (*env)->GetStringUTFChars(env, msg, 0);
comedi_perror(message);
(*env)->ReleaseStringUTFChars(env,msg, message);
}
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1data_1read
(JNIEnv * env, jclass clazz, jlong device_ptr, jint subdev, jint channel, jint range, jint aref)
{
lsampl_t data;
int retval;
if(device_ptr<=0L)
{
throwIllegalArgumentException(env,"device is null");
return 0L;
}
comedi_t *device=(comedi_t*)device_ptr;
retval = comedi_data_read(device, subdev, channel, range, aref,&data);
if( retval <0 )
{
throwIllegalArgumentException(env," comedi_data_read failed");
return 0L;
}
assert(sizeof(lsampl_t)<=sizeof(jlong));
return (jlong)data;
}
JNIEXPORT jint JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1set_1global_1oor_1behavior
(JNIEnv * env, jclass clazz, jint value)
{
return comedi_set_global_oor_behavior(value);
}
/*
JNIEXPORT jbyteArray JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1get_1range
(JNIEnv * env, jclass clazz,jlong device_ptr, jint subdev, jint chan, jint range)
{
jbyteArray result;
comedi_range* crange;
if(device_ptr<=0L)
{
throwIllegalArgumentException(env,"device is null");
return 0L;
}
crange = comedi_get_range((comedi_t*)device_ptr, subdev, chan, range);
result=(*env)->NewByteArray(env, sizeof(comedi_range));
(*env)->SetByteArrayRegion(env, result, 0, sizeof(comedi_range), (jbyte*)crange);
return result;
}*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1get_1range
(JNIEnv * env, jclass clazz,jlong device_ptr, jint subdev, jint chan, jint range)
{
comedi_range* crange;
if(device_ptr<=0L)
{
throwIllegalArgumentException(env,"device is null");
return 0L;
}
crange = comedi_get_range((comedi_t*)device_ptr, subdev, chan, range);
return (jlong)crange;
}
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_comedi_1get_1maxdata
(JNIEnv * env, jclass clazz,jlong device_ptr, jint subdev, jint chan)
{
if(device_ptr<=0L)
{
throwIllegalArgumentException(env,"device is null");
return 0L;
}
assert(sizeof(lsampl_t)<=sizeof(jlong));
return (long) comedi_get_maxdata((comedi_t*)device_ptr,subdev,chan);
}
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: range_info_min
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_range_1info_1min
(JNIEnv *env, jclass clazz, jlong ptr)
{
return ((comedi_range*)ptr)->min;
}
/*
* Class: com_github_lindenb_mycomedi_ComediJNI
* Method: range_info_max
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_lindenb_mycomedi_ComediJNI_range_1info_1max
(JNIEnv *env, jclass clazz, jlong ptr)
{
return ((comedi_range*)ptr)->max;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter