A Racket interface to WiringPi

:: Pi, GPIO, hardware

I’ve made a start on a Racket interface to Gordon Henderson’s WiringPi C library, together with a first example of using it—driving the hardware of the ‎4tronix PiDie GPIO board. Hoping this will be useful for future projects.

*Racket accessing GPIO on Pi*

Racket accessing GPIO on Pi

In order to use this library, you’ll need an installation of Racket, described in an earlier post; Racket on the Raspberry Pi.

You will also need to install WiringPi on your Pi as described here. And for the example, you’ll need a PiDie, though looking through the source, you’ll be able to see the basics of driving the Pi’s GPIO and adapt this to any other hardware you have at hand.

Clone the wiringpi-racket code at https://github.com/stevelloyd/wiringpi-racket. There are just a handful of files, the interface is contained in wiringpi/wiringpi.rkt. A hardware folder has an example for the PiDie. This lights up each LED in turn, then waits for buttons to be pressed, and lights up the corresponding colour LEDs.

cd ~/wiringpi-racket/hardware
sudo ~pi/racket/bin/racket pidie.rkt  

(You need to run as sudo in order to access the GPIO hardware of the Pi with the example setup).

And the result is here.

If you examine wiringpi.rkt you will see a bunch of define expressions; these provide the definitions of WiringPi’s external functions so that they can be called by Racket. (This is the first Foreign Function Interface code I’ve done in Racket, and it seems straightfoward to map the WiringPi calls—Racket FFI docs). Then there are a set of constants which provide the values for various hardware configuration.

There’s more work to be done on the library (particularly thinking how to do tests), but I can start to use this in some projects now. (Not the least because Racket provides fast manipulation of the hardware). More fun ahead…