Creating Your First GRiSP Application

Published 8 November 2017, Updated 24 May 2018
re> A Grisp board, plugged in to a USB cable, with a bright green indicator light.
Green means go! So let's get going…

Before proceeding with this guide, make sure you have set up your development environment according to Setting Up a Development Environment.

Creating a New Project#

  1. First, we create a new project using the GRiSP application template in the directory created in the previous tutorials. We will use the name robot for the project in this tutorial. The destination path /tmp/sd_card should be replaced with where your SD-card is mounted in your operating system. For a FAT32 formatted SD-card with the name GRISP the locations will most likely be:

    • /Volumes/GRISP on macOS
    • /media/GRISP on Linux is common, but it can vary. Check the details for your distribution.

    It also works fine with using /tmp/sd_card which lets you to test the deployment locally and allows you to copy the files to an SD-card at a later time.

    $ cd ~/GRiSP
    $ rebar3 new grispapp name=robot dest=/tmp/sd_card
    ===> Writing robot/src/robot.erl
    ===> Writing robot/src/robot_sup.erl
    ===> Writing robot/src/robot.app.src
    ===> Writing robot/rebar.config
    ===> Writing robot/.gitignore
    ===> Writing robot/LICENSE
    ===> Writing robot/README.md
    $ cd robot
    

This is all that is needed to start development and later deploy the application to the GRiSP board itself.

Adding Code#

Let us create an application that uses the color LEDs already present on the board.

  1. Edit src/robot.erl to contain the following:

    % @doc robot public API.
    % @end
    -module(robot).
    
    -behavior(application).
    
    % Callbacks
    -export([start/2]).
    -export([stop/1]).
    
    %--- Callbacks -----------------------------------------------------------------
    
    start(_Type, _Args) ->
        {ok, Supervisor} = robot_sup:start_link(),
        LEDs = [1, 2],
        [grisp_led:flash(L, red, 500) || L <- LEDs],
        timer:sleep(5000),
        grisp_led:off(2),
        Random = fun() ->
            {rand:uniform(2) - 1, rand:uniform(2) -1, rand:uniform(2) - 1}
        end,
        grisp_led:pattern(1, [{100, Random}]),
        {ok, Supervisor}.
    
    stop(_State) -> ok.
    

    This will flash both LED red for 5 seconds, then switch to a randomly generated pattern on the first LED.

  2. Make sure your changes compile without issues locally:

    $ rebar3 compile
    ===> Verifying dependencies...
    ===> Compiling robot
    $
    

Next Step#

Continue with the guide Deploying a GRiSP Application.