Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove port_handler code which has now been extracted to port-serial

  • Loading branch information...
commit 10d66734e7a9ad88897c088a5cfbdd9af05eb43d 1 parent 0b23759
@samaaron authored
View
3  .gitignore
@@ -1,3 +1,6 @@
+.cake
+*.jar
+pom.xml
lib/
\.\#*
\#*#
View
7 project.clj
@@ -1,7 +1,6 @@
-(defproject org.clojars.samaaron/monome-serial "0.1.3"
+(defproject monome-serial "0.1.6"
:description "An interface to the monome (http://monome.org)"
:dependencies [[org.clojure/clojure "1.2.0"]
- [org.clojure/clojure-contrib "1.2.0"]]
- :dev-dependencies [[native-deps "1.0.5"]]
- :native-dependencies [[org.clojars.samaaron/rxtx "2.2.0"]])
+ [org.clojure/clojure-contrib "1.2.0"]
+ [serial-port "1.0.7"]])
View
27 src/monome_serial/core.clj
@@ -1,5 +1,5 @@
(ns monome-serial.core
- (:require [monome-serial.port-handler :as port-handler]
+ (:require [serial-port :as port]
[monome-serial.animations :as animations])
(:import (java.util.concurrent LinkedBlockingQueue)))
@@ -8,17 +8,30 @@
(defn connect
"Connect to a monome with a given port identifier"
[port-name]
- (let [port (port-handler/open-port port-name)
- send-fn (fn [bytes] (port-handler/write port bytes))
- close (fn [] (port-handler/close-port port))
+ (let [port (port/open port-name)
+ send-fn (fn [bytes] (port/write port bytes))
+ close (fn [] (port/close port))
handlers (ref {})
open? (ref true)
queue (LinkedBlockingQueue.)
worker (Thread. #(loop [bytes (.take queue)]
(apply send-fn [bytes])
(recur (.take queue))))
- _ (port-handler/listen port handlers)
- monome (Monome. send-fn close handlers open? queue worker)]
+ monome (Monome. send-fn close handlers open? queue worker)
+ parse-bytes (fn [[action-byte xy-byte]]
+ (let [action (cond
+ (= 0 action-byte) :press
+ (= 16 action-byte) :release
+ :else :unknown)
+ x (bit-shift-right xy-byte 4)
+ y (bit-shift-right ^Integer (.byteValue ^Integer (bit-shift-left xy-byte 4)) 4)
+ grouped-handlers @handlers
+ all-handlers (flatten (for [[_ group] grouped-handlers] (for [[_ handler] group] handler)))]
+
+ (doseq [handler all-handlers]
+ (handler action x y))))]
+
+ (port/on-n-bytes port 2 parse-bytes)
(.start worker)
(animations/intromation monome)
@@ -31,7 +44,7 @@
;;TODO fixme
(defn disconnect
- "Close the monome down. Currently crashes the JVM due to a bug in the port-handler code"
+ "Close the monome down."
[monome]
(dosync
((:close monome))
View
90 src/monome_serial/port_handler.clj
@@ -1,90 +0,0 @@
-(ns monome-serial.port-handler
- (:import
- (gnu.io CommPortIdentifier
- SerialPort
- SerialPortEventListener
- SerialPortEvent)
- (java.io OutputStream
- InputStream)))
-
-(def PORT-OPEN-TIMEOUT 2000)
-(defrecord Port [raw-port out-stream in-stream])
-
-(defn port-ids
- "Returns a seq representing all port identifiers visible to the system"
- []
- (enumeration-seq (CommPortIdentifier/getPortIdentifiers)))
-
-(defn port-at
- "Returns the name of the serial port at idx."
- [idx]
- (.getName (nth (port-ids) idx)))
-
-(defn list-ports
- "Print out the available ports with an index number for future reference
- with (port-at <i>)."
- ([] (list-ports (port-ids) 0))
- ([ports idx]
- (when ports
- (println idx ":" (.getName (first ports)))
- (recur (next ports) (inc idx)))))
-
-;;TODO fixme
-(defn close-port
- "Closes an open port."
- [port]
- (let [raw-port (:raw-port port)]
- (.removeEventListener raw-port)
- (.close raw-port)))
-
-(defn open-port
- "Returns an opened serial port.
-
- (open-port \"/dev/ttyUSB0\")"
- [path]
- (try
- (let [port-id (first (filter #(= path (.getName %)) (port-ids)))
- raw-port (.open port-id "monome" PORT-OPEN-TIMEOUT)
- out (.getOutputStream raw-port)
- in (.getInputStream raw-port)
- _ (.setSerialPortParams raw-port 115200
- SerialPort/DATABITS_8
- SerialPort/STOPBITS_1
- SerialPort/PARITY_NONE)]
-
- (Port. raw-port out in))
- (catch Exception e
- (throw (Exception. (str "Couldn't connect to the port with path " path ))))))
-
-(defn write
- "Write a byte array to a port"
- [port bytes]
- (.write ^OutputStream (:out-stream port) ^bytes bytes))
-
-(defn- event-handler [^InputStream in-stream ^SerialPortEvent event handlers]
- (when (= SerialPortEvent/DATA_AVAILABLE (.getEventType event))
- (while (pos? (.available in-stream))
- (let [op (.read in-stream)
- xy (.read in-stream)
- action (cond
- (= 0 op) :press
- (= 16 op) :release
- :else :unknown)
- x (bit-shift-right xy 4)
- y (bit-shift-right ^Integer (.byteValue ^Integer (bit-shift-left xy 4)) 4)
- grouped-handlers @handlers
- all-handlers (flatten (for [[_ group] grouped-handlers] (for [[_ handler] group] handler)))]
-
- (doseq [handler all-handlers]
- (handler action x y))))))
-
-(defn stop-listening [port]
- (.removeEventListener (:raw-port port)))
-
-(defn listen [port handlers]
- (let [raw-port (:raw-port port)
- in-stream (:in-stream port)
- listener (reify SerialPortEventListener
- (serialEvent [_ event] (event-handler in-stream event handlers)))]
- (.addEventListener raw-port listener)
- (.notifyOnDataAvailable raw-port true)))
Please sign in to comment.
Something went wrong with that request. Please try again.