From 441ec877d7ea7177271331a8da3103579b3598b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20F=C3=A4lt?= <mattiasf@control.lth.se>
Date: Thu, 11 Apr 2019 18:43:37 +0200
Subject: [PATCH] Fixed GPIO for julia1 and added GPIO on computer side

---
 src/BeagleBone/BeagleBone.jl     | 10 ++++++++-
 src/BeagleBone/GPIO.jl           |  9 ++++++++
 src/Computer/AbstractDevice.jl   |  2 ++
 src/Computer/BeagleBoneStream.jl | 11 ++++++++--
 src/Computer/ComediStream.jl     |  2 +-
 src/Computer/Computer.jl         |  1 +
 src/Computer/GPIO.jl             | 36 ++++++++++++++++++++++++++++++++
 7 files changed, 67 insertions(+), 4 deletions(-)
 create mode 100644 src/Computer/GPIO.jl

diff --git a/src/BeagleBone/BeagleBone.jl b/src/BeagleBone/BeagleBone.jl
index 1d888ac..0278241 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 88a1879..e016325 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 f5c5552..c88a2b0 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 844d721..1da986b 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 c2cddda..f3ac457 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 19e6ab5..1a803e4 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 0000000..a611e2a
--- /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
-- 
GitLab