Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • anders_blomdell/moberg
  • cont-frb/moberg
  • mattiasf/moberg
3 results
Select Git revision
Show changes
Commits on Source (48)
Showing
with 269 additions and 64 deletions
LIBRARIES=libmoberg.so
MOBERG_VERSION=$(shell git describe --tags | sed -e 's/^v//;s/-/_/g' )
MOBERG_VERSION=$(shell git describe --tags \
| sed -re 's/^v//;s/-(.*)-(.*)$$/.dev\1+\2/')
CCFLAGS+=-Wall -Werror -I$(shell pwd) -O3 -g
LDFLAGS+=-L$(shell pwd)/build/ -lmoberg
PLUGINS:=$(wildcard plugins/*)
ADAPTORS:=$(wildcard adaptors/*)
PLUGINS:=$(sort $(wildcard plugins/*))
ADAPTORS:=$(sort $(wildcard adaptors/*))
export CCFLAGS LDFLAGS
LDFLAGS_parse_config=-ldl
#-export-dynamic
......@@ -23,7 +24,7 @@ build/lib build:
mkdir -p $@
%: build/%.o Makefile
$(CC) $(LDFLAGS) $(LDFLAGS_$(*)) -o $@ $(filter %.o,$^)
$(CC) -o $@ $(filter %.o,$^) $(LDFLAGS) $(LDFLAGS_$(*))
build/%.o: %.c Makefile
$(CC) $(CCFLAGS) -c -o $@ $<
......@@ -62,9 +63,7 @@ test: all
$(MAKE) -C test test
clean:
rm -f build/*.so
rm -f build/*.mex*
rm -f build/lib/*.o
rm -rf build/
rm -f *~
rm -f moberg-*.spec
rm -f moberg-*.tar.gz
......
*.class
......@@ -3,9 +3,10 @@ JARNAME=Moberg
TARFILE=java_se.lth.control.realtime.Moberg-$(VERSION).tar.gz
JNINAME=se_lth_control_realtime_moberg_Moberg
JAVAH_PATH=$(shell javah -Xbootclasspath/p:build -version >/dev/null 2>&1 && \
echo -Xbootclasspath/p:build || echo -bootclasspath build)
JNI_INCLUDE=/usr/lib/jvm/java/include/
JAVA_HOME=$(shell realpath /usr/bin/javac | sed 's|/bin/javac||')
JNI_INCLUDE=$(JAVA_HOME)/include/
JNI_INCLUDE=$(JAVA_HOME)/include/
CC=gcc
CC_JNI_FLAGS=-Wall -Werror -shared -fPIC \
-I$(JNI_INCLUDE) -I$(JNI_INCLUDE)/linux \
......@@ -44,7 +45,7 @@ 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)
cd src ; javac -d ../build $(JAVAFILES)
build/$(JARNAME).jar: JAVAC
jar -cf $@ \
......@@ -53,7 +54,8 @@ build/$(JARNAME).jar: JAVAC
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)
cd src ; javac -h ../$(dir $@) -d ../build \
$(shell echo $* | sed -e 's|_|/|g').java
build/lib%.so: $(JNINAME:%=build/%.h) $(JNINAME:%=src/%.c)
# Too many dependencies, base the ones we need on $* (matches % above)
......
......@@ -35,8 +35,13 @@ public class AnalogIn extends IOChannel {
Moberg.analogInClose(index);
}
@Deprecated
public double get() throws IOChannelException {
return Moberg.analogIn(index);
}
public double read() throws IOChannelException {
return Moberg.analogIn(index);
}
}
......@@ -39,8 +39,13 @@ public class AnalogOut extends IOChannel {
Moberg.analogOutClose(index);
}
@Deprecated
public void set(double value) throws IOChannelException {
Moberg.analogOut(index, value);
}
public double write(double value) throws IOChannelException {
return Moberg.analogOut(index, value);
}
}
......@@ -37,8 +37,13 @@ public class DigitalIn extends IOChannel {
Moberg.digitalInClose(index);
}
@Deprecated
public boolean get() throws IOChannelException {
return Moberg.digitalIn(index);
}
public boolean read() throws IOChannelException {
return Moberg.digitalIn(index);
}
}
......@@ -36,8 +36,13 @@ public class DigitalOut extends IOChannel {
Moberg.digitalOutClose(index);
}
@Deprecated
public void set(boolean value) throws IOChannelException {
Moberg.digitalOut(index, value);
}
public boolean write(boolean value) throws IOChannelException {
return Moberg.digitalOut(index, value);
}
}
......@@ -37,8 +37,13 @@ public class EncoderIn extends IOChannel {
Moberg.encoderInClose(index);
}
@Deprecated
public long get() throws IOChannelException {
return Moberg.encoderIn(index);
}
public long read() throws IOChannelException {
return Moberg.encoderIn(index);
}
}
......@@ -25,7 +25,8 @@ import java.util.BitSet;
public abstract class IOChannel {
private static HashMap allocations = new HashMap();
private static class AllocationMap extends HashMap<String, BitSet> {}
private static AllocationMap allocations = new AllocationMap();
public final int index;
private BitSet allocation;
......
......@@ -28,7 +28,7 @@ public class Moberg {
public static native void analogOutOpen(int index) throws MobergException;
public static native void analogOutClose(int index) throws MobergException;
public static native void analogOut(int index, double value) throws MobergException;
public static native double analogOut(int index, double value) throws MobergException;
public static native void digitalInOpen(int index) throws MobergException;
public static native void digitalInClose(int index) throws MobergException;
......@@ -36,7 +36,7 @@ public class Moberg {
public static native void digitalOutOpen(int index) throws MobergException;
public static native void digitalOutClose(int index) throws MobergException;
public static native void digitalOut(int index, boolean value) throws MobergException;
public static native boolean digitalOut(int index, boolean value) throws MobergException;
public static native void encoderInOpen(int index) throws MobergException;
public static native void encoderInClose(int index) throws MobergException;
......
......@@ -113,7 +113,7 @@ struct {
static int up()
{
if (g_moberg.count <= 0) {
g_moberg.moberg = moberg_new(NULL);
g_moberg.moberg = moberg_new();
}
g_moberg.count++;
return 0;
......@@ -261,16 +261,19 @@ Java_se_lth_control_realtime_moberg_Moberg_analogOutClose(
}
JNIEXPORT void JNICALL
JNIEXPORT double JNICALL
Java_se_lth_control_realtime_moberg_Moberg_analogOut(
JNIEnv *env, jobject obj, jint index, jdouble value
JNIEnv *env, jobject obj, jint index, jdouble desired
)
{
struct channel *channel = channel_get(&analog_out, index);
if (! channel) {
throwMobergNotOpenException(env, index);
return 0.0;
} else {
channel->analog_out.write(channel->analog_out.context, value);
double actual;
channel->analog_out.write(channel->analog_out.context, desired, &actual);
return actual;
}
}
......@@ -354,16 +357,19 @@ Java_se_lth_control_realtime_moberg_Moberg_digitalOutClose(
}
}
JNIEXPORT void JNICALL
JNIEXPORT jboolean JNICALL
Java_se_lth_control_realtime_moberg_Moberg_digitalOut(
JNIEnv *env, jobject obj, jint index, jboolean value
JNIEnv *env, jobject obj, jint index, jboolean desired
)
{
struct channel *channel = channel_get(&digital_out, index);
if (! channel) {
throwMobergNotOpenException(env, index);
return 0;
} else {
channel->digital_out.write(channel->digital_out.context, value);
int actual;
channel->digital_out.write(channel->digital_out.context, desired, &actual);
return actual;
}
}
......
MobergIO.jl is licensed under the MIT License:
> Copyright (c) 2021: Anders Blomdell
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
name = "MobergIO"
uuid = "9bdc2bb6-e40d-4944-bd5f-2bf890d3f651"
authors = ["Anders Blomdell <anders.blomdell@control.lth.se>"]
version = "1.0.0"
[compat]
julia = "1"
[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[targets]
test = ["Test"]
# MobergIO.jl
A library for connecting to various input/output libraries with a common interface.
You need a local configuration and installation of Moberg for this package to work properly, see: [github.com/ControlLTH/moberg](https://github.com/ControlLTH/moberg).
mutable struct AnalogInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
mutable struct AnalogIn
moberg::Moberg
mutable struct AnalogIn <: AbstractMobergIn
moberg::Ptr{Nothing}
index::UInt32
channel::AnalogInChannel
channel::MobergInChannel
function AnalogIn(moberg::Moberg, index::Unsigned)
channel = AnalogInChannel(0,0)
channel = MobergInChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_analog_in_open, "libmoberg"),
Status,
(Moberg, Cint, Ref{AnalogInChannel}),
moberg, index, channel))
self = new(moberg, index, channel)
(Ptr{Nothing}, Cint, Ref{MobergInChannel}),
moberg_handle, index, channel))
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(ain::AnalogIn)
# println("closing $(ain)")
DEBUG && println("closing $(ain)")
checkOK(ccall((:moberg_analog_in_close, "libmoberg"),
Status,
(Moberg, Cint, AnalogInChannel),
(Ptr{Nothing}, Cint, MobergInChannel),
ain.moberg, ain.index, ain.channel))
end
......
mutable struct AnalogOutChannel
context::Ptr{Nothing}
write::Ptr{Nothing}
end
mutable struct AnalogOut
moberg::Moberg
mutable struct AnalogOut <: AbstractMobergOut
moberg::Ptr{Nothing}
index::UInt32
channel::AnalogOutChannel
channel::MobergOutChannel
function AnalogOut(moberg::Moberg, index::Unsigned)
channel = AnalogOutChannel(0,0)
channel = MobergOutChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_analog_out_open, "libmoberg"),
Status,
(Moberg, Cint, Ref{AnalogOutChannel}),
moberg, index, channel));
self = new(moberg, index, channel)
(Ptr{Nothing}, Cint, Ref{MobergOutChannel}),
moberg_handle, index, channel));
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(aout::AnalogOut)
# println("closing $(aout)")
DEBUG && println("closing $(aout)")
checkOK(ccall((:moberg_analog_out_close, "libmoberg"),
Status,
(Moberg, Cint, AnalogOutChannel),
(Ptr{Nothing}, Cint, MobergOutChannel),
aout.moberg, aout.index, aout.channel))
end
function write(aout::AnalogOut, value::Cdouble)
result = Ref{Cdouble}(0.0)
checkOK(ccall(aout.channel.write,
Status,
(Ptr{Nothing}, Cdouble),
aout.channel.context, value))
(Ptr{Nothing}, Cdouble, Ptr{Cdouble}),
aout.channel.context, value, result))
return result[];
end
mutable struct DigitalInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
mutable struct DigitalIn
moberg::Moberg
mutable struct DigitalIn <: AbstractMobergIn
moberg::Ptr{Nothing}
index::UInt32
channel::DigitalInChannel
channel::MobergInChannel
function DigitalIn(moberg::Moberg, index::Unsigned)
channel = DigitalInChannel(0,0)
channel = MobergInChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_digital_in_open, "libmoberg"),
Status,
(Moberg, Cint, Ref{DigitalInChannel}),
moberg, index, channel));
self = new(moberg, index, channel)
(Ptr{Nothing}, Cint, Ref{MobergInChannel}),
moberg_handle, index, channel));
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(din::DigitalIn)
# println("closing $(din)")
DEBUG && println("closing $(din)")
checkOK(ccall((:moberg_digital_in_close, "libmoberg"),
Status,
(Moberg, Cint, DigitalInChannel),
(Ptr{Nothing}, Cint, MobergInChannel),
din.moberg, din.index, din.channel))
end
......
mutable struct DigitalOutChannel
context::Ptr{Nothing}
write::Ptr{Nothing}
end
mutable struct DigitalOut
moberg::Moberg
mutable struct DigitalOut <: AbstractMobergOut
moberg::Ptr{Nothing}
index::UInt32
channel::DigitalOutChannel
channel::MobergOutChannel
function DigitalOut(moberg::Moberg, index::Unsigned)
channel = DigitalOutChannel(0,0)
channel = MobergOutChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_digital_out_open, "libmoberg"),
Status,
(Moberg, Cint, Ref{DigitalOutChannel}),
moberg, index, channel))
self = new(moberg, index, channel)
(Ptr{Nothing}, Cint, Ref{MobergOutChannel}),
moberg_handle, index, channel))
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(dout::DigitalOut)
# println("closing $(dout)")
DEBUG && println("closing $(dout)")
checkOK(ccall((:moberg_digital_out_close, "libmoberg"),
Status,
(Moberg, Cint, DigitalOutChannel),
(Ptr{Nothing}, Cint, MobergOutChannel),
dout.moberg, dout.index, dout.channel))
end
function write(dout::DigitalOut, value::Bool)
result = Ref{Cint}(0)
checkOK(ccall(dout.channel.write,
Status,
(Ptr{Nothing}, Cint),
dout.channel.context, value ? 1 : 0))
(Ptr{Nothing}, Cint, Ptr{Cint}),
dout.channel.context, value ? 1 : 0, result))
return result[] != 0
end
mutable struct EncoderInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
"""
encoder_in = EncoderIn(moberg::Moberg, index::Unsigned)
Example usage:
mutable struct EncoderIn
moberg::Moberg
encoder_in = EncoderIn(m, UInt32(40))
result = read(encoder_in)
"""
mutable struct EncoderIn <: AbstractMobergIn
moberg::Ptr{Nothing}
index::UInt32
channel::EncoderInChannel
channel::MobergInChannel
function EncoderIn(moberg::Moberg, index::Unsigned)
channel = EncoderInChannel(0,0)
channel = MobergInChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_encoder_in_open, "libmoberg"),
Status,
(Moberg, Cint, Ref{EncoderInChannel}),
moberg, index, channel))
self = new(moberg, index, channel)
(Ptr{Nothing}, Cint, Ref{MobergInChannel}),
moberg_handle, index, channel))
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(ein::EncoderIn)
# println("closing $(ein)")
DEBUG && println("closing $(ein)")
checkOK(ccall((:moberg_encoder_in_close, "libmoberg"),
Status,
(Moberg, Cint, EncoderInChannel),
(Ptr{Nothing}, Cint, MobergInChannel),
ein.moberg, ein.index, ein.channel))
end
......
module MobergIO
export Moberg
import Base: close, read, write
abstract type AbstractMobergIO end
abstract type AbstractMobergIn <: AbstractMobergIO end
abstract type AbstractMobergOut <: AbstractMobergIO end
"""
close(io::AbstractMobergIO)
""" close
"""
result = read(io::AbstractMobergIn)
""" read
"""
write(io::AbstractMobergIn)
""" write
const DEBUG = false
struct Status
result::Clong
......@@ -11,6 +32,16 @@ function checkOK(status::Status)
end
end
mutable struct MobergOutChannel
context::Ptr{Nothing}
write::Ptr{Nothing}
end
mutable struct MobergInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
mutable struct Moberg
handle::Ptr{Nothing}
end
......@@ -23,7 +54,9 @@ function Moberg()
end
function close(h::Moberg)
DEBUG && println("Destroy $(h)")
ccall((:moberg_free, "libmoberg"), Nothing, (Moberg,), h)
h.handle = Ptr{Nothing}(0)
end
include("AnalogIn.jl")
......