Toit: Infinite resilient application loops (try “catch”)

Toit is a modern high-level language designed specifically for microcontrollers. I’m spending some time at work making use of it these days, and as it’s a younger language, there are not as many resources out there in comparison to others.

In Toit, you could write a very simple hello world application with the following code…

main:
    print "Hello world"Code language: PHP (php)

Say you wanted to get a new name from an external package to say hello to, you might do the following, which would call the method names.next, which can return a string, and print it to the user.

import somePackage show names

main:
    print "Hello world $names.next"Code language: JavaScript (javascript)

To do this in a loop every second, your code might look something like this…

import somePackage show names

main:
    while true:
        print "Hello world $names.next"
        sleep --ms=500Code language: JavaScript (javascript)

If you wanted two separate tasks running with their own control flows, you might make use of tasks, and a second loop.

Read more