diff --git a/adaptors/julia/moberg.jl b/adaptors/julia/moberg.jl index 176ded28d1edb313b6e48a2887b67ac264bb0711..3cb531780f0414d77322dbe98ed6d42b7d4c9269 100755 --- a/adaptors/julia/moberg.jl +++ b/adaptors/julia/moberg.jl @@ -1,102 +1,99 @@ #!/usr/bin/julia -struct moberg_status +struct MobergStatus result::Clong end -function check_OK(status::moberg_status) +function checkOK(status::MobergStatus) if status.result != 0 error("Moberg call failed with errno $(status.result)") end end -mutable struct moberg +mutable struct Moberg handle::Ptr{Nothing} end -function moberg() +function Moberg() handle = ccall((:moberg_new, "libmoberg"), Ptr{Nothing}, ()) println(handle) - m = moberg(handle) - function close(h::moberg) + m = Moberg(handle) + finalizer(m) do h println(h) ccall((:moberg_free, "libmoberg"), Nothing, (Ptr{Nothing},), h.handle) end - finalizer(close, m) m end -mutable struct moberg_analog_in +mutable struct MobergAnalogIn context::Ptr{Nothing} do_read::Ptr{Nothing} - handle::moberg - function moberg_analog_in(m::moberg, index::Unsigned) + handle::Moberg + function MobergAnalogIn(m::Moberg, index::Unsigned) self = new() - check_OK(ccall((:moberg_analog_in_open, "libmoberg"), - moberg_status, - (Ptr{Nothing}, Cint, Ref{moberg_analog_in}), + checkOK(ccall((:moberg_analog_in_open, "libmoberg"), + MobergStatus, + (Ptr{Nothing}, Cint, Ref{MobergAnalogIn}), m.handle, index, self)) self.handle = m - function close(channel::moberg_analog_in) + finalizer(self) do channel println(channel) ccall((:moberg_analog_in_close, "libmoberg"), - moberg_status, - (Ptr{Nothing}, Cint, moberg_analog_in), + MobergStatus, + (Ptr{Nothing}, Cint, MobergAnalogIn), channel.handle.handle, index, self) end - finalizer(close, self) self end end -mutable struct moberg_analog_out +mutable struct MobergAnalogOut context::Ptr{Nothing} do_write::Ptr{Nothing} - handle::moberg - function moberg_analog_out(m::moberg, index::Unsigned) + handle::Moberg + function MobergAnalogOut(m::Moberg, index::Unsigned) self = new() - check_OK(ccall((:moberg_analog_out_open, "libmoberg"), - moberg_status, - (Ptr{Nothing}, Cint, Ref{moberg_analog_out}), + checkOK(ccall((:moberg_analog_out_open, "libmoberg"), + MobergStatus, + (Ptr{Nothing}, Cint, Ref{MobergAnalogOut}), m.handle, index, self)) self.handle = m - function close(channel::moberg_analog_out) + finalizer(self) do channel println(channel) ccall((:moberg_analog_out_close, "libmoberg"), - moberg_status, - (Ptr{Nothing}, Cint, moberg_analog_out), + MobergStatus, + (Ptr{Nothing}, Cint, MobergAnalogOut), channel.handle.handle, index, self) end - finalizer(close, self) self end end -function read(ain::moberg_analog_in) +function read(ain::MobergAnalogIn) result = Ref{Cdouble}(0.0) - check_OK(ccall(ain.do_read, - moberg_status, + checkOK(ccall(ain.do_read, + MobergStatus, (Ptr{Nothing}, Ptr{Cdouble}), ain.context, result)) return result[] end -function write(aout::moberg_analog_out, value::Cdouble) - check_OK(ccall(aout.do_write, - moberg_status, +function write(aout::MobergAnalogOut, value::Cdouble) + checkOK(ccall(aout.do_write, + MobergStatus, (Ptr{Nothing}, Cdouble), aout.context, value)) end function test() - m = moberg() + m = Moberg() println(m) for v in -10.0:2.0:10 for i in 30:31 try - aout = moberg_analog_out(m, Unsigned(i)) + aout = MobergAnalogOut(m, Unsigned(i)) value = v + i - 32 write(aout, value) print("$value ") @@ -108,7 +105,7 @@ function test() sleep(0.1) for j in 30:33 try - ain = moberg_analog_in(m, Unsigned(j)) + ain = MobergAnalogIn(m, Unsigned(j)) println(read(ain)) catch println("analog_in $j does not exist") diff --git a/adaptors/julia/test_moberg.jl b/adaptors/julia/test_moberg.jl index 5e3ad8b3a633c2dc7c84b6a8004096551a3b9068..615c6cdca1b9381ea619ce4fea4cb9b1ef4d0d1d 100644 --- a/adaptors/julia/test_moberg.jl +++ b/adaptors/julia/test_moberg.jl @@ -63,4 +63,5 @@ test() println("DONE") GC.gc() +GC.gc() # See https://github.com/JuliaCI/BenchmarkTools.jl/blob/af35d0513fe1e336ad0d8b9a35f924e8461aefa2/src/execution.jl#L1 println("Really DONE")