Commit bf995ec0 authored by Mattias Fält's avatar Mattias Fält
Browse files

Bugs

parent 00db8886
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
include("SysLED.jl") include("SysLED.jl")
#List of available devices and their constructors #List of available devices and their constructors
const DEVICES = Dict("sysled" => SysLED()] const DEVICES = Dict("sysled" => SysLED())
""" """
dev = getdev(devname) dev = getdev(devname)
...@@ -39,7 +39,7 @@ function bbparse(l::Tuple) ...@@ -39,7 +39,7 @@ function bbparse(l::Tuple)
ndev = l[2]::Int32 #Number of devices/commands ndev = l[2]::Int32 #Number of devices/commands
for i = 1:ndev for i = 1:ndev
command = l[2+i]::Tuple command = l[2+i]::Tuple
dev = getdev(devname) dev = getdev(command[1])
if iswrite if iswrite
write!(dev, command[2], command[3]) write!(dev, command[2], command[3])
else else
...@@ -59,8 +59,9 @@ function run_server(port=2001) ...@@ -59,8 +59,9 @@ function run_server(port=2001)
@async while isopen(server) @async while isopen(server)
sock = accept(server) sock = accept(server)
@async while isopen(sock) @async while isopen(sock)
l = deserialize(sock); println("deserialize: $l") l = deserialize(sock);
bbparse(l) println("deserialize: $l")
bbparse(l)
end end
end end
return server return server
......
...@@ -12,7 +12,7 @@ function write!(::SysLED, ind::Int32, val::Bool) ...@@ -12,7 +12,7 @@ function write!(::SysLED, ind::Int32, val::Bool)
close(file) close(file)
return return
end end
function read(::SysLED, ind::Int32) function Base.read(::SysLED, ind::Int32)
ind [1,2,3,4] && error("Invalid SysLEND ind: $ind") ind [1,2,3,4] && error("Invalid SysLEND ind: $ind")
filename = "/sys/class/leds/beaglebone:green:usr$(ind-1)" filename = "/sys/class/leds/beaglebone:green:usr$(ind-1)"
file = open(filename, "r") file = open(filename, "r")
......
struct BeagleBoneStream <: LabStream struct BeagleBoneStream <: LabStream
devices::Device devices::Array{Device,1}
stream::TCPSocket stream::TCPSocket
end end
...@@ -9,13 +9,13 @@ function BeagleBoneStream(addr::IPAddr, port::Int64=2001) ...@@ -9,13 +9,13 @@ function BeagleBoneStream(addr::IPAddr, port::Int64=2001)
end end
#For BeagleBoneStream we can directly serialize the data, other streams might want to send binary data #For BeagleBoneStream we can directly serialize the data, other streams might want to send binary data
serialize(bbstream::BeagleBoneStream, cmd) = serialize(bbstream.stream, cmd) Base.serialize(bbstream::BeagleBoneStream, cmd) = serialize(bbstream.stream, cmd)
function init_devices!(bbstream::BeagleBoneStream, devs::Device...) function init_devices!(bbstream::BeagleBoneStream, devs::Device...)
for dev in devs for dev in devs
if dev bbstream.devices if dev bbstream.devices
setstream!(dev, bbstream) setstream!(dev, bbstream)
append!(bbstream.devices, dev) push!(bbstream.devices, dev)
initialize(dev) initialize(dev)
else else
warn("Device $dev already added to a stream") warn("Device $dev already added to a stream")
...@@ -28,11 +28,11 @@ function send!(stream::BeagleBoneStream) ...@@ -28,11 +28,11 @@ function send!(stream::BeagleBoneStream)
cmds = Tuple[] cmds = Tuple[]
for dev in stream.devices for dev in stream.devices
val = getsetvalue(dev) val = getsetvalue(dev)
devstream, cmd = safe_getwritecommand(dev, val) cmd, devstream = safe_getwritecommand(dev, val)
devstream == stream || error("Device $dev is connected to other stream $devstream") devstream == stream || error("Device $dev is connected to other stream $devstream")
push!(cmds, cmd) push!(cmds, cmd)
end end
ncmds = length(cmds) ncmds = Int32(length(cmds))
if ncmds > 0 if ncmds > 0
allcmds = (true, ncmds, cmds...) allcmds = (true, ncmds, cmds...)
println("Sending command: $allcmds") println("Sending command: $allcmds")
...@@ -40,14 +40,14 @@ function send!(stream::BeagleBoneStream) ...@@ -40,14 +40,14 @@ function send!(stream::BeagleBoneStream)
end end
return return
end end
function read(stream::BeagleBoneStream) function Base.read(stream::BeagleBoneStream)
cmds = Tuple[] cmds = Tuple[]
for dev in stream.devices for dev in stream.devices
devstream, cmd = safe_getreadcommand(dev) cmd, devstream = safe_getreadcommand(dev)
devstream == stream || error("Device $dev is connected to other stream $devstream") devstream == stream || error("Device $dev is connected to other stream $devstream")
push!(cmds, cmd) push!(cmds, cmd)
end end
ncmds = length(cmds) ncmds = Int32(length(cmds))
if ncmds > 0 if ncmds > 0
allcmds = (false, ncmds, cmds...) allcmds = (false, ncmds, cmds...)
println("Sending command: $allcmds") println("Sending command: $allcmds")
...@@ -61,7 +61,7 @@ function init_devices!(bbstream::BeagleBoneStream, devs::Device...) ...@@ -61,7 +61,7 @@ function init_devices!(bbstream::BeagleBoneStream, devs::Device...)
for dev in devs for dev in devs
if dev bbstream.devices if dev bbstream.devices
setstream!(dev, bbstream) setstream!(dev, bbstream)
append!(bbstream.devices, dev) push!(bbstream.devices, dev)
initialize(dev) initialize(dev)
else else
warn("Device $dev already added to a stream") warn("Device $dev already added to a stream")
...@@ -70,11 +70,11 @@ function init_devices!(bbstream::BeagleBoneStream, devs::Device...) ...@@ -70,11 +70,11 @@ function init_devices!(bbstream::BeagleBoneStream, devs::Device...)
return return
end end
function close!(bbstream::BeagleBoneStream) function Base.close(bbstream::BeagleBoneStream)
cmds = Tuple[] cmds = Tuple[]
for dev in stream.devices for dev in stream.devices
close!(dev) close(dev)
end end
close!(bbstream.stream) close(bbstream.stream)
return return
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#What to do when connecting #What to do when connecting
initialize(::Device) = nothing initialize(::Device) = nothing
#What to do when disconnecting #What to do when disconnecting
close!(::Device) = nothing Base.close(::Device) = nothing
#Which stream is the Device connected to #Which stream is the Device connected to
getstream(dev::Device) = dev.stream getstream(dev::Device) = dev.stream
#Set the stream the Device is connected to #Set the stream the Device is connected to
...@@ -31,12 +31,13 @@ end ...@@ -31,12 +31,13 @@ end
#Maybe rethink a bit to support IObox #Maybe rethink a bit to support IObox
function send!(dev::Device, val) function send!(dev::Device, val)
cmd, stream = safe_getwritecommand(dev, val) cmd, stream = safe_getwritecommand(dev, val)
serialize(stream, (true, 1, cmd)) #TODO This is not very general
serialize(stream, (true, Int32(1), cmd))
return return
end end
function read(dev::Device) function Base.read(dev::Device)
cmd, stream = safe_getreadcommand(dev) cmd, stream = safe_getreadcommand(dev)
serialize(stream, (false, 1, cmd)) serialize(stream, (false, Int32(1), cmd))
#TODO get, wait for and return response #TODO get, wait for and return response
return return
end end
...@@ -3,24 +3,24 @@ mutable struct SysLED <: Device ...@@ -3,24 +3,24 @@ mutable struct SysLED <: Device
nextout::Bool nextout::Bool
latestread::Bool latestread::Bool
stream::LabStream stream::LabStream
SysLED(i::Int32) = new(i) SysLED(i::Int32) = new(i, false, false)
end end
SysLED(i::Int64) = SysLED(convert(Int32, i), false, false) SysLED(i::Int64) = SysLED(convert(Int32, i))
#Save value to send later #Save value to send later
set!(led::SysLED, val::Bool) = led.nextout = val set!(led::SysLED, val::Bool) = led.nextout = val
#Get the value from set! back for use #Get the value from set! back for use
getsetvalue(led::SysLED) = led.nextout getsetvalue(led::SysLED) = led.nextout
#Get value that was read erlier #Get value that was read erlier
get(led::SysLED) = led.latestread Base.get(led::SysLED) = led.latestread
#No definition for IOBox since there are no LEDs #No definition for IOBox since there are no LEDs
#Stream specific methods #Stream specific methods
function getwritecommand(stream::BeagleBoneStream, led::SysLED, val::Bool) function getwritecommand(stream::BeagleBoneStream, led::SysLED, val::Bool)
led.i [1,2,3,4] && error("SysLED $i not defined on BeagleBoneStream") led.i Int32[1,2,3,4] && error("SysLED $i not defined on BeagleBoneStream")
return ("sysled", led.i, val) return ("sysled", led.i, val)
end end
function getreadcommand(stream::BeagleBoneStream, led::SysLED) function getreadcommand(stream::BeagleBoneStream, led::SysLED)
led.i [1,2,3,4] && error("SysLED $i not defined on BeagleBoneStream") led.i Int32[1,2,3,4] && error("SysLED $i not defined on BeagleBoneStream")
return ("sysled", led.i) return ("sysled", led.i)
end end
...@@ -14,7 +14,7 @@ for i = 1:10 ...@@ -14,7 +14,7 @@ for i = 1:10
send!(stream) #Sends all the outputs to the stream in one atomic call send!(stream) #Sends all the outputs to the stream in one atomic call
#read(stream) #Sends request to read, reads all inputs #read(stream) #Sends request to read, reads all inputs
sleep(1) sleep(1)
led_on = !led_on ledon = !ledon
end end
for i = 1:10 for i = 1:10
send!(led2, ledon) send!(led2, ledon)
...@@ -22,9 +22,9 @@ for i = 1:10 ...@@ -22,9 +22,9 @@ for i = 1:10
send!(led3, !ledon) send!(led3, !ledon)
#read(stream) #Sends request to read, reads all inputs #read(stream) #Sends request to read, reads all inputs
sleep(0.5) sleep(0.5)
led_on = !led_on led_on = !ledon
end end
set!(led2, false) set!(led2, false)
set!(led3, false) set!(led3, false)
send!(stream) send!(stream)
close!(stream) #Tells BeagleBone to stop listening and close outputs close(stream) #Tells BeagleBone to stop listening and close outputs
...@@ -3,6 +3,19 @@ abstract type Device end ...@@ -3,6 +3,19 @@ abstract type Device end
include("Device.jl") include("Device.jl")
##### General interface for LabStream
## A stream of type T <: LabStream should define the methods
# serialize(::T, cmd) # Send data `cmd`
# init_devices!(::T, devs::Device...) # Initialize devices and connect them to stream
# send!(::T) # Send set! commands for all devices to stream, using getwritecommand(stream::LabStream, dev::Device, getsetvalue(dev::Device))
# read(::T) # Send read commands for all devices to stream, sing get(dev::Device)
# init_devices! # Initialize all connected devises, using initialize(::Device)
# close # Close connection, call close(::Device) on all connected devices
#Include the stream definitions
include("BeagleBoneStream.jl")
##### General interface for devices ##### General interface for devices
## A device to type T<:Device should define the methods ## A device to type T<:Device should define the methods
# set!(dev::T, val) # Remember val for next send!(::LabStream) command # set!(dev::T, val) # Remember val for next send!(::LabStream) command
...@@ -14,22 +27,10 @@ include("Device.jl") ...@@ -14,22 +27,10 @@ include("Device.jl")
## Default methods: ## Default methods:
# initialize(::Device) = nothing #What to do when initializing # initialize(::Device) = nothing #What to do when initializing
# close!(::Device) = nothing #What to do when disconnecting # close(::Device) = nothing #What to do when disconnecting
# getstream(dev::Device) = dev.stream #Which stream is the Device connected to # getstream(dev::Device) = dev.stream #Which stream is the Device connected to
# setstream!(dev::Device, stream::LabStream) = dev.stream = stream #Set the stream the Device is connected to # setstream!(dev::Device, stream::LabStream) = dev.stream = stream #Set the stream the Device is connected to
##### END General interface for devices ##### END General interface for devices
#Include the device definitions #Include the device definitions
include("SysLED.jl") include("SysLED.jl")
##### General interface for LabStream
## A stream of type T <: LabStream should define the methods
# serialize(::T, cmd) # Send data `cmd`
# init_devices!(::T, devs::Device...) # Initialize devices and connect them to stream
# send!(::T) # Send set! commands for all devices to stream, using getwritecommand(stream::LabStream, dev::Device, getsetvalue(dev::Device))
# read(::T) # Send read commands for all devices to stream, sing get(dev::Device)
# init_devices! # Initialize all connected devises, using initialize(::Device)
# close! # Close connection, call close!(::Device) on all connected devices
#Include the stream definitions
include("BeagleBoneStream.jl")
Markdown is supported
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