Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
dpom committed Dec 24, 2024
1 parent e58ed09 commit 5681804
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 deletions.
4 changes: 3 additions & 1 deletion hooks/pre-push
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
#!/usr/bin/env bash



set -e

Expand Down
2 changes: 1 addition & 1 deletion hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
#
# prepend a issue number to a commit message automatically
#
Expand Down
8 changes: 6 additions & 2 deletions src/mirror/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
[manifold.stream :as stm]
[mirror.system :as sys]))


(def ig-key :mirror/main)


(defmethod ig/init-key ig-key [_ config]
(let [{:keys [tracer renderer logger]} config
s1 (:stream tracer)
Expand All @@ -16,14 +18,16 @@
(stm/connect s1 s2)
config))


(defmethod ig/halt-key! ig-key [_ sys]
(log (:logger sys) :info ::halt))


(defn -main
"Mirror entry point"
[]
(let [system (-> :prod
sys/prep
ig/init)
sys/prep
ig/init)
logger (get system [ig-key :logger])]
(log logger :info ::main)))
24 changes: 17 additions & 7 deletions src/mirror/renderer/screen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(processing.core
PApplet)))


(def ig-key :mirror.renderer/screen)

(derive ig-key :mirror/renderer)
Expand All @@ -19,19 +20,23 @@
(def background 255)
(def color 0)


(def colors
{:pen color
:rubber background})


(defn scale
[x max-x]
(if x
(Math/round (float (/ (abs (- max-x x)) scale-value)))
x))


(def screen {:width (scale sch/max-y 0)
:height (scale sch/max-x 0)})
(def screen
{:width (scale sch/max-y 0)
:height (scale sch/max-x 0)})


;; state

Expand All @@ -42,6 +47,7 @@
(reset! (:current-line state) {:tool nil :points []})
(reset! (:in-line? state) false))


(defn process-event!
[{:keys [lines current-point current-line in-line?] :as state}
{:keys [pen x y rubber]}]
Expand All @@ -50,8 +56,7 @@
(do
(swap! lines conj @current-line)
(reset! current-line {:tool nil :points []})
(reset! in-line? false)
)
(reset! in-line? false))
(do
(reset! current-point [(or (scale y 0) (first @current-point))
(or (scale x sch/max-x) (second @current-point))])
Expand Down Expand Up @@ -79,11 +84,13 @@
(q/stroke-weight 1)
(q/smooth))


(defn clean
[state]
(init-state! state)
(setup))


(defn ui-key-press
[state]
(let [raw-key (q/raw-key)
Expand All @@ -94,14 +101,16 @@
\c (clean state)
nil)))


(defn draw-line
[{:keys [tool points]}]
(q/stroke (get colors tool color))
(doseq [pair (partition 2 1 points)]
(apply q/line pair)))


(defn draw [{:keys [lines in-line? current-line]}]
(defn draw
[{:keys [lines in-line? current-line]}]
(doseq [l @lines]
(draw-line l))
(when @in-line?
Expand All @@ -116,8 +125,8 @@
:setup #'setup
:draw #(draw state)
:key-pressed #(ui-key-press state)
:middleware [qm/pause-on-error])
)
:middleware [qm/pause-on-error]))


;; integrant methods

Expand All @@ -136,6 +145,7 @@
:logger logger
:skatch (create-sketch state)}))


(defmethod ig/halt-key! ig-key [_ sys]
(let [{:keys [stream logger skatch]} sys]
(stm/close! stream)
Expand Down
29 changes: 17 additions & 12 deletions src/mirror/schema.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
(ns mirror.schema
(:require
[malli.core :as m]
[malli.generator :as mg]
))
(ns mirror.schema)


(def max-x 20967)

Expand All @@ -12,23 +9,27 @@

(def Y [:int {:min 0 :max max-y}])


(def Point
[:tuple #'X #'Y])


(def Event
[:map
[:x {:optional true} #'X]
[:y {:optional true} #'Y]
[:pres {:optional true} int?]
[:pen {:optional true} [:enum 0 1]]
[:rubber {:optional true} [:enum 0 1]]
[:touch {:optional true} [:enum 0 1]]])
[:map
[:x {:optional true} #'X]
[:y {:optional true} #'Y]
[:pres {:optional true} int?]
[:pen {:optional true} [:enum 0 1]]
[:rubber {:optional true} [:enum 0 1]]
[:touch {:optional true} [:enum 0 1]]])


(def Line
[:map
[:type [:enum :pen :rubber]]
[:points [:vector #'Point]]])


(def State
[:map
[:lines [:vector #'Line]]
Expand All @@ -38,6 +39,10 @@


(comment
(require
'[malli.core :as m]
'[malli.generator :as mg])

(mg/generate X)
;; => 11653
(mg/generate Y)
Expand Down
4 changes: 2 additions & 2 deletions src/mirror/system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

(defn prep
[profile]
(let [config (config profile)]
(ig/load-namespaces config)
(let [conf (config profile)]
(ig/load-namespaces conf)
config))
20 changes: 10 additions & 10 deletions src/mirror/tracer/event.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
[:sync 0])


(defmethod format-event abs-type [[_ code val]]
[(get abs-codes code :noop) val])
(defmethod format-event abs-type [[_ code v]]
[(get abs-codes code :noop) v])


(defmethod format-event key-type [[_ code val]]
[(get key-codes code :noop) val])
(defmethod format-event key-type [[_ code v]]
[(get key-codes code :noop) v])


(defn noop?
Expand All @@ -68,14 +68,14 @@
(defn format-raw-event
"Extract usefull infor from a raw event"
[event]
(let [type (get event 8)
(let [t (get event 8)
code (+ (get event 10)
(* (get event 11) 0x100))
val (+ (get event 12)
(* (get event 13) 0x100)
(* (get event 14) 0x10000)
(* (get event 15) 0x1000000))]
[type code val]))
v (+ (get event 12)
(* (get event 13) 0x100)
(* (get event 14) 0x10000)
(* (get event 15) 0x1000000))]
[t code v]))


(defn format-events
Expand Down

0 comments on commit 5681804

Please sign in to comment.