   
   GpioPort
 
-  (set-direction! [_ direction] (set-direction! gpio-port direction))
-  (set-active-low! [_ active-low?] (set-active-low! gpio-port active-low?))
+  (set-direction! [this direction]
+    (set-direction! gpio-port direction)
+    this)
+
+  (set-active-low! [this active-low?]
+    (set-active-low! gpio-port active-low?)
+    this)
+
   (read-value [_] (read-value gpio-port))
 
-  (write-value! [this value] (>!! write-ch value))
+  (write-value! [this value]
+    (>!! write-ch value)
+    this)
 
   GpioChannelProvider
 
-  (set-edge! [_ setting]
-    (do-set-edge! port setting))
+  (set-edge! [this setting]
+    (do-set-edge! port setting)
+    this)
 
   (create-edge-channel [_] (tap-and-wrap-chan mult-ch (chan-factory-fn)))
-
-  clojure.lang.IDeref
-  (deref [this] (read-value this))
 
   Closeable
   (close! [_]

(defn- value-file [port]
  (str "/sys/class/gpio/gpio" port "/value"))

(defn random-access [filename]
  (RandomAccessFile. filename "rw"))

(defrecord BasicGpioPort [port filename file formatter]
  GpioPort

  (set-direction! [_ direction]
    (do-set-direction! port direction))

  (set-active-low! [_ active-low?]
    (do-set-active-low! port active-low?))

  (read-value
    [_]
    (.seek file 0)
    (formatter (char (.read file))))

  (write-value!
    [_ value]
    (.seek file 0)
    (.writeByte file (high-low-value value)))

  clojure.lang.IDeref
  (deref [this] (read-value this))

  Closeable
  (close! [_]
    (.close file)
    (unexport! port)))

(defn open-port

        formatter (or from-raw-fn
                      (partial format-raw-digital digital-result-format))
        filename (value-file port)
        gpio-port (BasicGpioPort. port filename formatter)]
    ; Need to wait for the direction file to be available
    (Thread/sleep 100)
    (try
      (cond-> gpio-port
        direction (set-direction! direction)
        active-low? (set-active-low! active-low?)
        initial-value (write-value! initial-value))
      (catch Exception e
        (close! gpio-port)
        (throw e)))))
