Created
August 14, 2014 08:31
-
-
Save blakejakopovic/e695d503d57dd230e5d4 to your computer and use it in GitHub Desktop.
Clojurescript clj-firmata WIP adapter
This file contains hidden or 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 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