Skip to content
Snippets Groups Projects
Commit 12e1d3f6 authored by Mattias Fält's avatar Mattias Fält
Browse files

more readable commands and gpio test

parent 441ec877
No related branches found
No related tags found
No related merge requests found
Pipeline #701 failed
using LabConnections.Computer
using Sockets
stream = BeagleBoneStream(ip"192.168.7.2")
gpio112 = GPIO(1, true) # Writing P9.30 (according to setup specification on BB)
gpio66 = GPIO(29, false) # Reading P8.7
init_devices!(stream, gpio112, gpio66)
send(gpio112, true)
val = read(gpio112)
const READ = Int32(0)
const WRITE = Int32(1)
const INIT = Int32(2)
include("IO_Object.jl")
include("Debug.jl")
include("SysLED.jl")
......@@ -118,14 +122,14 @@ and send back on socket (vals, timestamps).
function bbparse(l::Tuple, sock)
operation = l[1]::Int32 #1 if write command, 0 if read, 2 if init
ndev = l[2]::Int32 #Number of devices/commands
if operation == 1
if operation == WRITE
for i = 1:ndev
command = l[2+i]::Tuple
dev = getdev(command[1], command[2])
write!(dev, command[3])
end
return
elseif operation == 0
elseif operation == READ
#TODO fix to have at least partial type stability
vals = Array{Any,1}(undef,ndev)
timestamps = Array{UInt64,1}(undef,ndev)
......@@ -137,7 +141,7 @@ function bbparse(l::Tuple, sock)
end
bbsend(sock, (vals, timestamps))
return
elseif operation == 2
elseif operation == INIT
for i = 1:ndev
command = l[2+i]::Tuple
dev = initdev(command[1], command[2])
......
const GPIO_VALUE = Int32(1)
const GPIO_DIRECTION = Int32(2)
const GPIO_EDGE = Int32(3)
"""
GPIO(i::Int32)
Lowest form of communication with the GPIO pins. The available pins are
......@@ -37,7 +41,7 @@ end
# Default to writing "value"
function write!(gpio::GPIO, val::String, debug::Bool=false)
write!(gpio, (Int32(1), val), debug=debug)
write!(gpio, (GPIO_VALUE, val), debug=debug)
end
"""
......@@ -60,7 +64,7 @@ end
# Default to reading "value"
function read(gpio::GPIO, debug::Bool=false)
read(gpio, Int32(1), debug=debug)
read(gpio, GPIO_VALUE, debug=debug)
end
"""
l = read(gpio::GPIO, operation::Int32, debug::Bool=false)
......
export BeagleBoneStream, init_devices!
const BB_READ = Int32(0)
const BB_WRITE = Int32(1)
const BB_INIT = Int32(2)
struct BeagleBoneStream <: LabStream
devices::Array{AbstractDevice,1}
sendbuffer::Array{Tuple,1}
......@@ -26,14 +30,14 @@ function init_devices!(bbstream::BeagleBoneStream, devs::AbstractDevice...)
readcmd = getreadcommand(bbstream, dev)
name = readcmd[1]::String
idx = readcmd[2]::Integer
serialize(bbstream.stream, (Int32(2), Int32(1), (name, Int32(idx))))
serialize(bbstream.stream, (BB_INIT, 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)))
serialize(bbstream.stream, (BB_WRITE, Int32(1), (name, Int32(idx), commands)))
end
else
@warn "Device $dev already added to a stream"
......@@ -44,14 +48,14 @@ end
function send(bbstream::BeagleBoneStream)
ncmds = length(bbstream.sendbuffer)
serialize(bbstream.stream, (Int32(1), Int32(ncmds), bbstream.sendbuffer...))
serialize(bbstream.stream, (BB_WRITE, Int32(ncmds), bbstream.sendbuffer...))
empty!(bbstream.sendbuffer)
return
end
#TODO know the types of outputs some way
function read(bbstream::BeagleBoneStream)
ncmds = length(bbstream.readbuffer)
serialize(bbstream.stream, (Int32(0), Int32(ncmds), bbstream.readbuffer...))
serialize(bbstream.stream, (BB_READ, Int32(ncmds), bbstream.readbuffer...))
empty!(bbstream.readbuffer)
vals, timestamps = deserialize(bbstream.stream)
length(vals) == ncmds || error("Wrong number of return values in $vals on request $(bbstream.readbuffer)")
......@@ -61,13 +65,13 @@ end
#The following are for interal use only
function send(bbstream::BeagleBoneStream, cmd)
allcmds = (Int32(1), Int32(1), cmd)
allcmds = (BB_WRITE, Int32(1), cmd)
println("Sending single command: $allcmds")
serialize(bbstream.stream, allcmds)
return
end
function read(bbstream::BeagleBoneStream, cmd)
allcmds = (Int32(0), Int32(1), cmd)
allcmds = (BB_READ, Int32(1), cmd)
println("Sending single command: $allcmds")
serialize(bbstream.stream, allcmds)
vals, timestamps = deserialize(bbstream.stream)
......
export GPIO
const GPIO_VALUE = Int32(1)
const GPIO_DIRECTION = Int32(2)
const GPIO_EDGE = Int32(3)
"""
GPIO(i::Integer, direction:Bool)
direction = true if output (write)
......@@ -16,9 +19,9 @@ 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"))
return ("gpio", gpio.i, (GPIO_DIRECTION, "out"))
else
return ("gpio", gpio.i, (Int32(2), "in"))
return ("gpio", gpio.i, (GPIO_DIRECTION, "in"))
end
end
......@@ -28,7 +31,7 @@ function getwritecommand(stream::BeagleBoneStream, gpio::GPIO, val::Bool)
error("Can not write to GPIO input (read) device")
end
# TODO Check valid GPIO index
return ("gpio", gpio.i, (Int32(1), val ? "1" : "0"))
return ("gpio", gpio.i, (GPIO_VALUE, val ? "1" : "0"))
end
function getreadcommand(stream::BeagleBoneStream, gpio::GPIO)
# TODO Check valid GPIO index
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment