Skip to content
Snippets Groups Projects
Commit 3bc988de authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
name = "MobergIO"
uuid = "9bdc2bb6-e40d-4944-bd5f-2bf890d3f651"
authors = ["Anders Blomdell <anders.blomdell@control.lth.se>"]
version = "0.9.15"
mutable struct AnalogInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
mutable struct AnalogIn
moberg::Ptr{Nothing}
index::UInt32
channel::AnalogInChannel
function AnalogIn(moberg::Moberg, index::Unsigned)
channel = AnalogInChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_analog_in_open, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, Ref{AnalogInChannel}),
moberg_handle, index, channel))
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(ain::AnalogIn)
DEBUG && println("closing $(ain)")
checkOK(ccall((:moberg_analog_in_close, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, AnalogInChannel),
ain.moberg, ain.index, ain.channel))
end
function read(ain::AnalogIn)
result = Ref{Cdouble}(0.0)
checkOK(ccall(ain.channel.read,
Status,
(Ptr{Nothing}, Ptr{Cdouble}),
ain.channel.context, result))
return result[]
end
mutable struct AnalogOutChannel
context::Ptr{Nothing}
write::Ptr{Nothing}
end
mutable struct AnalogOut
moberg::Ptr{Nothing}
index::UInt32
channel::AnalogOutChannel
function AnalogOut(moberg::Moberg, index::Unsigned)
channel = AnalogOutChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_analog_out_open, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, Ref{AnalogOutChannel}),
moberg_handle, index, channel));
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(aout::AnalogOut)
DEBUG && println("closing $(aout)")
checkOK(ccall((:moberg_analog_out_close, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, AnalogOutChannel),
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, Ptr{Cdouble}),
aout.channel.context, value, result))
return result[];
end
mutable struct DigitalInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
mutable struct DigitalIn
moberg::Ptr{Nothing}
index::UInt32
channel::DigitalInChannel
function DigitalIn(moberg::Moberg, index::Unsigned)
channel = DigitalInChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_digital_in_open, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, Ref{DigitalInChannel}),
moberg_handle, index, channel));
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(din::DigitalIn)
DEBUG && println("closing $(din)")
checkOK(ccall((:moberg_digital_in_close, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, DigitalInChannel),
din.moberg, din.index, din.channel))
end
function read(din::DigitalIn)
result = Ref{Cint}(0)
checkOK(ccall(din.channel.read,
Status,
(Ptr{Nothing}, Ptr{Cint}),
din.channel.context, result))
return result[] != 0
end
mutable struct DigitalOutChannel
context::Ptr{Nothing}
write::Ptr{Nothing}
end
mutable struct DigitalOut
moberg::Ptr{Nothing}
index::UInt32
channel::DigitalOutChannel
function DigitalOut(moberg::Moberg, index::Unsigned)
channel = DigitalOutChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_digital_out_open, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, Ref{DigitalOutChannel}),
moberg_handle, index, channel))
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(dout::DigitalOut)
DEBUG && println("closing $(dout)")
checkOK(ccall((:moberg_digital_out_close, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, DigitalOutChannel),
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, Ptr{Cint}),
dout.channel.context, value ? 1 : 0, result))
return result[] != 0
end
mutable struct EncoderInChannel
context::Ptr{Nothing}
read::Ptr{Nothing}
end
mutable struct EncoderIn
moberg::Ptr{Nothing}
index::UInt32
channel::EncoderInChannel
function EncoderIn(moberg::Moberg, index::Unsigned)
channel = EncoderInChannel(0,0)
moberg_handle = moberg.handle
checkOK(ccall((:moberg_encoder_in_open, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, Ref{EncoderInChannel}),
moberg_handle, index, channel))
self = new(moberg_handle, index, channel)
finalizer(close, self)
self
end
end
function close(ein::EncoderIn)
DEBUG && println("closing $(ein)")
checkOK(ccall((:moberg_encoder_in_close, "libmoberg"),
Status,
(Ptr{Nothing}, Cint, EncoderInChannel),
ein.moberg, ein.index, ein.channel))
end
function read(ein::EncoderIn)
result = Ref{Clong}(0)
checkOK(ccall(ein.channel.read,
Status,
(Ptr{Nothing}, Ptr{Clong}),
ein.channel.context, result))
return result[]
end
module MobergIO
const DEBUG = false
struct Status
result::Clong
end
function checkOK(status::Status)
if status.result != 0
error("Moberg call failed with errno $(status.result)")
end
end
mutable struct Moberg
handle::Ptr{Nothing}
end
function Moberg()
handle = ccall((:moberg_new, "libmoberg"), Ptr{Moberg}, ())
m = Moberg(handle)
finalizer(close, m)
m
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")
include("AnalogOut.jl")
include("DigitalIn.jl")
include("DigitalOut.jl")
include("EncoderIn.jl")
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment