In this tutorial, we will cover the basics of using Swift on the Raspberry Pi. The Raspberry Pi is a small and inexpensive single board computer that is limited only by its computing resources. It is well known to technologists and DIY enthusiasts. This is a great device for those who need to experiment with an idea or test a certain concept in practice. It can be used for a wide range of projects and can be easily placed almost anywhere - for example, it can be mounted on the top of a monitor and used as a desktop, or connected to a breadboard to control an electronic circuit.
The official raspberry programming language is Python. While Python is fairly easy to use, it lacks type safety and consumes a lot of memory. Swift, on the other hand, has ARC memory management and is almost 8 times faster than Python. And since the amount of RAM and computing capabilities of the Raspberry Pi processor is limited, using a language like Swift allows you to maximize the potential of the hardware of this mini PC.
OS installation
Before installing Swift, you need to choose an OS. For this you can use one of the optionsprovided by third party developers. The most common choice is Raspbian, the official OS from the Raspberry Pi. There are several possibilities to install Raspbian on an SD card; in our case, we will use balenaEtcher. Here's what to do:
Use balenaetcher for uploading to a formatted card.
Step Two: Format the SD Card in MS-DOS (FAT)
Step three: use balenaEtcher to upload Raspbian to the map
We recommend a free machine learning intensive for beginners: Writing the first machine learning model in three days - September 2-4. A free intensive that allows you to understand what Machine Learning is and learn how to work with open data from the Internet. We also learn how to predict the dollar exchange rate using a self-developed model.
Raspberry Pi Setup
Halfway already passed! We now have an SD card with the OS we will be using, but the operating system has yet to be installed. There are two possibilities for this:
Use the monitor, keyboard and mouse connected to the device.
Do everything from another PC via SSH or using the USB Console cable.
If this is your first experience with the Pi, I recommend option #1. Once the Raspbian OS SD card is inserted into the Pi, connect the HDMI cable, mouse, keyboard, and power cable.
The Pi should boot up when you turn it on. Congratulations! Now you can spend some time learning about your desktop and its capabilities.
Installing Swift
In order to install Swift on the Raspberry, you need to connect it to the Internet (using Ethernet or WiFi, depending on the board model). Once the internet is connected, you can start installing Swift.
It can be done in two ways. First - building your own Swift build, the second is to use already compiled binaries. I strongly recommend the second method, since the first will require several days of preparation. The second way appeared thanks to the group Swift-ARM. She owns a repo from which you can install Swift using apt (Advanced Packnowledgment TOol).
It's a command line tool, kind of like the App Store for apps and packages for Linux devices. We start working with apt by typing apt-get in the terminal. Next, you need to specify a number of commands that will clarify the task to be performed. In our case, we need to install Swift 5.0.2. The corresponding packages can be find here.
Well, let's start. Now that we know we'll be installing Swift with apt, we need to add the repo to the list of repositories.
Add/Install repo command swift arm looks like that:
That's all! Now Swift is installed on our "raspberry".
Create a test project
At the moment Swift REPL doesn't work, but everything else does. As a test, let's create a Swift package using the Swift Package Manager.
First we create a directory named MyFirstProject.
mkdir MyFirstProject
Next, change the current working directory to the newly created MyFirstProject.
cd MyFirstProject
We create a new executable Swift package.
swift package init --type=executable
These three lines create an empty Swift package called MyFirstProject. To run it, type in the swift run command.
Once compilation is complete, we will see the phrase "Hello, world!" on the command line.
Now that we've created our first Pi program, let's change a few things. In the MyFirstProject directory, we will make changes to the main.swift file. It contains the code that is executed when we run the package with the swift run command.
Change directory to Sources/MyFirstProject.
cd Sources/MyFirstProject
Editing the main.swift file using the built-in nano editor.
nano main.swift
Once the editor is open, you will be able to modify the code of your program. Let's replace the contents of the main.swift file with this:
print("Hello, Marc!")
Of course, you can put in your name. To save changes, you need to do the following:
CTRL+X to save the file.
Confirm changes by pressing "Y".
We confirm the change in the main.swift file by pressing Enter.
All changes have been made, now it's time to restart the program.
swift run
Congratulations! Once the code is compiled, the terminal should show the modified line.
Now that Swift is installed, you have a lot to do. So, to control hardware, for example, LEDs, servos, relays, you can use the library of "iron" projects for Linux / ARM boards called SwiftyGPIO.
Good luck experimenting with Swift on your Raspberry Pi!