{"id":2528,"date":"2016-01-16T19:39:40","date_gmt":"2016-01-17T01:39:40","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=2528"},"modified":"2016-01-17T08:21:58","modified_gmt":"2016-01-17T14:21:58","slug":"raspberry-pi-2-gpio-with-swiftygpio","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/raspberry-pi-2-gpio-with-swiftygpio\/","title":{"rendered":"Raspberry Pi 2 GPIO with SwiftyGPIO"},"content":{"rendered":"<p>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.  <\/p>\n<p>Since Apple open sourced the Swift programming language late last year enthusiasts have <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/ubuntu-packages-for-open-source-swift\/\">created Ubuntu packages<\/a>, <a href=\"http:\/\/www.housedillon.com\/?p=2287\">ported it to ARM devices such as the Raspberry Pi 2<\/a>, built <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/building-rest-apis-with-zewo\/\">web development frameworks<\/a>, and now <a href=\"https:\/\/www.uraimo.com\/\">Umberto Raimondi<\/a> has released <a href=\"https:\/\/github.com\/uraimo\/SwiftyGPIO\">SwiftyGPIO<\/a>, a Swift library for interacting with GPIO pins on ARM devices such as the Raspberry Pi and BeagleBone Black.<\/p>\n<p>The <a href=\"https:\/\/github.com\/uraimo\/SwiftyGPIO\/blob\/master\/README.md\">SwiftyGPIO README<\/a> 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&#8217;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.<\/p>\n<h3>Rock Chalk<\/h3>\n<p>Last year I was working with <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/getting-started-with-arduino-and-xcode\/\">Arduino programming with Xcode<\/a> and wrote up some Arduino code for blinking LEDs.  Let&#8217;s do the same with a Raspberry Pi 2 and Swift.<\/p>\n<p>If you&#8217;re coming into this cold you&#8217;re going to need:<\/p>\n<ul>\n<li>a Raspberry Pi 2\n<li>a couple of LEDs\n<li>wires for wiring\n<li><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/open-source-swift-on-raspberry-pi-2\/\">Swift installed on your Pi 2<\/a>\n<\/ul>\n<p>We&#8217;re going to use GPIO4 and GPIO27 since they are close to each other on the Pi 2 GPIO header.<\/p>\n<p>Here is our `main.swift` to blink the two lights back and forth:<\/p>\n<pre class=\"lang:swift\">\r\nimport Glibc\r\n\r\nlet gpios = SwiftyGPIO.getGPIOsForBoard(.RaspberryPiPlus2Zero)\r\n\r\n\/\/ GPIO4 and GPIO27\r\nlet leds = [gpios[.P4]!, gpios[.P27]!]\r\n\r\n\/\/ Initialize our GPIOs\r\nfor led in leds {\r\n  led.direction = .OUT\r\n  led.value     = 0\r\n}\r\n\r\n\/\/ Blink\r\nwhile true {\r\n  for led in leds {\r\n    led.value = 1\r\n    sleep(1)\r\n    led.value = 0\r\n  }\r\n}\r\n<\/pre>\n<p>To compile and run this code, until SwiftPM for ARM is fixed, we do this:<\/p>\n<pre class=\"crayon:false\">\r\n# wget https:\/\/raw.githubusercontent.com\/uraimo\/SwiftyGPIO\/master\/Sources\/SwiftyGPIO.swift\r\n# swiftc main.swift SwiftyGPIO.swift\r\n# .\/main\r\n<\/pre>\n<p>If you wired your LEDs up properly they should be flashing back and forth!<\/p>\n<p><insert picture><\/p>\n<figure id=\"attachment_2532\" aria-describedby=\"caption-attachment-2532\" style=\"width: 457px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/01\/SwiftLEDs.png\" rel=\"attachment wp-att-2532\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/01\/SwiftLEDs.png\" alt=\"Blinky Blinky\" width=\"457\" height=\"440\" class=\"size-full wp-image-2532\" \/><\/a><figcaption id=\"caption-attachment-2532\" class=\"wp-caption-text\">Blinky Blinky<\/figcaption><\/figure>\n<h3>Pick Your Color<\/h3>\n<p>I had a <a href=\"http:\/\/www.amazon.com\/Linrose-B4361H1-Green-Amber-Tricolor\/dp\/B008K1SWEC\">Linrose Tricolor<\/a> LED lying around so I decided to put it to good use with Swift.  In this code example we&#8217;ve written a command line application that lets you set the color of the LED (or to turn it off).  I&#8217;ve marked the code with `\/\/ 1`, `\/\/ 2` to describe each section.<\/p>\n<pre class=\"lang:swift\">\r\nimport Glibc\r\n\r\n\/\/ 1\r\nlet gpiodefs = SwiftyGPIO.getGPIOsForBoard(.RaspberryPiPlus2Zero)\r\n\r\n\/\/ 2\r\nenum GPIOState:Int {\r\ncase Off = 0\r\ncase On \r\n}\r\n\r\n\/\/ 3\r\nstruct LedColor {\r\n static let Off    = (GPIOState.Off, GPIOState.Off) \r\n static let Green  = (GPIOState.On,  GPIOState.Off)\r\n static let Orange = (GPIOState.On,  GPIOState.On)\r\n static let Red    = (GPIOState.Off, GPIOState.On)\r\n}\r\n\r\n\/\/ 4\r\nlet gpios = [gpiodefs[.P4]!, gpiodefs[.P27]!]\r\nfor gpio in gpios {\r\n  gpio.direction = .OUT\r\n  gpio.value     = GPIOState.Off.rawValue\r\n}\r\n\r\n\/\/ 5\r\nfunc setLedColor(color:(GPIOState,GPIOState), gpios:[GPIO]) {\r\n  gpios[0].value = color.0.rawValue\r\n  gpios[1].value = color.1.rawValue\r\n}\r\n\r\n\/\/ 6\r\nguard Process.arguments.count == 2 else {\r\n  print(\"Usage:  .\/main off|green|orange|red\")\r\n  exit(0)\r\n}\r\n\r\nlet color = Process.arguments[1]\r\n\r\n\/\/ 7\r\nswitch color {\r\n  case \"off\":\r\n    setLedColor(LedColor.Off, gpios:gpios)\r\n  case \"green\":\r\n    setLedColor(LedColor.Green, gpios:gpios)\r\n  case \"orange\":\r\n    setLedColor(LedColor.Orange, gpios:gpios)\r\n  case \"red\":\r\n    setLedColor(LedColor.Red, gpios:gpios)\r\n  default:\r\n    print(\"Invalid color\")\r\n}\r\n<\/pre>\n<p><b>1.<\/b> `SwiftyGPIO` provides canned GPIO definitions for popular board types.  In our case we&#8217;re using a Raspberry Pi 2.<\/p>\n<p><b>2.<\/b> This is purely syntactic sugar to describe GPIO states as `On` or `Off`.  The code would probably look less cluttered if we removed this.<\/p>\n<p><b>3.<\/b> `LedColor` is a structure to &#8220;namespace&#8221; definitions for `Off`, `Green`, `Orange`, and `Red`.<\/p>\n<p><b>4.<\/b> 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.<\/p>\n<p><b>5.<\/b> `setLedColor` takes a tuple of `(GPIOState,GPIOState)` and `[GPIO]` array to set the pair of GPIO pins to a certain state.<\/p>\n<p><b>6.<\/b>  Our application takes a single argument so we `guard` that we have two (one is the application name).  Our color is the second.<\/p>\n<p><b>7.<\/b>  A switch on the color and a call is made to `setLedColor` with the appropriate color tuple and GPIO set.<\/p>\n<h3>Closing Remarks<\/h3>\n<p><a href=\"https:\/\/github.com\/uraimo\/SwiftyGPIO\">SwiftyGPIO<\/a> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,13,5],"tags":[],"class_list":["post-2528","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2528"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=2528"}],"version-history":[{"count":14,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2528\/revisions"}],"predecessor-version":[{"id":2543,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2528\/revisions\/2543"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=2528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=2528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=2528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}