Raspberry Pi 2 GPIO with SwiftyGPIO


Working with open source developers worldwide is a fun and rewarding experience. As the cost of computing devices and broadband Internet continues to fall and bring new technologies to people around the globe, developers of all stripes from different cultures and backgrounds come together to collaborate and build awesome things.

Since Apple open sourced the Swift programming language late last year enthusiasts have created Ubuntu packages, ported it to ARM devices such as the Raspberry Pi 2, built web development frameworks, and now Umberto Raimondi has released SwiftyGPIO, a Swift library for interacting with GPIO pins on ARM devices such as the Raspberry Pi and BeagleBone Black.

The SwiftyGPIO README covers all the bases explaining how to use the module. As Umberto notes the Swift Package Manager is currently unavailable in the ARM builds (I’ve been working on getting it to compile but something always preempts me) so we turn to downloading the SwiftyGPIO.swift file with wget and using swiftc to compile and link everything together.

Rock Chalk

Last year I was working with Arduino programming with Xcode and wrote up some Arduino code for blinking LEDs. Let’s do the same with a Raspberry Pi 2 and Swift.

If you’re coming into this cold you’re going to need:

We’re going to use GPIO4 and GPIO27 since they are close to each other on the Pi 2 GPIO header.

Here is our main.swift to blink the two lights back and forth:

To compile and run this code, until SwiftPM for ARM is fixed, we do this:

# wget https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift
# swiftc main.swift SwiftyGPIO.swift
# ./main

If you wired your LEDs up properly they should be flashing back and forth!

Blinky Blinky
Blinky Blinky

Pick Your Color

I had a Linrose Tricolor LED lying around so I decided to put it to good use with Swift. In this code example we’ve written a command line application that lets you set the color of the LED (or to turn it off). I’ve marked the code with // 1, // 2 to describe each section.

1. SwiftyGPIO provides canned GPIO definitions for popular board types. In our case we’re using a Raspberry Pi 2.

2. This is purely syntactic sugar to describe GPIO states as On or Off. The code would probably look less cluttered if we removed this.

3. LedColor is a structure to “namespace” definitions for Off, Green, Orange, and Red.

4. The tricolor LED has two anode pins; we will attach one pin to GPIO4 and the other to GPIO27. The application always starts by setting the pin direction to .OUT and Off. Again, because of the way we created an enum for the GPIOState we have to use the .rawValue nonsense.

5. setLedColor takes a tuple of (GPIOState,GPIOState) and [GPIO] array to set the pair of GPIO pins to a certain state.

6. Our application takes a single argument so we guard that we have two (one is the application name). Our color is the second.

7. A switch on the color and a call is made to setLedColor with the appropriate color tuple and GPIO set.

Closing Remarks

SwiftyGPIO is a great API to get started with GPIO manipulation on ARM boards with Swift. Each passing day Swift makes inroads into the maker community and stands a great chance to be the language of choice for single board computer development projects.

Leave a Reply

Your email address will not be published. Required fields are marked *