moberg.jl 3.25 KB
Newer Older
Anders Blomdell's avatar
Anders Blomdell committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/julia

struct moberg_status
    result::Clong
end

function check_OK(status::moberg_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{Nothing}, ())
    println(handle)
    m = moberg(handle)
    function close(h::moberg)
        println(h)
        ccall((:moberg_free, "libmoberg"), Nothing, (Ptr{Nothing},),
              h.handle)
    end
    finalizer(close, m)
    m
end

mutable struct moberg_analog_in
    context::Ptr{Nothing}
    do_read::Ptr{Nothing}
    handle::moberg
    moberg_analog_in(m::moberg, index::Unsigned) = (
        self = new();
        check_OK(ccall((:moberg_analog_in_open, "libmoberg"),
                       moberg_status,
                       (Ptr{Nothing}, Cint, Ref{moberg_analog_in}),
                       m.handle, index, self));
        self.handle = m;
        function close(channel::moberg_analog_in)
            println(channel)
            ccall((:moberg_analog_in_close, "libmoberg"),
                  moberg_status,
                  (Ptr{Nothing}, Cint, moberg_analog_in),
                  channel.handle.handle, index, self)
        end;
        finalizer(close, self);
        self
    )
end

mutable struct moberg_analog_out
    context::Ptr{Nothing}
    do_write::Ptr{Nothing}
    handle::moberg
    moberg_analog_out(m::moberg, index::Unsigned) = (
        self = new();
        check_OK(ccall((:moberg_analog_out_open, "libmoberg"),
                       moberg_status,
                       (Ptr{Nothing}, Cint, Ref{moberg_analog_out}),
                       m.handle, index, self));
        self.handle = m;
        function close(channel::moberg_analog_out)
            println(channel)
            ccall((:moberg_analog_out_close, "libmoberg"),
                  moberg_status,
                  (Ptr{Nothing}, Cint, moberg_analog_out),
                  channel.handle.handle, index, self)
        end;
        finalizer(close, self);
        self
    )
end

function read(ain::moberg_analog_in)
    result = Ref{Cdouble}(0.0)
    check_OK(ccall(ain.do_read,
                   moberg_status,
                   (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,
                   (Ptr{Nothing}, Cdouble),
                   aout.context, value))
end

function test()
    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))
                value = v + i - 32;
                write(aout, value)
                print("$value ")
            catch
                println("analog_out $i does not exist")
            end
        end
        println()
        sleep(0.1)
        for j in 30:33
            try
                ain = moberg_analog_in(m, Unsigned(j))
                println(read(ain))
            catch
                println("analog_in $j does not exist")
            end
        end
        println()
        GC.gc()
    end
end

test()

println("DONE")
GC.gc()
println("Really DONE")