diff --git a/src/BeagleBone/BeagleBone.jl b/src/BeagleBone/BeagleBone.jl index 1d888ac94f3ff6e3e2aa530fbd16680cac59c906..0278241babc0772088dcce043a99c52f1ab0d564 100644 --- a/src/BeagleBone/BeagleBone.jl +++ b/src/BeagleBone/BeagleBone.jl @@ -177,10 +177,18 @@ function run_server(port=2001; debug=false) @async while isopen(sock) try l = deserialize(sock); - bbparse(l, sock) + println("deserialized:") + println(l) + try + bbparse(l, sock) + catch err + @warn "Failure in bbparse, server should keep running, error:" + println(err) + end catch err if !isopen(sock) && (isa(err, Base.EOFError) || isa(err, Base.UVError)) println("Connection to server closed") + # TODO teardown and remove all devices else println("error: $(typeof(err))") println("err: $err") diff --git a/src/BeagleBone/GPIO.jl b/src/BeagleBone/GPIO.jl index 88a1879f361fbbf95b272d008f393fb6171332f1..e016325033b215b94069c4ef1011e1feb5d64160 100644 --- a/src/BeagleBone/GPIO.jl +++ b/src/BeagleBone/GPIO.jl @@ -35,6 +35,11 @@ struct GPIO <: IO_Object end end +# Default to writing "value" +function write!(gpio::GPIO, val::String, debug::Bool=false) + write!(gpio, (Int32(1), val), debug=debug) +end + """ write!(gpio::GPIO, args::Tuple{Int32,String}, debug::Bool=false) Writes an entry to an operation on a GPIO, of the form args = (operation, entry). @@ -53,6 +58,10 @@ function write!(gpio::GPIO, args::Tuple{Int32,String}, debug::Bool=false) end end +# Default to reading "value" +function read(gpio::GPIO, debug::Bool=false) + read(gpio, Int32(1), debug=debug) +end """ l = read(gpio::GPIO, operation::Int32, debug::Bool=false) Reads the current value from an operation on a GPIO. diff --git a/src/Computer/AbstractDevice.jl b/src/Computer/AbstractDevice.jl index f5c5552622e44f7ac8ad8edcceab48f1168eb57f..c88a2b0955ce2b420dca0c12864919e415e6773e 100644 --- a/src/Computer/AbstractDevice.jl +++ b/src/Computer/AbstractDevice.jl @@ -10,6 +10,8 @@ getstream(dev::AbstractDevice) = dev.stream #Set the stream the AbstractDevice is connected to setstream!(dev::AbstractDevice, stream::LabStream) = dev.stream = stream +# Write command that needs to be sent after setup (for example direction of GPIO) +getsetupwrite(::LabStream, ::AbstractDevice) = nothing function safe_getwritecommand(dev::AbstractDevice, val) stream = try getstream(dev) catch diff --git a/src/Computer/BeagleBoneStream.jl b/src/Computer/BeagleBoneStream.jl index 844d721e0d8c3168b8031fec1572d16fc31589ee..1da986ba3935ef7ac6bc2e6b40e6b2ae93c3243e 100644 --- a/src/Computer/BeagleBoneStream.jl +++ b/src/Computer/BeagleBoneStream.jl @@ -27,8 +27,16 @@ function init_devices!(bbstream::BeagleBoneStream, devs::AbstractDevice...) name = readcmd[1]::String idx = readcmd[2]::Integer serialize(bbstream.stream, (Int32(2), Int32(1), (name, Int32(idx)))) + + setupwrite = getsetupwrite(bbstream, dev) + if setupwrite !== nothing + name = setupwrite[1]::String + idx = setupwrite[2]::Integer + commands = setupwrite[3]::Tuple + serialize(bbstream.stream, (Int32(1), Int32(1), (name, Int32(idx), commands))) + end else - warn("Device $dev already added to a stream") + @warn "Device $dev already added to a stream" end end return @@ -69,7 +77,6 @@ function read(bbstream::BeagleBoneStream, cmd) end function close(bbstream::BeagleBoneStream) - cmds = Tuple[] for dev in bbstream.devices close(dev) end diff --git a/src/Computer/ComediStream.jl b/src/Computer/ComediStream.jl index c2cddda9b24aecc532972c14bf2752d452d5f221..f3ac457e44098bfe6f4cc54e2a603dcc9dfe8e1b 100644 --- a/src/Computer/ComediStream.jl +++ b/src/Computer/ComediStream.jl @@ -23,7 +23,7 @@ function init_devices!(comedistream::ComediStream, devs::AbstractDevice...) push!(comedistream.devices, dev) initialize(dev) else - warn("Device $dev already added to the stream") + @warn "Device $dev already added to the stream" end end return diff --git a/src/Computer/Computer.jl b/src/Computer/Computer.jl index 19e6ab5c6c85f634f35355db6c701c018ec1b13e..1a803e4a5f43fc1fea3f2c0e9590a893a47f1954 100644 --- a/src/Computer/Computer.jl +++ b/src/Computer/Computer.jl @@ -34,4 +34,5 @@ include("BeagleBoneStream.jl") #Include the device definitions include("SysLED.jl") +include("GPIO.jl") include("10V.jl") diff --git a/src/Computer/GPIO.jl b/src/Computer/GPIO.jl new file mode 100644 index 0000000000000000000000000000000000000000..a611e2a866946ed219ed0c0a7a421cc47ef51122 --- /dev/null +++ b/src/Computer/GPIO.jl @@ -0,0 +1,36 @@ +export GPIO + +""" +GPIO(i::Integer, direction:Bool) +direction = true if output (write) +direction = false if input (read) + +""" +mutable struct GPIO <: AbstractDevice + i::Int32 + direction::Bool + stream::LabStream + GPIO(i::Int32, direction::Bool) = new(i, direction) +end +GPIO(i::Integer, direction::Bool) = GPIO(convert(Int32, i), direction) + +function getsetupwrite(stream::BeagleBoneStream, gpio::GPIO) + if gpio.direction + return ("gpio", gpio.i, (Int32(2), "out")) + else + return ("gpio", gpio.i, (Int32(2), "in")) + end +end + +#Stream specific methods +function getwritecommand(stream::BeagleBoneStream, gpio::GPIO, val::Bool) + if !gpio.direction + error("Can not write to GPIO input (read) device") + end + # TODO Check valid GPIO index + return ("gpio", gpio.i, (Int32(1), val ? "1" : "0")) +end +function getreadcommand(stream::BeagleBoneStream, gpio::GPIO) + # TODO Check valid GPIO index + return ("gpio", gpio.i) +end