Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clojurescript clj-firmata WIP adapter
(ns testdrive.example
(:require [firmata.core :refer [open-board event-channel]]
[firmata.stream :refer [FirmataStream]]
[cljs.nodejs :as nodejs])
(def net (nodejs/require "net"))
(defrecord SocketStream [host port]
FirmataStream
(open! [this]
(let [socket (.Socket net)
conn (.connect socket (:port this) (:host this))]
(assoc this :conn conn)))
(close! [this]
(when-let [conn (:conn this)]
(.close conn)
(dissoc this :conn)))
(listen [this handler]
(when-let [conn (:conn this)]
(.on conn "data" handler)))
(write [this data]
(when-let [conn (:conn this)]
(.write conn data))))
(let [b (SocketStream. "192.168.2.202" 5678)]
(reset! board (open-board b))
(reset! receiver-ch (event-channel @board))
(go (while true
(when-let [event (<! @receiver-ch)]
(println event)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment