 
 ;; startup and shutdown
 
-(defn prepare-state [this]
+(defn prepare-state [state]
   (let [default-z-drive (core getFocusDevice)
         default-xy-stage (core getXYStageDevice)
         z (get-z-stage-position default-z-drive)
         xy (get-xy-stage-position default-xy-stage)
         exposure (core getExposure)]
-    (swap! (.state this) assoc
-             :pause false
-             :stop false
-             :finished false
-             :last-wake-time (jvm-time-ms)
-             :last-stage-positions (into {} [[default-z-drive z]
-                                             [default-xy-stage xy]])
-             :reference-z z
-             :start-time (jvm-time-ms)
-             :init-auto-shutter (core getAutoShutter)
-             :init-exposure exposure
-             :init-shutter-state (core getShutterOpen)
-             :exposure {(core getCameraDevice) exposure}
-             :default-z-drive default-z-drive
-             :default-xy-stage default-xy-stage
-             :init-z-position z
-             :init-system-state (get-system-config-cached)
-             :init-continuous-focus (core isContinuousFocusEnabled)
-             :init-width (core getImageWidth)
-             :init-height (core getImageHeight)
-             :binning (core getProperty (core getCameraDevice) "Binning")
-             :bit-depth (core getImageBitDepth)
-             :pixel-size-um (core getPixelSizeUm)
-             :source (core getCameraDevice)
-             :pixel-type (get-pixel-type)
-      )))
+    (swap! state assoc
+           :pause false
+           :stop false
+           :finished false
+           :last-wake-time (jvm-time-ms)
+           :last-stage-positions (into {} [[default-z-drive z]
+                                           [default-xy-stage xy]])
+           :reference-z z
+           :start-time (jvm-time-ms)
+           :init-auto-shutter (core getAutoShutter)
+           :init-exposure exposure
+           :init-shutter-state (core getShutterOpen)
+           :exposure {(core getCameraDevice) exposure}
+           :default-z-drive default-z-drive
+           :default-xy-stage default-xy-stage
+           :init-z-position z
+           :init-system-state (get-system-config-cached)
+           :init-continuous-focus (core isContinuousFocusEnabled)
+           :init-width (core getImageWidth)
+           :init-height (core getImageHeight)
+           :binning (core getProperty (core getCameraDevice) "Binning")
+           :bit-depth (core getImageBitDepth)
+           :pixel-size-um (core getPixelSizeUm)
+           :source (core getCameraDevice)
+           :pixel-type (get-pixel-type)
+           )))
 
 (defn cleanup []
   (try
     (attempt-all
       (log "cleanup")
-      ; (do-when #(.update %) (:display @state))
       (state-assoc! :finished true :display nil)
       (when (core isSequenceRunning)
         (core stopSequenceAcquisition))

    (await-resume)))

(defn run-acquisition [this settings out-queue cleanup?]
  (try
    (def acq-settings settings)
    (log "Starting MD Acquisition: " settings)
    (. gui enableLiveMode false)
    (. gui enableRoiButtons false)
    (prepare-state this)
    (binding [state (.state this)]
      (def last-state state) ; for debugging
      (let [acq-seq (generate-acq-sequence settings @attached-runnables)]
        (def acq-sequence acq-seq)
        (execute (mapcat #(make-event-fns % out-queue) acq-seq))
        (when cleanup?
          (cleanup))
        (.put out-queue TaggedImageQueue/POISON)
        ))
    (catch Throwable t (do (ReportingUtils/showError t "Acquisition failed.")
                           (.printStackTrace t)
                           (when cleanup?
                             (cleanup))
                           (.put out-queue TaggedImageQueue/POISON)))))

;; generic metadata


(def active-slice-sequence (atom nil))

(def pixel-type-depths {"GRAY8" 1 "GRAY16" 2 "RGB32" 4 "RGB64" 8})

(defn throw-exception [msg]
  (throw (Exception. msg)))

;; time

