Why Coding for Raspberry Pi Is Way Better With Code-OSS

raspi-code-oss

No matter if you are learning the first steps of programming, or are a seasoned developer, you are going to look at a lot of code. A good code editor is a must, but the options for the Raspberry Pi are limited.

Visual Studio Code is a free integrated development environment (IDE) for Windows, Mac, and Linux. It’s not available for Raspberry Pi. However, since it is open source, anyone can compile a version.

Below you’ll learn how to install Code-OSS, a community compiled version of VS Code, on the Raspberry Pi.

Why Do I Need an IDE?

The Raspbian Stretch operating system (OS) comes with a number of code editors already installed. You might be wondering why bother installing another one?

Code-OSS is based on VS Code and is more than just a code editor. Most IDEs have built-in explorers for managing whole project folders rather than individual scripts. Many support auto-completion of code and have terminals built in for code testing. Several also support version control and dependency management for packages.

In short, IDEs make your life easier. Exactly which one you choose is personal preference.

Why Install Code-OSS?

All of those reasons why IDEs are great? Code-OSS can do them. As it is essentially a fully functional version of VS Code under another name, it likely has the most features of any coding tool available currently on the Pi.

A Raspberry Pi - the swiss army knife of mini computers

This isn’t to say it is perfect. The Pi suffers from its success, and older models of the Pi may struggle with performance while running Code-OSS. This tutorial uses a Raspberry Pi 3B+, which runs the program with no problems.

Why Not Install VS Code?

Given the fact that Code-OSS is the same as VS Code, which is maintained by Microsoft, why not simply install VS Code?

Currently, there is no official release of VS Code for ARM devices like the Raspberry Pi. While this may change in future, Code-OSS is your best bet for now!

How to Get Code-OSS

Image of the Headmelted site for linux and chromebook builds of VS Code

Code-OSS, built by Jay Rodgers, is available at his headmelted GitHub page.

He also maintains the headmelted website with installation instructions. You will need both of these sites open in your browser to continue, along with a terminal window.

Installing the GPG key

The terminal output for adding the GPG key for Code-OSS's install
Currently, installing Code-OSS for Pi takes some added steps not listed on the headmelted website. Scroll down to the Linux section, and look under Manual Installation.

Here you will find a link to a public GPG key. Use this key in the terminal, with the following command:

wget -o - https://packagecloud.io/headmelted/codebuilds/gpgkey| sudo apt-key add -

Note the spacing here, as it’s essential to get it right! This command downloads a key which is part of the Gnu Privacy Guard (GPG) and adds it to your system. This doesn’t install Code-OSS, it just ensures that when the program downloads, it will install correctly.

Installing Code-OSS

Currently, following the instructions on the headmelted website results in a successful installation, but the program won’t run. The fix for this bug may come in time, but for now, the answer lies in installing an earlier version of Code-OSS.

Enter this code in the terminal:

sudo apt-get install code-oss=1.29.0-1539702286

This version will run, but every time your system updates it will break again. The way around this is available in an issue on the Github page for the project. By marking Code-OSS with hold using the terminal, it will not update automatically.

apt-mark hold code-oss

Now it is held to the working version. Change this at any time by rerunning the command, replacing hold with unhold.

Introducing Code-OSS

The welcome screen for VS code, or in this case Code-OSS.

Open Code-OSS in the Raspberry Pi’s application menu. If you’ve used VS Code before, this should look very familiar. Other than the name, the program looks and behaves the same.

On the left side is a toolbar for opening folders, searching within projects, version control, debugging and extensions. Covering all of these features is beyond the scope of this article. For now, let’s install the Python extension.

Click the box logo on the left toolbar to open the extensions menu. Code-OSS shares VS Codes extensive library of add-ons and helpers for almost every conceivable type of code, language, or project.

Search for Python in the menu, and click install. Once it has installed, you will need to reload Code-OSS.

Testing Out Code-OSS

To test out the extension, let’s make a quick Python script for controlling the GPIO pins. Setting up an LED with your Pi for this test is entirely optional, and I won’t cover the circuit diagram here. If you want to follow along, our guide on using LEDs with the Pi will help you get set up.

Enter this code, and notice the ways Code-OSS helps you as you do it:

from gpiozero import LED
from time import sleep

led = LED(17) #Change this to your LED's GPIO pin number!

while True:
 print("LED on")
 led.on()
 sleep(1)
 print("LED off")
 led.off()
 sleep(1)

The Python extension provides code completion along with live code checking. Any errors should highlight in real time.

It works! Kind of.

You’ll probably notice a couple of errors in the bottom right-hand side of the screen.

Python Extension errors in Code-OSS

If you use pylint you’ll be familiar with this error. The usual fix is to install the correct version of pylint for your Python install. The other error is down to the fact that the Python Language Server currently has no support on the Pi. This isn’t a problem, as it defaults to using Jedi instead, which sounds way cooler.

If you don’t understand what this means, you can safely ignore it!

Everything in One Place

Installing Code-OSS doesn’t just make editing code easier. Code-OSS has a terminal built right into the program. You can open the terminal by selecting View > Terminal or pressing Ctrl + `.

The built-in, fully functional terminal in Code-OSS

Alternatively you can run any Python script from the command palette. Opening it in Code-OSS is the same as in VS Code. Press Ctrl + Shift + P to open the command palette and search for Python: Run Python File in Terminal.

The Code-OSS command palette, running Python code in the built-in terminal

Once you select it, the terminal will run your program, all within Code-OSS. Being able to access all of your code and a terminal in once place is a game changer on the Raspberry Pi!

A Big Upgrade for Raspberry Pi Coders

This is definitely a significant upgrade. That isn’t to say that the tools already on the Pi aren’t good enough, in fact, we used the pre-installed IDLE code editor for our Raspberry Pi LED control tutorial, and it was perfectly fine.

It is definitely nice to have something more fully featured though. Code-OSS can do much more than could be covered here. Why not spend some time brushing up on your coding on the Raspberry Pi to get to grips with Code-OSS?

Read the full article: Why Coding for Raspberry Pi Is Way Better With Code-OSS



from MakeUseOf http://bit.ly/2U3EAC1
via IFTTT
Share on Google Plus

About Oyetoke Toby

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment