Add motion control

master
Jure Podgoršek 2018-08-10 18:29:18 +02:00
parent c8b6ceebdc
commit 2e8b3140ce
8 changed files with 64 additions and 17 deletions

View File

@ -5,7 +5,8 @@
[sliva.data :refer [appstate]]
[sliva.routes :refer [app-routes]]
[sliva.pages.hub :refer [hub]]
[sliva.pages.visual :refer [visual]])
[sliva.pages.visual :refer [visual]]
[sliva.pages.gibanica :refer [gibanica]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(enable-console-print!)
@ -16,6 +17,7 @@
(defmulti current-page #(@appstate :page))
(defmethod current-page :hub [] [hub])
(defmethod current-page :visual [] [visual])
(defmethod current-page :gibanica [] [gibanica])
;; On figwheel reload do this (reload routes, render currently active page)
(defn init-app []

View File

@ -7,6 +7,9 @@
(defn set-param [param val]
(swap! appstate assoc-in [:vizual param] val))
(defn update-param [param fun]
(swap! appstate update-in [:vizual param] fun))
;; Inicializacija ;;
(def scena (THREE.Scene.))
(def kamera (THREE.PerspectiveCamera.
@ -108,3 +111,7 @@
(.removeEventListener js/window "resize" on-window-resize false)
(.removeChild container (.-firstChild container)))
(defn spin-objects [[cas x y z]]
(update-param :rotacija-x (fn [old] (+ old (/ x 1000))))
(update-param :rotacija-y (fn [old] (+ old (/ y 1000))))
(update-param :rotacija-z (fn [old] (+ old (/ z 1000)))))

View File

@ -0,0 +1,30 @@
(ns sliva.pages.gibanica
(:require [reagent.core :as reagent]
[sliva.data :refer [appstate]]
[sliva.pages.navigation :refer [navigation]]
[sliva.socket :refer [send-message]]))
(defn motion-track [event]
(let [pospesek (.-acceleration event)
gibX (.-x pospesek)
gibY (.-y pospesek)
gibZ (.-z pospesek)
cas (-> (js/Date.)
(.valueOf))]
(send-message "gibanica" cas gibX gibY gibZ)))
(defn start-motion-track []
(.addEventListener js/window "devicemotion" motion-track))
(defn stop-motion-track []
(.removeEventListener js/window "devicemotion" motion-track))
(defn gibanica []
(reagent/create-class
{:display-name "gibanica"
:reagent-render (fn [] [:div
[:h1 "gibaj me!"]
[navigation]])
:component-did-mount #(start-motion-track)
:component-will-unmount #(stop-motion-track)}))

View File

@ -19,4 +19,4 @@
[:tr {:key idx}
[:td cid]
[:td "connected"]])))))]]
(navigation)])
[navigation]])

View File

@ -5,4 +5,5 @@
[:h2 "Navigation"]
[:ul
[:li [:a {:href "#/hub"} "Hub"]]
[:li [:a {:href "#/visual"} "Visual"]]]])
[:li [:a {:href "#/visual"} "Visual"]]
[:li [:a {:href "#/gibanica"} "Gibanica"]]]])

View File

@ -20,4 +20,5 @@
(defroute "/hub" [] (swap! appstate assoc :page :hub))
(defroute "/visual" [] (swap! appstate assoc :page :visual))
(defroute "/test" [] (console.log "test!"))
(defroute "/gibanica" [] (swap! appstate assoc :page :gibanica))
(hook-browser-navigation!))

View File

@ -3,36 +3,39 @@
[compojure.core :refer [defroutes GET]]
[compojure.route :refer [resources]]
[clojure.string :as str]
[clojure.core.async :refer [chan <! <!! >! put! close! go go-loop]]
[clojure.core.async :refer [chan <! <!! >! put! close! go go-loop sliding-buffer]]
[ring.util.response :refer [resource-response]]
[ring.middleware.reload :refer [wrap-reload]]
[chord.http-kit :refer [with-channel]]))
(def clients (atom {}))
(defn send-all [& args]
(println "sending to all" args (keys @clients))
(defn send-all [cmd args]
(doseq [conn @clients]
(println "sending" args)
(put! (val conn) (str/join ":" args))))
;;(println "sending" cmd (str/join ":" args))
(put! (val conn)
(str cmd ":" (str/join ":" args)))))
(defn socket-handler [request]
(let [cid (java.util.UUID/randomUUID)]
(with-channel request ws-ch
{:read-ch (chan (sliding-buffer 100))
:write-ch (chan (sliding-buffer 100))}
(go
(let [{:keys [message]} (<! ws-ch)]
(println "new connection - " cid)
(>! ws-ch (str "hello:" cid))
(swap! clients assoc cid ws-ch)
(send-all "open" cid)
(send-all "open" [cid])
(go-loop []
(let [{:keys [message]} (<! ws-ch)]
(println "GOT MSG!" message)
(let [[cmd & args] (str/split message #":")]
(println "command" cmd "(" args ")")
(println "command" cmd args)
(condp = cmd
"ping" (>! ws-ch "pong")
(println "DEBUG: msg ignored: " cmd))) (keys @clients)
"gibanica" (send-all "spin" args)
(println "DEBUG: msg ignored: " cmd)))
(recur))))))))
;;(close! ws-ch)))))

View File

@ -2,17 +2,23 @@
(:require [chord.client :refer [ws-ch]]
[clojure.string :as str]
[cljs.core.async :refer [<! >! put! close!]]
[sliva.data :refer [appstate]])
[sliva.data :refer [appstate]]
[sliva.gfx :refer [spin-objects]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(defn handle-message [msg-str]
(let [[msg & args] (str/split msg-str #":")]
(console.info "msg" msg "args" args)
;;(console.info "msg" msg "args" args)
(condp = msg
"pong" (console.log "hura, server se odziva")
"open" (swap! appstate assoc :clients (conj (:clients @appstate) (first args)))
"spin" (spin-objects args)
(console.info "msg ingored: " msg))))
(defn send-message [& message]
(let [vtic (:vtic @appstate)]
(put! vtic (str/join ":" message))))
(defn websocket-init []
(console.log "init webscoket")
(go (let [host (aget js/window "location" "hostname")
@ -32,7 +38,4 @@
(.addEventListener
js/window
"click"
(fn []
(let [vtic (:vtic @appstate)]
(console.log "klik")
(put! vtic "ping")))))
#(send-message "ping")))