未加星标

Setting up a Swift development environment on Raspberry Pi

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Setting up a Swift development environment on Raspberry Pi
Image taken from raspberrypi.org

In my previous post I gave an overview of the current state of Swift on the Raspberry Pi platform. In this post I will try highlight how to setup a pleasant development environment to write Swift code on the Pi.

One of the biggest pains right now when writing Swift code on any linux platform is the lack of a real IDE. Sure, you may hate Xcode but go start typing your program in Nano on the command line and you will see how real misery feels.


Setting up a Swift development environment on Raspberry Pi
You get a lot of these since you do not have compiler warnings at writetime

The first step for a good development experience is a good IDE and the best Swift IDE for linux right now is…. Xcode.

What!? But Xcode does not work on Linux. How do I install it on the Pi? Well you dont, and frankly you would not want to write code on the Pi. It is a slow computer and running it with a monitor and keyboard is uneccesary. So where do I write my code? On your Mac of course, where else dummy.

First thing you need to do is setup a headless Pi. There is a really nice tutorial on how to do that on the Raspberry forums . The tutorial is written for Raspian and I recommend using Ubuntu for the Pi since the SPM works there. I first runned my Pi with a keyboard and monitor and did the initial setup and connected it to my WiFi network. Once you have the Pi connected to your router you can start controlling it by SSH from your mac. First you need to find the IP address of your Pi on the network. I do that using nmap which scans devices in your network that listed on port 22 (ssh port).


Setting up a Swift development environment on Raspberry Pi

To find your Pi, you need to replace the 192.168.6.0 part with your local address space. Once you have it, connect your mac through ssh.

ssh {your-pi-username}@{you-pi-ip-address}

And there you go, sky is the limit now. Try to build and run your first Swift script remotely on the Pi.

// Create the script echo "print(\"hello from swift\")" >> hello.swift // Run it swift hello.swift

This is all fine and dandy but what are you still doing on the command line? Go fire up Xcode and begin some serious development!

We will create a program that will switch on and off an LED connected to the Pi if you press the return key on your Mac. To switch nicely between the two environments we will use the Swift Package Manager , which is a system agnostic dependency manager for Swift. It runs fine both on OS X and on Linux and it will be a vital part that will enable us to develop code on the Mac and run it on the Pi.

// create the project folder mkdir led-blink // go to the folder cd led-blink // initialize the swift project using SPM swift package init --type executable

You should now have the directory structure and main.swift file generated by SPM. Edit the Package.swift to include the GPIO swift library .

import PackageDescription let package = Package( name: "led-blink", dependencies: [ .Package(url: "https://github.com/uraimo/SwiftyGPIO.git", majorVersion: 0) ] )

Now fetch the library using the command:

swift package update

You can now use SPM to generate an Xcode project for you! Do this using the command:

swift package generate-xcodeproj

Open the generated xcode project and build it (cmd+B), so the compiler knows about the SwiftyGPIO library. Voila, you can now use Xcode to develop your program, having access to all those sweet tools like code completion, syntax highlighting, compiler errors, documentation and more.

Open the main.swift file and add the following code:

import SwiftyGPIO import Foundation // Get a dictionary of all the gpio pins let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi2) // Get the pin that you connected the LED to. Remember to set the right pin number, for me it was 27 guard let ledGpio = gpios[GPIOName.P27] else { fatalError("Could not initialize the gpio") } // Set the pin direction to .OUT and turn it off ledGpio.direction = .OUT ledGpio.value = 0 // Read user input from keyboard and switch the LED on/off each time the user presses the return key print("Press return to switch the LED on/off. To quit type exit") while let userInput = readLine(strippingNewline: true), userInput != "exit" { print("Switching LED") ledGpio.value = ledGpio.value == 0 ? 1 : 0 }

Ok, the program is completed. The only thing left to do is to build and run it on the Pi. To do this, you will need to copy the project you created on your mac to the Pi. There are a couple of solutions how you might do it. For one, you could create a GIT repository and host it on Github. Then you would push the changes from your mac, and pull them on the Pi. Another option is to use SCP to copy the folder.

scp -r {path-to-project-folder-on-mac} {your-pi-username}@{you-pi-ip-address}:{path-to-folder-on-pi}

Once you copied the project, you can ssh to the Pi and go to the project folder. The folder has all the files there but you still have to build it using the compiler on the Raspberry.

swift build

Should create an executable which you can then run and marvel at your work! Here is the masterpiece I’ve created in action.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: SwiftRaspberry PiLinuxGitUbuntuUTWiFi
分页:12
转载请注明
本文标题:Setting up a Swift development environment on Raspberry Pi
本站链接:http://www.codesec.net/view/534015.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(15)