Skip to content

Instantly share code, notes, and snippets.

@tonsky tonsky/xmas.clj
Created Dec 19, 2016

Embed
What would you like to do?
Xmas decoration with AnyBar and Clojure
(import '[java.net InetAddress DatagramPacket DatagramSocket])
(defonce socket (DatagramSocket.))
(defn send-message [text port]
(let [host (InetAddress/getLocalHost)
packet (DatagramPacket. (.getBytes text) (.length text) host port)]
(.send socket packet)))
(defonce *run? (atom true))
(defonce *timeout (atom 200))
(defn run! [program]
(future
(reset! *run? false)
(Thread/sleep (* 2 @*timeout))
(reset! *run? true)
(loop [program program]
(let [[a b c] (first program)]
(send-message a 1738)
(send-message b 1739)
(send-message c 1740)
(Thread/sleep @*timeout)
(when @*run?
(recur (next program)))))))
(defn stop! []
(reset! *run? false))
(def toggle [["green" "green" "green"]
["red" "red" "red"]])
(def left-to-right
[["green" "green" "green"]
["red" "green" "green"]
["red" "red" "green"]
["red" "red" "red"]
["green" "red" "red"]
["green" "green" "red"]])
(def blink
[["green" "red" "green"]
["red" "green" "red"]
["red" "green" "red"]
["green" "red" "green"]])
(def program
(->>
(concat
(repeat 5 toggle)
left-to-right
["green" "green" "green"]
(reverse left-to-right)
(repeat 4 blink))
(flatten)
(partition 3)
(cycle)))
(comment
(reset! *timeout 200)
(run! program)
(stop!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.