Created
August 3, 2014 08:21
-
-
Save blakejakopovic/4c64849fd86378001642 to your computer and use it in GitHub Desktop.
FirmataSerial Nodejs Clojurescript clj-firmata serial adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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