I got an arduino microcontroller a little while ago, and have played with it a little but found it’s C/C++ development environment annoying. I wanted to control it from lisp, and that meant serial IO. Many other languages have special serial libraries you can use, where you instatiate a Serial object with configuration like baud, parity, etc. John Wiseman wrote arduino_serial.py that shows this pattern.

I searched around for lisp options, and came up with a few options:

open /dev/ttyUSB0 directly (from a comp.lang.lisp thread) use a FFI wrapper around libusb (from a comp.lang.lisp thread) use sb-ext:run-program to call out to python/C/whatever to deal with the serial port (we do something similar at work to render trac wiki markup to HTML in lisp) write a small C program and FFI to that (was tempting for the experience)

After much trial and error and some advice from the helpful folks on #lisp, I got method #1 working tonight. I was able to read from arduino pretty easily, but I needed to issue this magic stty command before I could write:

stty -F /dev/ttyUSB0 9600 raw -parenb -parodd cs8 -hupcl -cstopb clocal

I had been curious how lisp (or my underlying linux) would know what baud, parity, etc to use, and it makes perfect sense that I need to set these first. After that, the lisp side ends up pretty simple. It took a little tweaking to find the right :direction , :if-exists , and :external-format arguments.

(with-open-file (stream "/dev/ttyUSB0" :direction :io :if-exists :overwrite :external-format :ascii) (format stream "hello") (read-line stream))

Disorganized source is available at http://github.com/ryepup/arduino-experiments. I have a few servos laying around, maybe this weekend I’ll have time to get lisp moving around the real world.

My dream goal is to have lisp controlling motors that are spinning mirrors to reflect a laser in very particular patterns. I’d use this on halloween decorations for starters, combining with fog machine/dry ice to create nifty patterns and make people wonder how the hell I did it. Maybe, if I have the willpower to see that through, then I’ll also hook up a USB camera (using cl-v4l2) and get lisp to track and hightlight objects, augmented-reality style. That’d be great for table-top games, being able to overlay terrain or effects on a grid mat.