Git Product home page Git Product logo

archytas's Introduction

archytas

Build Status

archytas provides access to Standard Firmata commands via Clojure/clojurescript and then some extra functionalities.

This project is heavily based on peterschwarz/clj-firmata, you could consider it as a fork of it.

Usage

Add the following to your project.clj dependencies

[archytas "0.1.0-SNAPSHOT"]

For Clojurescript usage, see here

Connect to a Board

Connecting to a board is a simple as

(use 'archytas.core)
(def board (open-serial-board "ttyACM0"))

replacing ttyACM0 with the name or path of the appropriate serial port, arduinos tend to use that port name on linux.

An valid serial port of a connected arduino may be autodetected by passing :auto-detect Currently, this only works on Mac OS X and Linux.

Communicating with the board.

Performing simple queries on the board will result in events placed onto the board's core.async event channel.

For example, calling will result in an event of type :firmware-report to be placed on the channel. I.e. the following would be true:

(let [ch    (event-channel board)
      _     (query-firmware board)
      event (<!! ch)]
  (is (= :firmware-report (:type event)))
  (is (= "2.3" (:version event)))
  (is (= "Firmware Name" (:name event))))

Setting Pin Values

Setting a digital pin value to HIGH (1)

(set-digital board 13 :high)

and likewise to LOW (0)

(set-digital board 13 :low)

For analog values a pin must be in :pwm mode:

(set-pin-mode board 11 :pwm)
(set-analog board 11 255)

The above will set the brightness of an LED on pin 11 to maximum brightness

Receiving Information

The Firmata protocol provides several ways of receiving events from the board. The first is via an event channel:

(let [ch (event-channel board)]
  ; ...
  ; take events from the channel
  ; ...
  ; Then, when you're done, you should clean up:
  (release-event-channel board ch))

The channels have the same buffer size as the board is configured with on open-board.

The protocol also provides a core.async publisher, which publishes events based on [:event-type :pin]. This can be used in the standard fashion:

(let [sub-ch (chan)]
  (sub (event-publisher board) [:digital-msg 3] sub-ch)
  (go (loop
        (when-let [event (<! sub-ch)]
          ; ... do some stuff
          (recur)))))

To enable digital pin reporting:

(-> board
  (set-pin-mode 3 :input)
  (enable-digital-port-reporting 3 true))

This will result in the following events on the channel:

(let [ch    (event-channel board)
      event (<!! ch)]
   (is (= :digital-msg (:type event)))
   (is (= 3 (:pin event)))
   (is (= :high (:value event)))

By default, the pin value is returned as a key word, either :high or :low. This may be changed by using the :digital-result-format option when opening the board. For example:

(def board (open-serial-board "ttyACM0" :digital-result-format :raw))

With this board instance, any read or report of a digital pin's HIGH/LOW state will be 1 or 0, respectively. One can use :keyword, :boolean, :char, :symbol and :raw. The default is :keyword.

One can also write a custom digital formatter by passing a function for the :from-raw-digital:

(def board (open-serial-board "ttyACM0" 
                              :from-raw-digital #(if (= 1 %) :foo :bar)))

With this board instance, any read or report of a digital pin's HIGH/LOW state will be :foo or :bar respectively

For convenience, the archytas.async namspace provides the function digital-event-chan, which creates a channel with filtered events with the :digital-msg type and a specific pin. For example:

(let [ch (digital-event-chan board 3)]
  (go-loop [evt (<! ch)]
    (if (= :high (:value evt)) "Pressed" "Released")))

This ch can be closed like any other:

; Assuming (require '[clojure.core.async :as a])
(a/close! receiver)

Similarly for analog in reporting (on A0 in this example):

(enable-analog-in-reporting board 0 true)

will result in the following events on the channel:

(let [ch    (event-channel board)
     event (<!! ch)]
    (is (= :analog-msg (:type event)))
    (is (= 0 (:pin event)))
    (is (= 1000 (:value event)))

Like digital-event-chan, there is an analog-event-chan which will provide the events to a particular analog pin.

Exceptions and Error handling

Any exceptions that occur while reading or writing to the board will be forwarded along the event channel.

Close the connection to a Board

Board connections should be closed when complete:

(close! board)

Any channels will be closed as well.

License

Copyright © 2017 Hansel Wave & Victor Vanegas.

Distributed under GPL3.

License of the project which Archytas is based on, clj-firmata:

Copyright © 2014 Peter Schwarz

Distributed under the Eclipse Public License, the same as Clojure.

archytas's People

Contributors

hanselmw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.