# HG changeset patch # User Robert McIntyre # Date 1331199684 21600 # Node ID c88ad4f6d9b4338e61250988403909e78661709d # Parent 39928bf4622d3d933c540c5d2b79aa311881266a can now write proper vbm files from clojure diff -r 39928bf4622d -r c88ad4f6d9b4 clojure/com/aurellem/vbm.clj --- a/clojure/com/aurellem/vbm.clj Thu Mar 08 02:47:09 2012 -0600 +++ b/clojure/com/aurellem/vbm.clj Thu Mar 08 03:41:24 2012 -0600 @@ -15,9 +15,9 @@ (defn repair-vbm "Two 0's must be inserted after every reset." - [vbm-seq] + [vbm-masks] (loop [fixed [] - pending vbm-seq] + pending vbm-masks] (if (empty? pending) fixed (let [mask (first pending)] (if (not= 0x0000 (bit-and mask (button-code :reset))) @@ -38,7 +38,58 @@ (reset) (dorun (map step (vbm-masks vbm)))) +(defn convert-buttons + "To write a vbm file, we must remove the last two buttons after any + reset event." + [buttons] + (loop [fixed [] + pending buttons] + (if (empty? pending) fixed + (let [mask (first pending)] + (if (contains? (first pending) :reset) + (recur (conj fixed mask) (drop 3 pending)) + (recur (conj fixed mask) (next pending))))))) +(def vbm-header + (byte-array + (map + byte + [86 66 77 26 1 0 0 0 105 74 88 79 89 1 0 0 0 0 0 0 0 1 2 112 0 0 0 + 0 0 0 0 0 1 0 0 0 80 79 75 69 77 79 78 32 89 69 76 76 1 -105 124 4 + 3 0 0 0 0 0 0 0 0 1 0 0 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 82 111 98 101 114 116 32 32 77 99 73 110 116 121 114 101 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 + 95 95 95 95]))) + +(def vbm-trailer + (byte-array + (map byte [0]))) + +(defn buttons->vbm-bytes [buttons] + (let [bytes-in-ints + (map button-mask (convert-buttons buttons)) + high-bits (map #(bit-shift-right (bit-and 0xFF00 %) 8) + bytes-in-ints) + low-bits (map #(bit-and 0xFF %) bytes-in-ints) + convert-byte (fn [i] (byte (if (>= i 128) (- i 256) i))) + contents + (byte-array + (concat + vbm-header + (map convert-byte (interleave high-bits low-bits)) + vbm-trailer))] + contents)) + +(defn write-vbm [buttons #^File out] + (clojure.java.io/copy (buttons->vbm-bytes buttons) out)) + + \ No newline at end of file