annotate clojure/com/aurellem/vbm.clj @ 70:ff6f1acae59e

removed zero frame confusion
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 02:25:20 -0600
parents 86093f2ce7d1
children 39928bf4622d
rev   line source
rlm@68 1 (ns com.aurellem.vbm
rlm@68 2 (:import java.io.File)
rlm@68 3 (:import org.apache.commons.io.FileUtils))
rlm@68 4
rlm@68 5 (defn vbm-bytes [#^File vbm]
rlm@68 6 (let [bytes (FileUtils/readFileToByteArray vbm)
rlm@68 7 ints (int-array (count bytes))]
rlm@68 8 (areduce bytes idx _ nil
rlm@68 9 (aset ints idx
rlm@68 10 (bit-and 0xFF (aget bytes idx))))
rlm@68 11 ints))
rlm@68 12
rlm@68 13 (def button-mask
rlm@68 14 {;; main buttons
rlm@68 15 :a 0x0001
rlm@68 16 :b 0x0002
rlm@68 17
rlm@68 18 ;; directional pad
rlm@68 19 :r 0x0010
rlm@68 20 :l 0x0020
rlm@68 21 :u 0x0040
rlm@68 22 :d 0x0080
rlm@68 23
rlm@68 24 ;; meta buttons
rlm@68 25 :select 0x0004
rlm@68 26 :start 0x0008
rlm@68 27
rlm@68 28 ;; hard reset -- not really a button
rlm@68 29 :reset 0x0800})
rlm@68 30
rlm@68 31 (defn button-code [buttons]
rlm@68 32 (reduce bit-or 0x0000 (map button-mask buttons)))
rlm@68 33
rlm@68 34 (defn buttons [mask]
rlm@68 35 (loop [buttons []
rlm@68 36 masks (seq button-mask)]
rlm@68 37 (if (empty? masks) buttons
rlm@68 38 (let [[button value] (first masks)]
rlm@68 39 (if (not= 0x0000 (bit-and value mask))
rlm@68 40 (recur (conj buttons button) (rest masks))
rlm@68 41 (recur buttons (rest masks)))))))
rlm@68 42
rlm@68 43 (def vbm-header-length 255)
rlm@68 44
rlm@68 45 (defn repair-vbm
rlm@70 46 "Two 0's must be inserted after every reset."
rlm@68 47 [vbm-seq]
rlm@68 48 (loop [fixed []
rlm@70 49 pending vbm-seq]
rlm@68 50 (if (empty? pending) fixed
rlm@68 51 (let [mask (first pending)]
rlm@68 52 (if (not= 0x0000 (bit-and mask (button-mask :reset)))
rlm@68 53 (recur (conj fixed mask 0x0000 0x0000) (next pending))
rlm@68 54 (recur (conj fixed mask) (next pending)))))))
rlm@68 55
rlm@68 56 (defn vbm-masks [#^File vbm]
rlm@68 57 (repair-vbm
rlm@68 58 (map (fn [[a b]]
rlm@68 59 (+ (bit-shift-left a 8) b))
rlm@68 60 (partition
rlm@68 61 2 (drop vbm-header-length (vbm-bytes vbm))))))
rlm@68 62
rlm@68 63 (defn vbm-buttons [#^File vbm]
rlm@68 64 (map buttons (vbm-masks vbm)))
rlm@68 65
rlm@68 66
rlm@68 67
rlm@68 68
rlm@68 69
rlm@68 70