The Swift Programming Language on Raspberry Pi

The Swift Programming Language on Raspberry Pi
Raspberry Pi 3 Model B+

In this manual, we will explore the basics of using Swift on the Raspberry Pi. The Raspberry Pi is a small, inexpensive single-board computer, whose potential is limited only by its computational resources. It is well-known among tech enthusiasts and DIY lovers. It's an excellent device for anyone looking to experiment with ideas or test specific concepts in practice. It can be used for a variety of projects, easily fitting into almost any space — for example, it can be mounted on a monitor lid and used as a desktop, or connected to a breadboard to control an electronic circuit.

The official programming language for the ‘berry’ is Python. While Python is quite easy to use, it lacks type safety and consumes a lot of memory. Swift, on the other hand, features ARC memory management and is nearly 8 times faster than Python. Since the RAM and computing power of the Raspberry Pi are limited, using a language like Swift allows you to make the most of this mini-PC's hardware potential.

So, we have everything necessary to install the system.

Before installing Swift, you need to choose the operating system. For that, you can use one of the options, provided by third-party developers. The most common choice is Raspbian, the official OS from Raspberry Pi. There are several ways to install Raspbian onto an SD card; in our case, we will use balenaEtcher. Here’s what to do:

The Swift Programming Language on Raspberry Pi
Step two: format the SD card to MS-DOS (FAT)

The Swift Programming Language on Raspberry Pi
Step three: use balenaEtcher to flash Raspbian onto the card

We recommend a free boot camp in machine learning for beginners:
Build your first machine learning model in three days — September 2-4. A free boot camp that helps you understand what Machine Learning is and learn to work with open data from the internet. You'll also learn to predict the dollar exchange rate using a self-developed model..

Setting Up Raspberry Pi

Halfway there! Now we have an SD card with the OS that we will be using, but we still need to install the operating system. There are two options for this:

  • Use the connected monitor, keyboard, and mouse.
  • Do everything from another PC via SSH or using a USB Console cable.

If this is your first experience with Pi, I recommend option #1. After inserting the SD card with Raspbian OS into the Pi, connect the HDMI cable, mouse, keyboard, and power cable.

The Pi should boot up when turned on. Congratulations! Now you can spend some time exploring your desktop and its capabilities.

The Swift Programming Language on Raspberry Pi

Installing Swift

To install Swift on the Raspberry Pi, it needs to be connected to the internet (either via Ethernet or WiFi, depending on the model of the board). Once the internet is connected, you can start installing Swift.

This can be done in two ways. The first is building Swift from source, the second is to use precompiled binaries. I strongly recommend the second method, as the first will require several days of preparation. The second method became available thanks to the group Swift-ARM. They own the repo from which you can install Swift using apt (Aadvanced Ppackage Ttool).

This is a command-line tool, similar to an App Store for applications and packages for Linux devices. We start working with apt by typing apt-get in the terminal. Next, you need to specify a series of commands to clarify the task to be performed. In our case, we need to install Swift 5.0.2. The corresponding packages can be found here.

Well then, let's get started. Now that we know we will install Swift using apt, we need to add the repo to the list of repositories.

Command to add/install the repo swift-arm looks like this:

curl -s  | sudo bash

The Swift Programming Language on Raspberry Pi

Next, we install Swift from the added repo:

sudo apt-get install swift5=5.0.2-v0.4

The Swift Programming Language on Raspberry Pi

That's it! Now Swift is installed on our Raspberry Pi.

Creating a test project

Currently, Swift REPL doesn't work, but everything else does. For the test, let's create a Swift package using Swift Package Manager.

First, create a directory named MyFirstProject.

mkdir MyFirstProject

The Swift Programming Language on Raspberry Pi

Next, change the current working directory to the newly created MyFirstProject.

cd MyFirstProject

The Swift Programming Language on Raspberry Pi

Create a new Swift executable package.

swift package init --type=executable

The Swift Programming Language on Raspberry Pi

These three lines create an empty Swift package called MyFirstProject. To run it, type the command swift run.

The Swift Programming Language on Raspberry Pi

Once the compilation is complete, we will see "Hello, world!" in the command line.

After we created the first program on Pi, let’s make some changes. In the MyFirstProject directory, we will modify the main.swift file. It contains the code that runs when we execute the package with the command swift run.

Change the directory to Sources/MyFirstProject.

cd Sources/MyFirstProject 

The Swift Programming Language on Raspberry Pi

Edit the main.swift file using the built-in nano editor.

nano main.swift

The Swift Programming Language on Raspberry Pi

Once the editor is open, you can modify the code of your program. Let’s replace the contents of the main.swift file with the following:

The Swift Programming Language on Raspberry Pi

print("Hello, Marc!")

Of course, you can insert your own name. To save changes, follow these steps:

  • CTRL+X to save the file.
  • Confirm changes by pressing 'Y'.
  • Confirm the modification of the main.swift file by pressing Enter.

The Swift Programming Language on Raspberry Pi

The Swift Programming Language on Raspberry Pi

All changes have been made, now it’s time to restart the program.

swift run

The Swift Programming Language on Raspberry Pi

Congratulations! Once the code is compiled, the terminal should show the modified line.

Now that Swift is installed, you have something to work on. To control hardware, such as LEDs, servos, relays, you can use the library for 'hardware' projects for Linux/ARM boards, which is called SwiftyGPIO.

Good luck experimenting with Swift on Raspberry Pi!

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster