Skip to content

Instantly share code, notes, and snippets.

@claj
Last active December 20, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claj/6195310 to your computer and use it in GitHub Desktop.
Save claj/6195310 to your computer and use it in GitHub Desktop.
(use 'seesaw.core)
;; the state, when was the last keypress?
(def last-tap (atom nil))
(defn nanoseconds-diff-to-bpm [now then]
(let [nanosecs-in-a-minute (* 60 1000 1000 1000.0)
diff (- now then)]
(/ nanosecs-in-a-minute diff)))
(def ze-label (label "o"))
(def ze-frame (frame :title "BPM-counter" :content ze-label))
(-> ze-frame pack! show!)
;; looking for changes of the state, process and presentes the BPM from this info
(add-watch last-tap :calculate-bpm-and-put-it-to-label
(fn [_ _ last-time now]
(if last-time
(config! ze-label :text (nanoseconds-diff-to-bpm now last-time)))))
;; attempts to put the current time in nanoseconds into the state
(defn tap! [] (reset! last-tap (System/nanoTime)))
;;when any key is pressed the window triggers fn tap!
(listen ze-frame :key-pressed (fn [e] (future (tap!))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment