 (defn first-trigger-missing? []
   (= "1" (get-property-value (core getCameraDevice) "OutputTriggerFirstMissing")))
 
+(defn keep-collecting-from-circular-buffer?
+  "Returns true if there is no reason to stop popping images
+   from the circular buffer."
+  []
+  (when (. mmc isBufferOverflowed)
+    (swap! state assoc :circular-buffer-overflow true))
+  (and (not (@state :stop))
+       (or (pos? (. mmc getRemainingImageCount))
+           (and (. mmc isSequenceRunning)
+                (not (@state :circular-buffer-overflow))))))
+
 (defn init-burst [length trigger-sequence relative-z]
   (core setAutoShutter (@state :init-auto-shutter))
   (load-property-sequences (:properties trigger-sequence))

    (swap! state assoc-in [:last-stage-positions (core getFocusDevice)]
           (last absolute-slices))))

(defn pop-burst-image
  "Returns a TaggedImage from the circular buffer -- times out if no
   image is found in circular buffer after waiting up to timeout-ms."
  [timeout-ms]
  (let [start-time (System/currentTimeMillis)]
    (while (and (. mmc isSequenceRunning)
                (zero? (. mmc getRemainingImageCount)))
      (if (< timeout-ms (- (System/currentTimeMillis) start-time))
        (throw (Exception. "Timed out waiting for image to arrive from camera."))
        (Thread/sleep 5))))
  (let [tagged-image (core popNextTaggedImage)]
    {:pix (.pix tagged-image)
     :tags (json-to-data (.tags tagged-image))}))


   (ReportingUtils/showError "Circular buffer overflowed."))
 (while (and (not (@state :stop)) (. mmc isSequenceRunning))
   (Thread/sleep 5)))

(defn assoc-if-nil [m k v]
  (if (nil? (m k))
