changeset 72:c88ad4f6d9b4

can now write proper vbm files from clojure
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 03:41:24 -0600
parents 39928bf4622d
children 8a895ed4c0f9
files clojure/com/aurellem/vbm.clj
diffstat 1 files changed, 53 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/vbm.clj	Thu Mar 08 02:47:09 2012 -0600
     1.2 +++ b/clojure/com/aurellem/vbm.clj	Thu Mar 08 03:41:24 2012 -0600
     1.3 @@ -15,9 +15,9 @@
     1.4  
     1.5  (defn repair-vbm
     1.6    "Two 0's must be inserted after every reset."
     1.7 -  [vbm-seq]
     1.8 +  [vbm-masks]
     1.9    (loop [fixed []
    1.10 -         pending vbm-seq]
    1.11 +         pending vbm-masks]
    1.12      (if (empty? pending) fixed
    1.13          (let [mask (first pending)]
    1.14            (if (not= 0x0000 (bit-and mask (button-code :reset)))
    1.15 @@ -38,7 +38,58 @@
    1.16    (reset)
    1.17    (dorun (map step (vbm-masks vbm))))
    1.18  
    1.19 +(defn convert-buttons
    1.20 +  "To write a vbm file, we must remove the last two buttons after any
    1.21 +   reset event."
    1.22 +  [buttons]
    1.23 +  (loop [fixed []
    1.24 +         pending buttons]
    1.25 +    (if (empty? pending) fixed
    1.26 +        (let [mask (first pending)]
    1.27 +          (if (contains? (first pending) :reset)
    1.28 +            (recur (conj fixed mask) (drop 3 pending))
    1.29 +            (recur (conj fixed mask) (next pending)))))))
    1.30  
    1.31 +(def vbm-header
    1.32 +  (byte-array
    1.33 +   (map
    1.34 +    byte
    1.35 +    [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
    1.36 +     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
    1.37 +     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
    1.38 +     95 95 82 111 98 101 114 116 32 32 77 99 73 110 116 121 114 101 95
    1.39 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.40 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.41 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.42 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.43 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.44 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.45 +     95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
    1.46 +     95 95 95 95])))
    1.47 +
    1.48 +(def vbm-trailer
    1.49 +  (byte-array
    1.50 +   (map byte [0])))
    1.51 +
    1.52 +(defn buttons->vbm-bytes [buttons]
    1.53 +  (let [bytes-in-ints
    1.54 +        (map button-mask (convert-buttons buttons))
    1.55 +        high-bits (map #(bit-shift-right (bit-and 0xFF00 %) 8)
    1.56 +                       bytes-in-ints)
    1.57 +        low-bits (map #(bit-and 0xFF %) bytes-in-ints)
    1.58 +        convert-byte (fn [i] (byte (if (>= i 128) (- i 256) i)))
    1.59 +        contents
    1.60 +        (byte-array
    1.61 +         (concat
    1.62 +          vbm-header
    1.63 +          (map convert-byte (interleave high-bits low-bits))
    1.64 +          vbm-trailer))]
    1.65 +    contents))
    1.66 +        
    1.67 +(defn write-vbm [buttons #^File out]
    1.68 +  (clojure.java.io/copy (buttons->vbm-bytes buttons) out))
    1.69 +    
    1.70 +  
    1.71           
    1.72      
    1.73    
    1.74 \ No newline at end of file