Commit ee5e8457 authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Style changes

parent 00c1ec6c
#!/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")
......
......@@ -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")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment