Skip to content

Commit

Permalink
Release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
peterschwarz committed Jan 5, 2016
2 parents a8e31b6 + b201d34 commit 65b0af4
Show file tree
Hide file tree
Showing 20 changed files with 620 additions and 325 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ target

*.sublime-*
*.swp

node_modules
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: clojure
lein: lein2
script: lein2 test
script: lein2 cleantest
jdk:
- openjdk7
- oraclejdk7
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# clj-gpio

A basic library for reading, writing and watching GPIO signals on a Raspberry
Pi, in a Clojure REPL-friendly way.
Pi, in a Clojure REPL-friendly way. Now, also targets ClojureScript.

## Usage

Add the following to your `project.clj`

[clj-gpio "0.1.0"]
[clj-gpio 0.2.0]

Fire up a REPL, and require `gpio.core`.

Expand All @@ -28,11 +28,6 @@ To read the value of the port, we can do the following:
user=> (read-value port)
:low

Or, more conveniently, we can deref it:

user=> @port
:low

To set values on the port, The port needs to be configured for `out` mode:

user=> (set-direction! port :out)
Expand All @@ -45,6 +40,12 @@ as follows:
With our LED connected to gpio 17, we should see it turned on. We can also
read back the value and see that `(= :high @port)`.

We can also toggle the state, for convenience:

user=> (toggle! port)

which will flip the state from `:low` to `:high` or vice versa.

### GPIO Listening.

We can also pull events off of a gpio port by using `open-channel-port`. In
Expand All @@ -57,14 +58,13 @@ For example (if we have a push button on GPIO 18):
user=> (def ch-port (open-channel-port 18))
#'user/ch-port
user=> (set-direction! ch-port :in)
nil
...
user=> (set-edge! ch-port :both) ; or :falling, :rising, and :none to disable
nil
...

We'll also set the bit to :high when the button pressed:

user=> (set-active-low! ch-port true)
nil

Let's turn on the LED we defined in the Read/Write example above when our
button is pressed:
Expand Down
33 changes: 20 additions & 13 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
(defproject clj-gpio "0.1.0"
:description "A lightweight Clojure library for Raspberry PI GPIO"
:url "https://github.com/peterschwarz/clj-gpio"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:min-lein-version "2.0.0"
:source-paths ["src/main/clojure"]
:test-paths ["src/test/clojure"]
:java-source-paths ["src/main/java"]
:javac-options ["-target" "1.6" "-source" "1.6"]
(defproject clj-gpio "0.2.0"
:description "A lightweight Clojure library for Raspberry PI GPIO"
:url "http://peterschwarz.github.io/clj-gpio"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:min-lein-version "2.0.0"
:source-paths ["src/main/clojure"]
:test-paths ["src/test/clojure"]
:java-source-paths ["src/main/java"]
:javac-options ["-target" "1.6" "-source" "1.6"]

:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[net.java.dev.jna/jna "4.1.0"]])
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "1.7.170"]
[org.clojure/core.async "0.2.374"]
[net.java.dev.jna/jna "4.2.1"]]

:aliases {"cljs:test" ["trampoline" "run" "-m" "clojure.main" "./scripts/test.clj"]
"cljs:repl" ["trampoline" "run" "-m" "clojure.main" "./scripts/repl.clj"]
"cljs:dev" ["trampoline" "run" "-m" "clojure.main" "./scripts/build.clj"]
"cljs:dev:watch" ["trampoline" "run" "-m" "clojure.main" "./scripts/watch.clj"]
"cleantest" ["do" ["clean"] ["test"] ["cljs:test"]]})

8 changes: 8 additions & 0 deletions scripts/build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(require 'cljs.build.api)

(cljs.build.api/build
(cljs.build.api/inputs "src/main/clojure" "src/dev/clojure")
{:main 'gpio.dev
:output-to "target/out/dev.js"
:output-dir "target/out"
:target :nodejs })
12 changes: 12 additions & 0 deletions scripts/repl.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(require 'cljs.repl)
(require 'cljs.build.api)
(require 'cljs.repl.node)

(cljs.build.api/build "src/main/clojure"
{:output-to "target/out/main.js"
:output-dir "target/out"
:verbose true})

(cljs.repl/repl (cljs.repl.node/repl-env)
:watch "src/main/clojure"
:output-dir "target/out")
22 changes: 22 additions & 0 deletions scripts/test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(require 'cljs.build.api)

(def test-inputs (cljs.build.api/inputs "src/main/clojure" "src/test/clojure"))

(def test-opts
{:main 'gpio.test-runner
:output-to "target/out/test.js"
:output-dir "target/out"
:target :nodejs })

(require 'clojure.java.shell)

(defn run-tests []
(let [result (clojure.java.shell/sh "node" (:output-to test-opts))]
(println (:out result))
(.println *err* (:err result))))

(cljs.build.api/build test-inputs test-opts)

(run-tests)

(System/exit 0)
8 changes: 8 additions & 0 deletions scripts/watch.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(require 'cljs.build.api)

(cljs.build.api/watch
(cljs.build.api/inputs "src/main/clojure" "src/dev/clojure")
{:main 'gpio.dev
:output-to "target/out/dev.js"
:output-dir "target/out"
:target :nodejs })
52 changes: 52 additions & 0 deletions src/dev/clojure/gpio/dev.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(ns gpio.dev
(:require [cljs.nodejs :as nodejs]
[gpio.core :as gpio]
[cljs.core.async :as a :refer [<!]])
(:require-macros [cljs.core.async.macros :refer [go]]))


(nodejs/enable-util-print!)


(defn- configure-ports [led btn]
(println "Configuring ports")
(gpio/set-direction! led :out)

(gpio/set-direction! btn :in)
(gpio/set-active-low! btn true)
(gpio/set-edge! btn :both))

(defn- forward-value [ch led]
(go
(println "Looping on btn changes...")
(loop []
(when-let [value (<! ch)]
(gpio/write-value! led value)
(recur)))))

(defn- on-sigint [led btn]
(println "\nCleaning up ports")
(gpio/close! led)
(gpio/close! btn))

(defn setup-led-and-button [led-pin button-pin]
(let [led (gpio/open-port led-pin)
btn (gpio/open-channel-port button-pin)
ch (gpio/create-edge-channel btn)]

(.on js/process "SIGINT" #(on-sigint led btn))

(println "Waiting to settle")
(js/setTimeout (fn []
(configure-ports led btn)
(forward-value ch led))
100)))

(defn -main [& args]
(let [args (into [] args)
led (get args 0 18)
button (get args 1 19)]
(println "Running against an LED on pin" led "and a button on pin" button)
(setup-led-and-button led button)))

(set! *main-cli-fn* -main)
Loading

0 comments on commit 65b0af4

Please sign in to comment.