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

Bugs

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