annotate clojure/com/aurellem/vbm.clj @ 71:39928bf4622d

refactored
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 02:47:09 -0600
parents ff6f1acae59e
children c88ad4f6d9b4
rev   line source
rlm@68 1 (ns com.aurellem.vbm
rlm@68 2 (:import java.io.File)
rlm@71 3 (:import org.apache.commons.io.FileUtils)
rlm@71 4 (:use com.aurellem.gb-driver))
rlm@68 5
rlm@68 6 (defn vbm-bytes [#^File vbm]
rlm@68 7 (let [bytes (FileUtils/readFileToByteArray vbm)
rlm@68 8 ints (int-array (count bytes))]
rlm@68 9 (areduce bytes idx _ nil
rlm@68 10 (aset ints idx
rlm@68 11 (bit-and 0xFF (aget bytes idx))))
rlm@68 12 ints))
rlm@68 13
rlm@68 14 (def vbm-header-length 255)
rlm@68 15
rlm@68 16 (defn repair-vbm
rlm@70 17 "Two 0's must be inserted after every reset."
rlm@68 18 [vbm-seq]
rlm@68 19 (loop [fixed []
rlm@70 20 pending vbm-seq]
rlm@68 21 (if (empty? pending) fixed
rlm@68 22 (let [mask (first pending)]
rlm@71 23 (if (not= 0x0000 (bit-and mask (button-code :reset)))
rlm@68 24 (recur (conj fixed mask 0x0000 0x0000) (next pending))
rlm@68 25 (recur (conj fixed mask) (next pending)))))))
rlm@68 26
rlm@68 27 (defn vbm-masks [#^File vbm]
rlm@68 28 (repair-vbm
rlm@68 29 (map (fn [[a b]]
rlm@68 30 (+ (bit-shift-left a 8) b))
rlm@68 31 (partition
rlm@68 32 2 (drop vbm-header-length (vbm-bytes vbm))))))
rlm@68 33
rlm@68 34 (defn vbm-buttons [#^File vbm]
rlm@68 35 (map buttons (vbm-masks vbm)))
rlm@68 36
rlm@71 37 (defn play-vbm [#^File vbm]
rlm@71 38 (reset)
rlm@71 39 (dorun (map step (vbm-masks vbm))))
rlm@68 40
rlm@68 41
rlm@68 42
rlm@68 43
rlm@68 44