Skip to content

Instantly share code, notes, and snippets.

@blakejakopovic
Created August 3, 2014 08:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakejakopovic/4c64849fd86378001642 to your computer and use it in GitHub Desktop.
Save blakejakopovic/4c64849fd86378001642 to your computer and use it in GitHub Desktop.
FirmataSerial Nodejs Clojurescript clj-firmata serial adapter
(ns cljs-nw-serial.core
(:require [cljs.nodejs :as nodejs]))
(enable-console-print!)
(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]
FirmataStream
(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