diff --git a/src/firmata/core.clj b/src/firmata/core.clj index 397e8c0..a19df2a 100644 --- a/src/firmata/core.clj +++ b/src/firmata/core.clj @@ -1,5 +1,5 @@ (ns firmata.core - (:require [clojure.core.async :as a :refer [go chan >! >!! ! >!! @board-state :board-version :version)) + + (firmware + [this] + (dissoc (:board-firmware @board-state) :type)) + (query-firmware [this] (send-message this [SYSEX_START REPORT_FIRMWARE SYSEX_END])) diff --git a/src/firmata/shift.clj b/src/firmata/shift.clj new file mode 100644 index 0000000..3e4e7da --- /dev/null +++ b/src/firmata/shift.clj @@ -0,0 +1,20 @@ +(ns firmata.shift + (:require [firmata.core :refer [set-digital]])) + +(defn shift-out + "Sends a shift out to the board, for sending values to a shift register" + [board latch-pin data-pin clock-pin bit-order value] + {:pre [(or (= bit-order :lsb-first) (= bit-order :msb-first))]} + + (set-digital board latch-pin :high) + + (doseq [i (range 8)] + (let [shift-by (if (= :lsb-first bit-order) i (- 7 i))] + (set-digital board data-pin (if (= 0 (bit-and value (bit-shift-left 1 shift-by))) :low :high))) + + (set-digital board clock-pin :high) + (set-digital board clock-pin :low)) + + (set-digital board latch-pin :low) + + board) diff --git a/src/firmata/util.clj b/src/firmata/util.clj index 70b6127..c015e35 100644 --- a/src/firmata/util.clj +++ b/src/firmata/util.clj @@ -1,6 +1,24 @@ (ns firmata.util (:require [clojure.core.async :as a])) +(defn arduino-map + "Clojure implemation of the Arduino map function. + http://arduino.cc/en/reference/map" + [x, in-min, in-max, out-min, out-max] + (+ (quot (* (- x in-min) (- out-max out-min)) (- in-max in-min)) out-min)) + +(defn arduino-constrain + "Clojure implementation of the Arduino constrain function. + http://arduino.cc/en/Reference/Constrain" + [x min max] + (cond (< x min) min + (<= min x max) x + :else max)) + +(defn to-voltage + "Takes an analog value and converts it to the true voltage value." + [x] + (* x 0.004882814)) (defn to-hex-str "For debug output" diff --git a/test/firmata/test/core.clj b/test/firmata/test/core.clj index 4844924..4cc3884 100644 --- a/test/firmata/test/core.clj +++ b/test/firmata/test/core.clj @@ -23,18 +23,30 @@ (reduce (fn [^ByteBuffer b ^bytes value] (.put b (to-bytes value))) buffer more) (ByteArrayInputStream. (.array buffer)))) - +(defn mock-serial-listen + [handler] + (fn [_ h _] + (reset! handler h) + (h (create-in-stream 0xF9 9 9)) + (h (create-in-stream 0xF0 0x79 9 9 "Test Firmware" 0xF7)) + nil)) (deftest test-read-events (let [handler (atom nil)] (with-redefs [serial/open (fn [name _ rate] {:port name :rate rate}) - serial/listen (fn [port h skip?] (reset! handler h))] + serial/listen (mock-serial-listen handler)] (let [board (open-board "some_board") evt-chan (event-channel board)] + (testing "board version initialized with board handshake messages" + (is (= "9.9" (version board)))) + + (testing "board firmware initialized with board handshake messages" + (is (= {:name "Test Firmware" :version "9.9"} (firmware board)))) + (testing "read protocol version" (@handler (create-in-stream 0xF9 2 3)) (if-let [event (