First and foremost, I hope you guys have the Swift environment in place for running and programming. If not, check out these following posts:
* Step 1 – set up Raspbian OS
* Step 2 – Set up Swift environment on Raspbian
There are many ways to write a program for Raspberry Pi.
The simplest way is you will have a keyboard, a mouse, a monitor plug directly in the Raspberry board, or just simply connect it over SSH remotely. Use command-line editors like nano, vim, whatever. Create a swift file, code it, then compile and run the program. Everything can be done on terminal.
But I don’t like to be the man in stone of age. 🙂
In order to develop a complex project. I don’t think that is a good way to go. I have come up with several ideas, researched and ended up with getting XCode handshaking with Rasberry Pi.
That is amazing for guys who use a MAC. We’re able take advantage of XCode, make development more productivity, convienient and more fun. This is the goal I like to share in this post.
Note: The tutorial was done on the following environment.
* MAC: MAC OS Mojave 10.14.12, XCode 10.1
* Raspberry: Raspberry Pi 3B+, Raspbian Strech Lite OS, Swift 4.1.1
Create a project
First of all, We should know how to create a project with the help of Swift tools. I’m just showing you the steps on MAC.
On your MAC, open the terminal. Make a directory, and jump in it
Create the swift project
swift package init --type executable
Executable type creates a executable program, you can try types like empty|library|executable|system-module later
Ok, the project has been created. Now compile and run it.
You can call for compile and run separately, or just call swift run. It will automatically compile and run right after
Or you can just run the program which has been compiled by adding this option –skip-run
After all, you should see Hello World!
To keep this post short, I’m not gonna explain the directory structure which has been generated. You can learn more here
XCode project file
In order to open the project, We need a XCode project file which has .xcodeproj extension.
Simply generate the project file by the following command
swift package generate-xcodeproj
Double click to open the project in Xcode.
How to compile?
Basically, XCode cannot compile program for Raspberry on MAC OS, because of CPU arch, OS kernel, libraries and stuff. What We do here is ask Raspberry to compile code.
The mechanism includes 2 phases:
- Phase 1: Sync the source files. XCode call the command rsync. This command automatically syncs up everything to Raspberry Pi include source code and relevant files in the project folder.
- Phase 2: XCode send compiling command swift build over ssh, then Raspberry Pi takes care of building the source. In case of failure or success, the message will be sent back to XCode’s console.
In order to achieve these, We need a script to do this. I need to say thank Thomas Paulmann , He has the idea implemented and package it perfectly. It works like a boss. Check it out his project here Swish for more detail.
——> See the full setup guide of Swish here
Run this command to install Swish on your MAC
brew install thomaspaulmann/homebrew-formulae/swish
Then you need to pair your MAC with Raspberry, so that everytime you call ssh, it will not ask you again for password.
Firstly, Generate public key
ssh-keygen -t rsa
Put it into Raspberry
cat ~/.ssh/id_rsa.pub | ssh @ "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Check ssh pairing again.
Try this twice. In the second time, it should not ask you for the password.
Config Swish in XCode
In this part, We integrates Swish into XCode
Firstly, We need to add an external build system target to project. Follow steps in the following pictures
Secondly, We need to create scheme for the target We has added. Follow these steps
Now, We need to set swish with the Raspberry user and ip address in the build target
– The Swift version on Raspberry is Swift 4.1.1. So make you the swift-tools-version in Package.swift is 4.1
– The Raspberry Pi must be on, and accessible via the given user and ip. (Use ssh to check accessible)
When everything is in place, to build the project. You need to select the target “RemoteBuild”, choose the platform as a MAC (In fact, It triggers the build script to build on Raspberry Pi)
Then click play Icon to compile, or use the shortcut key Cmd + B on MAC
After all, you should see “Build succeeded” message.
Now, you connect to the Raspberry Pi by ssh.
The project locates at /home/pi/Swish/swift_program
Change to directory the project directory, and use the following command to run the project
swift run --skip-build
That is it.
At this time, I see XCode is the best candidate for Swift developement. However XCode can be so advanced for developers who are not familar. There is another option, Visual Studio Code. I will share this next time, so stay tuned
- rsync – https://www.raspberrypi.org/documentation/remote-access/ssh/rsync.md
- ssh – https://www.raspberrypi.org/documentation/remote-access/ssh/
- Unix linux execute command using ssh – https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/
- Swish – https://github.com/thomaspaulmann/Swish