Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clojurescript node.js firmata message parser (incomplete)
(defn parse-message
"Parses incoming data and emits valid messages"
[]
(let [data (js/Buffer 0)]
(fn [emitter buffer]
;; Append new data to buffer
(set! data (.concat js/Buffer [data buffer]))
;; Remove incomplete messages (ie. not control charactors)
(while (> (.slice data 0 1) 127))
(set! data (.slice data 1 (.length data)))
(cond
;; analog/digital I/O message or set pin mode or protocol version
(some #{byte} [0xE0 0x90 0xF4 0xF9])
(if (>= (.length data) 3)
(.emit emitter "data" (.slice data 1 3))
(set! data (.slice 3 (.length data))))
;; report analog/digital pin
(some #{byte} [0xC0 0xD0])
(if (>= (.length data) 2)
(.emit emitter "data" (.slice data 1 2))
(set! data (.slice 2 (.length data))))
;; sysex start
(= byte 0xF0)
(if (= 0xF7 (.slice data (- 1 (.length data)) (.length data)))
(.emit emitter "data" data)
(set! data (js/Buffer 0)))
;; reset
(= byte 0xFF)
(do
(.emit emitter "data" 0xFF)
(set! data (.slice 2 (.data length))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment