Commit 319a5d89 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Finally putting this under version control.

parents
*~
Comedi/build/
Regler/build/
Time/build/
UnixSocket/build/
Comedi support for java
\ No newline at end of file
%define BASENAME se.lth.control.realtime.Comedi
%define INSTALL_DIR /opt/java/%{BASENAME}
Name: java_%{BASENAME}
Requires: java_%{BASENAME}-common
Version: 1.0
Release: 6%{?dist}
License: GPL
Summary: Comedi support for java
URL: http://www.control.lth.se/user/andersb/software/java/Comedi
Source0: java_%{BASENAME}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: java-devel comedilib-devel
Requires: java comedilib
%description
Comedi support for java
%package common
BuildArch: noarch
Summary: Comedi support for java [noarch]
%description common
Comedi support for java [noarch]
%prep
%setup -q
%build
make -f src/Makefile all
%install
rm -rf $RPM_BUILD_ROOT
make -f src/Makefile INSTALL_DIR=${RPM_BUILD_ROOT}/%{INSTALL_DIR} install_src
make -f src/Makefile INSTALL_DIR=${RPM_BUILD_ROOT}/%{INSTALL_DIR} install_jar
make -f src/Makefile INSTALL_DIR=${RPM_BUILD_ROOT}/%{INSTALL_DIR} install_so
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
%{INSTALL_DIR}/jre/lib
%exclude %{INSTALL_DIR}/jre/lib/ext
%files common
%defattr(-,root,root,-)
%{INSTALL_DIR}/README
%{INSTALL_DIR}/jre/lib/ext
%{INSTALL_DIR}/src
VERSION=1.0
JARNAME=Comedi
NAME=java_se.lth.control.realtime.Comedi
TARFILE=$(NAME)-$(VERSION).tar.gz
JNINAME=se_lth_control_realtime_comedi_Comedi
JAVAH_PATH=$(shell javah -Xbootclasspath/p:build -version >/dev/null 2>&1 && \
echo -Xbootclasspath/p:build || echo -bootclasspath build)
JNI_INCLUDE=$(shell dirname $$(readlink -f $$(which java)))/../../include
CC=gcc
CC_JNI_FLAGS=-Wall -Werror -shared -fPIC \
-I$(JNI_INCLUDE) -I$(JNI_INCLUDE)/linux -Ibuild \
-lcomedi
JAVADIR=/tmp
INSTALL_DIR=$(JAVADIR)/$(JARNAME)
JAR_DIR=$(INSTALL_DIR)/jre/lib/ext/
SO_DIR=$(INSTALL_DIR)/jre/lib/$(shell /bin/sh ./src/getProperty_os_arch)
SRCFILES=README \
src/Makefile \
src/getProperty_os_arch \
$(JNINAME:%=src/%.c) \
$(JAVAFILES:%=src/%)
JAVAFILES=$(shell find src -name '*.java' | sed -e 's:^src/::g')
all: JAVAC build/$(JARNAME).jar $(JNINAME:%=build/lib%.so)
install: install_src install_jar install_so
install_src:
for f in $(SRCFILES) ; do install -D $$f $(INSTALL_DIR)/$$f ; done
install_jar: build/$(JARNAME).jar
install -d $(JAR_DIR)
install build/$(JARNAME).jar $(JAR_DIR)
install_so: $(JNINAME:%=install_%.so)
install_%.so: build/lib%.so
install -d $(SO_DIR)
install $< $(SO_DIR)
JAVAC: $(JAVAFILES:%.java=build/%.class)
build/%.class: src/%.java
# Recompile all javafiles at once (do JIT compilation once)
mkdir -p build
cd src ; javac -d ../build -target 1.5 -source 1.5 $(JAVAFILES)
build/$(JARNAME).jar: JAVAC
jar -cf $@ \
-C build se \
$(SRCFILES)
build/%.h: $(shell echo $(JNINAME:%=build/%.class) | sed -e 's:_:/:g')
# Too many dependencies, base the ones we need on $* (matches % above)
javah -o $@ -force $(JAVAH_PATH) $(shell echo $* | sed -e s/_/./g)
build/lib%.so: $(JNINAME:%=build/%.h) $(JNINAME:%=src/%.c)
# Too many dependencies, base the ones we need on $* (matches % above)
$(CC) -o $@ $(CC_JNI_FLAGS) -I. build/$*.h src/$*.c
.PHONY: clean
clean:
rm -rf build
.PHONY: TAR
TAR:
@mkdir -p build/BUILD
@mkdir -p build/SPECS
@mkdir -p build/RPMS
@mkdir -p build/SRPMS
@mkdir -p build/SOURCES
@tar -C .. -czf build/SOURCES/$(TARFILE) \
$(SRCFILES:%=$(shell basename $$(pwd))/%)
.PHONY: SRPM
SRPM: TAR
@rpmbuild --define "_topdir $(shell pwd)/build" \
-bs $(NAME).spec 2>&1
#!/bin/sh
BASENAME=`basename $0`
cat > ${BASENAME}.java << EOF
public class ${BASENAME} {
public static void main(String[] args) {
System.out.println(System.getProperty("os.arch"));
}
}
EOF
javac ${BASENAME}.java
java ${BASENAME}
rm ${BASENAME}.java ${BASENAME}.class
\ No newline at end of file
/**
* se.lth.control.realtime.comedi.AlreadyAllocatedException.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
public class AlreadyAllocatedException extends IOChannelException {
private static final long serialVersionUID = 1L;
public AlreadyAllocatedException(String kind, int index) {
super(kind, index, "is already allocated");
}
}
/**
* se.lth.control.realtime.AnalogIn.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import se.lth.control.realtime.comedi.Comedi;
public class AnalogIn extends IOChannel {
public AnalogIn(final int index) throws IOChannelException {
super(index);
}
protected void open() throws IOChannelException {
Comedi.analogInOpen(index);
}
protected void close() throws IOChannelException {
Comedi.analogInClose(index);
}
public double get() throws IOChannelException {
return Comedi.analogIn(index);
}
}
/**
* se.lth.control.realtime.AnalogOut.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.comedi.Comedi;
public class AnalogOut extends IOChannel {
public AnalogOut(int index) throws IOChannelException {
super(index);
}
public void open() throws IOChannelException {
Comedi.analogOutOpen(index);
}
public void close() throws IOChannelException {
Comedi.analogOutClose(index);
}
public void set(double value) throws IOChannelException {
Comedi.analogOut(index, value);
}
}
/**
* se.lth.control.realtime.DigitalIn.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.comedi.Comedi;
public class DigitalIn extends IOChannel {
public DigitalIn(int index) throws IOChannelException {
super(index);
}
public void open() throws IOChannelException {
Comedi.digitalInOpen(index);
}
public void close() throws IOChannelException {
Comedi.digitalInClose(index);
}
public boolean get() throws IOChannelException {
return Comedi.digitalIn(index);
}
}
/**
* se.lth.control.realtime.DigitalOut.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.comedi.Comedi;
public class DigitalOut extends IOChannel {
public DigitalOut(int index) throws IOChannelException {
super(index);
}
public void open() throws IOChannelException {
Comedi.digitalOutOpen(index);
}
public void close() throws IOChannelException {
Comedi.digitalOutClose(index);
}
public void set(boolean value) throws IOChannelException {
Comedi.digitalOut(index, value);
}
}
/**
* se.lth.control.realtime.EncoderIn.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.comedi.Comedi;
public class EncoderIn extends IOChannel {
public EncoderIn(int index) throws IOChannelException {
super(index);
}
protected void open() throws IOChannelException {
Comedi.encoderInOpen(index);
}
protected void close() throws IOChannelException {
Comedi.encoderInClose(index);
}
public long get() throws IOChannelException {
return Comedi.encoderIn(index);
}
}
/**
* se.lth.control.realtime.comedi.IOChannel.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
* Copyright (C) 2014 Alfred Theorin <alfred.theorin@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.HashMap;
import java.util.BitSet;
public abstract class IOChannel {
private static HashMap allocations = new HashMap();
public final int index;
private BitSet allocation;
private boolean isAllocated = false;
public IOChannel(int index) throws IOChannelException {
this.index = index;
open();
allocate();
}
private BitSet getAllocation() {
BitSet result = allocation;
if (result == null) {
result = (BitSet)allocations.get(this.getClass().getName());
if (result == null) {
result = new BitSet();
allocations.put(this.getClass().getName(), result);
}
}
return result;
}
private synchronized void allocate() throws IOChannelException {
if (getAllocation().get(index)) {
// Try to free unreferenced channels
System.gc();
System.runFinalization();
}
if (getAllocation().get(index)) {
try {
close();
} catch (IOChannelException e) {
}
throw new AlreadyAllocatedException(this.getClass().getName(), index);
}
getAllocation().set(index);
isAllocated = true;
}
private synchronized void deallocate() {
if (isAllocated) {
getAllocation().clear(index);
try {
close();
} catch (IOChannelException e) {
}
isAllocated = false;
}
}
protected abstract void open() throws IOChannelException;
protected abstract void close() throws IOChannelException;
protected void finalize() throws IOChannelException {
deallocate();
}
}
/**
* se.lth.control.realtime.IOChannelException.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.io.IOException;
public class IOChannelException extends IOException {
private static final long serialVersionUID = 1L;
public IOChannelException(String kind, int index, String s) {
super(kind + " channel #" + index + " " + s);
}
}
/**
* se.lth.control.realtime.comedi.Comedi.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime.comedi;
import java.io.File;
public class Comedi {
public static native double analogIn(int channel) throws ComediException;
public static native void analogOut(int channel, double value) throws ComediException;
public static native boolean digitalIn(int channel) throws ComediException;
public static native void digitalOut(int channel, boolean value) throws ComediException;
public static native long encoderIn(int channel) throws ComediException;
public static native void analogInOpen(int channel) throws ComediException;
public static native void analogOutOpen(int channel) throws ComediException;
public static native void digitalInOpen(int channel) throws ComediException;
public static native void digitalOutOpen(int channel) throws ComediException;
public static native void encoderInOpen(int channel) throws ComediException;
public static native void analogInClose(int channel) throws ComediException;
public static native void analogOutClose(int channel) throws ComediException;
public static native void digitalInClose(int channel) throws ComediException;
public static native void digitalOutClose(int channel) throws ComediException;
public static native void encoderInClose(int channel) throws ComediException;
private static native void Init();
static {
System.loadLibrary("se_lth_control_realtime_comedi_Comedi");
Init();
}
}
<
/**
* se.lth.control.realtime.comedi.ComediConfigurationFileNotFoundError.java
*
* Copyright (C) 2005,2012 Anders Blomdell <anders.blomdell@control.lth.se>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime.comedi;