Last active
August 29, 2015 14:21
-
-
Save Gonzih/709312af3df44fb041e5 to your computer and use it in GitHub Desktop.
rpi.clj
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
#!/usr/bin/env boot | |
(require '[clojure.string :as string]) | |
(require '[clojure.java.io :as io]) | |
(require '[clojure.java.shell :as sh]) | |
(def HIGH 1) | |
(def LOW 0) | |
(defn LOW? [v] (= LOW v)) | |
(defn HIGH? [v] (= HIGH v)) | |
(defn pin-directory [pin] | |
(str "/sys/class/gpio/gpio" pin "/")) | |
(defn pin-mode [pin direction] | |
(let [dir (pin-directory pin)] | |
(when-not (.exists (io/file dir)) | |
(spit (str "/sys/class/gpio/export") pin)) | |
(spit (str dir "direction") (name direction)))) | |
(defn pin-mode-out [& pins] | |
(doseq [pin pins] | |
(pin-mode pin :out))) | |
(defn pin-mode-in [& pins] | |
(doseq [pin pins] | |
(pin-mode pin :in))) | |
(defn pin-write [pin value] | |
(spit (str (pin-directory pin) "value") value)) | |
(defn pin-read [pin] | |
(Integer/parseInt | |
(string/trim | |
(slurp (str (pin-directory pin) "value"))))) | |
(defn sleep [n] | |
(Thread/sleep n)) | |
(defn deploy! [] | |
(println "Calling make!") | |
(let [{:keys [out err exit]} (sh/sh "make" "something-important")] | |
(println err) | |
(println out) | |
(println "Exit code:" exit)) | |
(sleep 60000)) | |
(def acc (atom 0)) | |
(defn -main [& args] | |
(pin-mode-in 3) | |
(loop [v (pin-read 3)] | |
(sleep 100) | |
; Remember about pull up resistors | |
(if (LOW? v) | |
(do (swap! acc inc) | |
; Wait for 1 second before doing something | |
(when (= 10 @acc) | |
(reset! acc 0) | |
(deploy!))) | |
(reset! acc 0)) | |
(recur (pin-read 3)))) | |
; for gpi documentation look at | |
; https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment