Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active August 29, 2015 14:02
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 jackrusher/c42accc08ab691d6a880 to your computer and use it in GitHub Desktop.
Save jackrusher/c42accc08ab691d6a880 to your computer and use it in GitHub Desktop.
A quick example of talking to StudioNAND's tactile sensor with clojure.
(ns livecode.tact
(:require [serial-port :as serial])
(use quil.core))
;; what ports are there?
;; (serial/list-ports)
;; the serial port of the tact device
(def port (serial/open "/dev/tty.usbmodem1421"))
;; how to close a port
;;(serial/close port)
;; example of a simple debugging callback
;;(serial/on-n-bytes port 2 #(println (+ (int (first %)) (bit-shift-left (second %) 8))))
;; whether we've done the handshake and what samples we've got
(def running (atom false))
(def samples (atom []))
;; in case we need to remove the listener and install a new one
;; (serial/remove-listener port)
(serial/on-n-bytes port 2
#(let [v (+ (int (first %)) (bit-shift-left (second %) 8))]
(cond (and (not @running) (= v 53)) (do (println "hi5! Ready.")
(reset! running true))
(and (>= v 0) (< v 1024)) (do (swap! samples conj v))
(== 2000 v) (reset! samples [])
(== 2001 v) (str "store values")
(and (>= v 3000) (< v 3008)) (str "sensor idx:" (- v 3000))
:else (println "WAT?" v))))
(defn setup []
;; "hi" is our end of the handshake
(serial/write port (byte-array (map byte "hi")))
(frame-rate 1))
(defn draw []
(background 18)
(doseq [[i s] (map vector (range) @samples)]
(rect (* i 20) 0 10 (map-range s 0 1024 0 500)))
(serial/write port (byte-array (mapv byte "g0;44;32;2;\n"))))
(defsketch tact-graph
:title "signal histogram"
:setup setup
:draw draw
:size [700 500])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment