Permalink
Browse files

Added EC2 server code

  • Loading branch information...
awentz committed Feb 8, 2013
1 parent 9f5cf61 commit 6dab5b2b56b135574fc9452bdfcbe03fa35ae8e5
@@ -1,20 +1,23 @@
(ns renard-clojure.core
- (:use [cheshire.core]))
+ (:require [clj-http.client :as client]))
(use 'serial-port)
+(require '[clojure.data.json :as json])
+
+
+(def cm (clj-http.conn-mgr/make-reusable-conn-manager {:timeout 10 :threads 1}))
;to avoid having to use two-byte protocol version,
;instead, replace reserved channel values with nearby values
(defn package-values [channel-values]
- ;(concat [0x7e 0x80]
(replace {0x7d 0x7c 0x7e 0x7c 0x7f 0x80} channel-values))
(defn create-packets [pkg channel-count]
(loop [x (quot (count pkg) channel-count) rpkg pkg result []]
(if (>= x 0)
- (recur (- x 1) (drop channel-count rpkg) (concat result [0x7e 0x80] (take channel-count rpkg)))
+ (recur (- x 1) (drop channel-count rpkg) (concat result [(int 0x7e) (int 0x80)] (take channel-count rpkg)))
result)))
@@ -24,26 +27,38 @@
pkg
(loop [x (quot (count pkg) n) rpkg pkg result []]
(if (> x 0)
- (recur (- x 1) (drop n rpkg) (concat result (take n rpkg) '(0x7f)))
+ (recur (- x 1) (drop n rpkg) (concat result (take n rpkg) '((int 0x7f))))
result))))
;package the channel-values into packets of size channel-count and place a pad byte every n bytes
(defn pack-n-pad [channel-values n channel-count]
(insert-padding (create-packets (package-values channel-values) channel-count) n))
(defn renard-open-port [port]
- (open port))
+ (open port 38400))
(defn renard-close-port [port]
(close port))
;write data to port
(defn renard-write [data port]
(let [d (pack-n-pad data 100 32)]
- (write-int-seq port d)))
+ (write port (byte-array (count d) (map #(.byteValue %) d)))))
+
+
+(def port (renard-open-port "COM28"))
+
+
+(defn run-lights []
+ (while true
+ (do
+ (def b (client/get "http://107.22.230.121:50000" {:connection-manager cm :as :json}))
+ (def v (get b :body))
+ (renard-write v port)
+ (Thread/sleep 2000))))
+
+
+
-(defn test-lights []
- (dotimes [n 1000]
- ()))
@@ -1,39 +1,42 @@
-(ns renard-clojure.osc-server
- (:use [overtone.core]
- [renard-clojure.core]))
-
-(def renard-out :temp)
-(def port (renard-open-port "COM28"))
-
-(def channel-map (loop [n 1 result {}]
- (if (> n 32)
- result
- (recur (+ n 1) (merge result {(str "/Renard/fader" n) 0})))))
-
-(zero-conf-on)
-
-(defn make-osc-server [name port]
- (osc-server port name))
-
-(defn add-osc-handles [server]
- (dotimes [n 33] (osc-handle server (str "/Renard/fader" n)
- (fn [msg]
- (do
- (def channel-map
- (assoc channel-map (str "/Renard/fader" n) (int (first (:args msg)))))
- (renard-write channel-map port)
- )))))
-
-(defn remove-osc-handles [server]
- (osc-rm-all-handlers server "/Renard"))
-
-(defn close-osc-server [server] (osc-close server))
-
-
-
-(def server (make-osc-server "renard" 44100))
-
-(add-osc-handles server)
-
-
-
+(ns renard-clojure.osc-server
+ (:use [overtone.core]
+ [renard-clojure.core]))
+
+(require '[clojure.data.json :as json])
+
+
+(def renard-out :temp)
+(def port (renard-open-port "COM28"))
+
+(def channel-map (loop [n 1 result {}]
+ (if (> n 32)
+ result
+ (recur (+ n 1) (merge result {(str "/Renard/fader" n) 0})))))
+
+(zero-conf-on)
+
+(defn make-osc-server [name port]
+ (osc-server port name))
+
+(defn add-osc-handles [server]
+ (dotimes [n 33] (osc-handle server (str "/Renard/fader" n)
+ (fn [msg]
+ (do
+ (def channel-map
+ (assoc channel-map (str "/Renard/fader" n) (int (first (:args msg)))))
+ (renard-write (vals channel-map) port)
+ )))))
+
+(defn remove-osc-handles [server]
+ (osc-rm-all-handlers server "/Renard"))
+
+(defn close-osc-server [server] (osc-close server))
+
+
+
+(def server (make-osc-server "renard" 44100))
+
+(add-osc-handles server)
+
+
+
@@ -0,0 +1,45 @@
+(ns renard-clojure.osc-server
+ (:use [renard-clojure.core]))
+
+(require '[clojure.data.json :as json])
+(use 'overtone.osc)
+
+;(def renard-out :temp)
+;(def port (renard-open-port "COM28"))
+
+(def channel-map (loop [n 1 result {}]
+ (if (> n 32)
+ result
+ (recur (+ n 1) (merge result {(str "/Renard/fader" n) 0})))))
+
+;(zero-conf-on)
+
+(defn make-osc-server [name port]
+ (osc-server port name))
+
+
+
+
+
+(defn add-osc-handles [server]
+ (dotimes [n 33] (osc-handle server (str "/Renard/fader" n)
+ (fn [msg]
+ (do
+ (def channel-map
+ (assoc channel-map (str "/Renard/fader" n) (int (first (:args msg)))))
+ (println (vals channel-map))
+ )))))
+
+(defn remove-osc-handles [server]
+ (osc-rm-all-handlers server "/Renard"))
+
+(defn close-osc-server [server] (osc-close server))
+
+
+
+;(def server (make-osc-server "renard" 44100))
+
+;(add-osc-handles server)
+
+
+
@@ -0,0 +1,9 @@
+(ns renard-clojure.run
+ (:gen-class))
+
+(use 'ring.adapter.jetty)
+(require '[renard-clojure.web :as web])
+
+(defn -main [& args]
+ (run-jetty #'web/app {:port 50000}))
+
View
@@ -0,0 +1,53 @@
+(ns renard-clojure.web
+ (:use compojure.core)
+ (:use ring.middleware.json-params)
+ (:use renard-clojure.osc-server))
+
+(require '[clojure.data.json :as json])
+(use 'overtone.osc)
+; (def renard-channel-map (loop [n 1 result {}]
+; (if (> n 32)
+; result
+; (recur (+ n 1) (merge result {(str "/Renard/fader" n) 0})))))
+
+(def channel-vals (vec (for [x (range 1 33)] 0)))
+
+;(def renard-channel-map (apply merge (for [x (range 1 33)] {x 0})))
+
+(defn add-fader-handles [server]
+ (for [x (range 1 33)]
+ (osc-handle
+ server
+ (str "/Renard/fader" x)
+ (fn [msg]
+ (do
+ (def channel-vals (assoc channel-vals (- x 1) (int (first (:args msg)))))
+ (println channel-vals)
+ )))))
+
+; (defn add-renard-handles [server]
+; (dotimes [n 33] (osc-handle server (str "/Renard/fader" n)
+; (fn [msg]
+; (do
+; (def renard-channel-map
+; (assoc renard-channel-map (str "/Renard/fader" n) (int (first (:args msg)))))
+; (println (vals renard-channel-map)))))))
+
+
+(def server (make-osc-server "renard" 44100))
+
+(add-fader-handles server)
+
+
+(defn json-response [data & [status]]
+ {:status (or status 200)
+ :headers {"Content-Type" "application/json"}
+ :body (json/write-str data)})
+
+(defroutes handler
+ (GET "/" []
+ (json-response channel-vals)))
+
+(def app
+ (-> handler
+ wrap-json-params))

0 comments on commit 6dab5b2

Please sign in to comment.