BeagleBone.jl 2.21 KB
Newer Older
Mattias Fält's avatar
Mattias Fält committed
1
2
3
4
# Devices should define a type `T` with methods:
# write!(::T, identifier, val)
# read(::T, identifier)

5
include("Debug.jl")
Mattias Fält's avatar
Mattias Fält committed
6
include("SysLED.jl")
7
include("GPIO.jl")
Marcus Greiff's avatar
Marcus Greiff committed
8
include("PWM.jl")
Mattias Fält's avatar
Mattias Fält committed
9
10

#List of available devices and their constructors
Marcus Greiff's avatar
Marcus Greiff committed
11
const DEVICES = Dict("debug" => Debug(), "sysled" => SysLED(), "gpio" => GPIO(), "pwm" => PWM())
Mattias Fält's avatar
Mattias Fält committed
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

"""
    dev = getdev(devname)
    Gets the device corresponding to the name `devname`
"""
function getdev(devname)
    dev = try
        DEVICES[devname]
    catch
        error("Device $devname does not exist")
    end
    return dev
end

"""
    bbparse(cmd)
Parse and execute the command `cmd`
"""
bbparse(any) = error("Unexpected input: $any")

"""
    bbparse(l::Tuple)
Parse input on the form `l=(iswrite, ndev, cmd1, cmd2, ..., cmdn)``
where if `iswrite`
    `cmdi = (devname, id, val)`
    and if not `iswrite`
    `cmdi = (devname, id)`
"""
function bbparse(l::Tuple)
    iswrite = l[1]::Bool            #True if write command, false if read
    ndev = l[2]::Int32              #Number of devices/commands
    for i = 1:ndev
        command = l[2+i]::Tuple
Mattias Fält's avatar
Bugs  
Mattias Fält committed
45
        dev = getdev(command[1])
Mattias Fält's avatar
Mattias Fält committed
46
47
48
49
        if iswrite
            write!(dev, command[2], command[3])
        else
            val = read(dev, command[2])
Marcus Thelander Andrén's avatar
Marcus Thelander Andrén committed
50
            println("$val")
Mattias Fält's avatar
Mattias Fält committed
51
52
53
54
55
56
57
58
59
60
61
62
            #TODO return somewhere
        end
    end
end

"""
    run_server(port=2001)
Run a server on `port` that listens for commands from computer
"""
function run_server(port=2001)
    server = listen(port)
    @async while isopen(server)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        try
            sock = accept(server)
            @async while isopen(sock)
                try
                    l = deserialize(sock);
                    println("deserialize: $l")
                    bbparse(l)
                catch err
                    if !isopen(sock) && isa(err, Base.EOFError)
                        println("Connection to server closed")
                    else
                        throw(err)
                    end
                end
            end
        catch err
            if isa(err,Base.UVError) && err.prefix == "accept"
                println("Server closed successfully")
            else
                throw(err)
            end
Mattias Fält's avatar
Mattias Fält committed
84
85
86
87
        end
    end
    return server
end