Script Editor

The Script Editor is our web-based editor and compiler, allowing to write plugins that will run on the Air Lab, utilizing its sensors, screen and input buttons. Moreover, through the ports on the back, external sensors and other components can be connected.

Programming Language

The Script Editor is based on Go (or Golang), a high-level C-like programming language. For complete documentation and syntax, please refer to The Go Programming Language Specification, or Go by Example for a hands-on introduction.

Documentation

The Script Editor has built-in functions that allow quick access to its sensor and inputs, alongside drawing functions such as the Rect() or Line(), and the Beep() function to play sound through the built-in buzzer.

The full documentation can be found on the right-hand side of the Script Editor.

Setup

The Script Editor is part of the Studio and can be accessed from the menu on the left side in the Air Lab section.

Each newly created script contains a template script with a few basic lines of code that, when compiled, display the current temperature. The steps below show how to create a new script and run the template code:

  1. Create a new script by clicking New Script on the left side.
  2. Give it a Title and Name. The title will appear in the editor and the device, while the name will be the unique url of that script (therefore, it must be low caps and without spaces).
  3. Leave it unpublished and click Create.
  4. On the right-hand side, click Compile.
  5. Then, upload your script by clicking Run.
  6. Connect your Air Lab (only needed once per session).
  7. Wait for the upload to finish.

The plugin should now be running and showing the current temperature.

In case the plugin did not launch automatically, you can access through the Lab and run it:

  1. Navigate to the Plugins p program on Prof. Robin's computer.
  2. Press A to enter.
  3. Locate your plugin from the list and run it by pressing A again.

Template Script Dive

Let's break down the code inside of func main(), bit by bit:

package main

func main() {
    for {
        // clear screen
        Clear(White);
            
        // lookup info
        tmp := Lookup(SensorTemperature)
        
        // format string
        str := Format("Temp: %.2f C", tmp)
        
        // write string
        Write(Width/2, Height/2-8, 0, Pixel16, Black, str, AlignCenter);
         
        // wait for input or timeout after 1s    
        event := Yield(1000)
        
        // handle escape
        if event == Escape {
            return
        }
    }
}        

The logic of the script is wrapped by a for loop. In this loop, each line is executed one after the other, for as long as the plugin runs on the Air Lab.

Clear (White)

The Clear() function erases the Air Lab's screen at the beginning of each iteration, in preparation for new graphics to be drawn in the next code lines. The Clear() function takes one color argument: White, Black, or None.

tmp := Lookup(SensorTemperature)

The Lookup() function retrieves device information such as sensor readings, battery status, and storage data. It has one parameter, Info, which specifies what type of information to fetch. The full list of available arguments can be found under Info on the right-hand side of the Script Editor. In this case, the function looks up the temperature using the argument SensorTemperature.

The result of the Lookup() function is stored in a variable named tmp. In Go, variables are declared using the syntax name := value, where name is the variable's name and value is the data assigned to it. The := operator both declares and initializes the variable in one step.

str := Format("Temp: %.2f C", tmp)

This line formats the text using the Format() function and stores it in a variable. This function supports a limited set of format specifiers: %d, %x, %f, %.Nf, and %s. In this example, the string "Temp" is followed by %.2f, which formats the value as a floating-point number with two digits after the decimal point. The variable tmp provides the value that replaces the %.2f placeholder in the formatted string.

Write(Width/2, Height/2-8, 0, Pixel16, Black, str, AlignCenter)

The Write() function draws the text on the screen. It has seven parameters:

Task: Change the Yield() Timeout to 10000 and notice how the temperature updates only every 10 seconds.

event := Yield(1000)

This line creates a variable that stores the input event returned by Yield(). The function takes a single argument, a timeout in milliseconds, during which the script pauses and yields control back to the Air Lab's operating system.

If you omit the Yield() function, the script will run in an infinite loop without any pause, which can lead to high CPU usage and unresponsiveness. By including Yield(), you allow the Air Lab to manage resources effectively and respond to user inputs.

if event == Escape {
    return
}

This bit checks whether the variable event equals the predefined input constant Escape (a press on the B button of the Air Lab) if so, the function stops executing and returns, effectively exiting the plugin.

The full lists of the predefined input constants can be found in the documentation, under Event.

Physical Computing

Additional sensors and other components can be connected through the Air Lab's extension ports, either via I2C or via the 2 GPIOs (General-propose input/output).

The GPIOs can be configured as analog, PWM, or digital I/O, using the pin configuration functions:

Back to Top