Blinking a LED on a BeagleBone using Clojure and NREPL

Table of Contents


2 What you need

  • a beaglebone board
  • a clojure jar
  • a nrepl jar
  • the oracle arm jdk
  • a LED
  • I used a 200Ω resistor, but I have no idea what I am doing, this is just the resistor that came with the LEDs
  • an nrepl client, I used nrepl.el

3 Hardware Setup

  • connect your LED
    • negative to resistor to pin 1 (GND) of the P8 set of pins
    • positive to pin 3 of the P8 set of pins
    • when you power the board, the led may light up weakly
  • get your beaglebone connected to the network
    • over the ethernet port
    • over the usb port
    • see the beaglebone docs

4 Software Setup

  • scp over files
    • arm jdk
    • nrepl jar
    • clojure jar
  • untar the arm jdk
  • launch a clojure repl
jdk1.7.0_06/bin/java -Dfile.encoding=utf8 -cp clojure-1.5.0-master-SNAPSHOT.jar:tools.nrepl-0.2.0-beta8.jar clojure.main
  • launch a nrepl server
    • the nrepl server binds to (all interfaces) by default
(use '[ :only (start-server stop-server)])
;=> nil
(defonce server (start-server :port 7888))
;=> #'user/server
  • connect to the nrepl server you just launched with your nrepl client
    • the ip will depend on how you connect your bealgebone to the network
    • the port is an option passed in above
    • nrepl.el doesn't seem to provide an easy way to connect to a host that is not localhost, so I went in and changed "localhost" to the ip of my bealgebone
  • you can paste the following code in to nrepl to blink the led
(require '[ :as io])
;=> nil
;; set the pin to GPIO mode, if yout LED is getting some voltage
;; (weakly lit) this should turn it off completely
(with-open [f (io/output-stream "/sys/kernel/debug/omap_mux/gpmc_ad6")]
  (.write f (.getBytes "0x7")))
;=> nil
;; if this file exists the pin has already been "exported"
(.exists (io/file "/sys/class/gpio/gpio38/direction"))
;=> false
(with-open [f (io/output-stream "/sys/class/gpio/export")]
  (.write f (.getBytes "38")))
;=> nil
(.exists (io/file "/sys/class/gpio/gpio38/direction"))
;=> true
(with-open [f (io/output-stream "/sys/class/gpio/gpio38/direction")]
  (.write f (.getBytes "out")))
;=> nil
;; set the pin high
(with-open [f (io/output-stream "/sys/class/gpio/gpio38/value")]
  (.write f (.getBytes "1")))
;=> nil
(Thread/sleep 1000)
;=> nil
;; set the pin low
(with-open [f (io/output-stream "/sys/class/gpio/gpio38/value")]
  (.write f (.getBytes "0")))
;=> nil
;; good hygiene
(with-open [f (io/output-stream "/sys/class/gpio/unexport")]
  (.write f (.getBytes "38")))

5 References

Date: 2012-08-21 13:29:50 PDT

Author: Kevin Downey

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0