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

Style changes

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