Hello world

This tutorial shows how to create a simple "Hello world" application and how to run it on a Jaguar-enabled device.

Prerequisites

We assume that you have set up your development environment as described in the IDE tutorial.

Creating the project

While not strictly necessary we recommend starting new projects in a separate folder. Toit stores package information in a hidden folder and it is easier to keep track of things if you have one folder per project. We are not going to use packages in this tutorial yet, but it's a good habit to get into.

Once you have created a new folder (say hello) open it in Visual Studio Code: File -> Open Folder.

The code

In your project folder create a new file called hello.toit and add the following code:

main:
  print "Hello world"

Make sure to save it (or change the Visual Studio Code files.autoSave setting to onFocusChange).

Spaces are important in Toit, so make sure that the print line is indented compared to the main: line. Generally, we write nested things with two more spaces of indentation. If you have installed the Toit extension, then Visual Studio Code should already do this for you.

Running the code

We are now ready to run the code. As described in the IDE tutorial we typically have two terminals open: one where we interact with the device, and one where we monitor the output of the device. In this setup, run jag run hello.toit in the first terminal.

Screenshot of running the hello world program
Screenshot of running the hello world program

Within a fraction of a second Jaguar compiled the program and ran it on the device. The terminal monitoring the serial port shows the output of the program.

Let's change the program to emit a different output:

main:
  print "hi everyone"

Save it and run jag run hello.toit again. The new output should be visible on the monitor terminal.

Watching a Toit file

Jaguar has a nice functionality that automatically runs applications as soon as they are saved: jag watch <file> watches a file for changes and immediately runs it as soon as a new version has been saved.

Run jag watch hello.toit in the first terminal. Then change the program and save it. Without having to execute jag run again the new version should be running on the device.

Screenshot of Jaguar watching the hello world program
Screenshot of Jaguar watching the hello world program

Use ctrl+c to stop the watching. You will want to do this when you are done, or when you want to watch a different file.

Next steps

Congratulations! You have successfully created and run your first Toit application.

You can now move on to the package tutorial to learn how Toit packages work, or directly jump to the LED tutorial to learn how to blink an LED.