Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
FirmataSerial Nodejs Clojurescript clj-firmata serial adapter
(ns cljs-nw-serial.core
(:require [cljs.nodejs :as nodejs]))
(def SerialPort (.-SerialPort (nodejs/require "serialport")))
(defprotocol FirmataStream
(open! [this] "opens the stream")
(close! [this] "closes the stream")
(listen [this handler] "listens for data on this stream")
(write [this data]))
(defrecord NodeSerialStream [port-name baud-rate]
(open! [this]
(let [serial-port (new SerialPort (:port-name this) #js {:baudrate (:baud-rate this)})]
(assoc this :serial-port serial-port)))
(close! [this]
(when-let [serial-port (:serial-port this)]
(.close serial-port)
(dissoc this :serial-port)))
(listen [this handler]
(when-let [serial-port (:serial-port this)]
(.on serial-port "data" handler)))
(write [this data]
(when-let [serial-port (:serial-port this)]
(.write serial-port data))))
(def s (NodeSerialStream. "/dev/tty.usbmodemfd1231" 57600))
(listen (open! s)
(fn [data] (println data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment