Swift programming language on Raspberry Pi



Raspberry PI 3 Model B +



In this manual, we will cover the basics of using Swift on the Raspberry Pi. Raspberry Pi is a small and inexpensive single-board computer, the potential of which is limited only by its computing resources. He is well known to techies and DIY enthusiasts. This is a great device for those who need to experiment with an idea or test a specific concept in practice. It is used for a wide variety of projects, easily fits almost anywhere - for example, it can be mounted on the monitor cover and used as a desktop, or connected to a breadboard to control an electronic circuit.



The official Raspberry programming language is Python. Although Python is fairly easy to use, it lacks type safety, plus it consumes a lot of memory. Swift, by contrast, has ARC memory management and is almost 8 times faster than Python. Well, since the RAM and computing capabilities of the Raspberry Pi processor are limited, using a language like Swift allows you to maximize the hardware potential of this mini-PC.



OS installation



Before installing Swift, you need to select the OS. To do this, you can use one of the options offered by third-party developers. The most common choice is Raspbian, the official OS from Raspberry Pi. There are several options for installing Raspbian on an SD card; in our case, we will use balenaEtcher. Here's what you need to do:







Step two: format the SD card in MS-DOS (FAT)





Step Three: use balenaEtcher to upload Raspbian to the map



Recommended free intensive machine learning for beginners:

We are writing the first machine learning model in three days - September 2-4. 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 to predict the dollar exchange rate using our own developed model .

Configure Raspberry Pi



Halfway already passed! Now we have an SD card with an OS that we will use, but the operating system has yet to be installed. There are two possibilities for this:





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



Pi should boot at startup. Congratulations! Now you can spend some time exploring your desktop and its capabilities.







Install Swift



In order to install Swift on the “raspberry”, you need to connect it to the Internet (using Ethernet or WiFi, depending on the model of the board). Once the Internet is connected, you can begin installing Swift.



It can be done in two ways. The first is to independently create the 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 method appeared thanks to the Swift-ARM group. She owns a repo from which you can install Swift using apt ( A Advanced P ackage T ool ).



It is a command line tool, something like the App Store for applications and packages for Linux devices. We start working with apt by introducing apt-get in the terminal. Next, you need to specify a number of commands that will clarify the task being performed. In our case, you need to install Swift 5.0.2. Relevant packages can be found here .



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



The add / install repo swift-arm command looks like this:



curl -s <https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh> | sudo bash
      
      









Next, install Swift from the added repo:



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









That's all! Now Swift is installed on our "raspberry".



Create a test project



Swift REPL is not working right now, but everything else is yes. For the test, let's create a Swift package using the Swift Package Manager.



First, create a directory called MyFirstProject.



 mkdir MyFirstProject
      
      









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



 cd MyFirstProject
      
      









Create a new Swift executable package.



 swift package init --type=executable
      
      









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







Once compilation is complete, we will see the phrase “Hello, world!” On the command line.



After we created the first program on Pi, let's change something. In the directory MyFirstProject, make changes to the main.swift file. It contains code that executes when we run the package with the swift run command.



Change the directory to Sources / MyFirstProject.



 cd Sources/MyFirstProject
      
      









We edit the main.swift file using the built-in nano editor .



 nano main.swift
      
      









Once the editor is open, you can change the code of your program. Let's replace the contents of the main.swift file with this:







 print("Hello, Marc!")
      
      





Of course, you can insert your name. To save changes you need to perform the following actions:













All changes are made, now it's time to restart the program.



 swift run
      
      









Congratulations! As soon as the code is compiled, the terminal should show the changed line.



Now that Swift is installed, you have something to do. So, to control the hardware, for example, LEDs, servo drives, relays, you can use the library of “iron” projects for Linux / ARM boards called SwiftyGPIO .



Good luck experimenting with Swift on your Raspberry Pi!



All Articles