Skip to content

Instantly share code, notes, and snippets.

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

What would you like to do?
Xmas decoration with AnyBar and Clojure
(import '[ 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]
(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
(repeat 5 toggle)
["green" "green" "green"]
(reverse left-to-right)
(repeat 4 blink))
(partition 3)
(reset! *timeout 200)
(run! program)
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.