Using the Cloud9 IDE

As the name implies, Cloud9 is a cloud-hosted toolkit. For BBB, it provides an integrated, open source development environment to build BoneScript-powered (JavaScript) code. Its strengths are JavaScript and Node.js (which it actually uses on the backend; we'll discuss this in the next section), though it is also very flexible with other programming languages such as PHP, Ruby, and Python.

The IDE comes preloaded and ready to use immediately on the BBB firmware with no setup necessary. With your board still connected via USB, let's do a snappy recipe.

How to do it…

  1. To load the IDE, open a browser window to the following URL: http://192.168.7.2:3000/ide.html. The IDE will open to a window like this:
    How to do it…
  2. Next, change the color settings to improve readability in the UI. The default black scheme is funereal; let's change it to Cloud9 Bright Theme instead.

    Note

    Not all UI screens change to the new color scheme; there's a temporary bug in this beta release of Cloud9's IDE.

    How to do it…
  3. One handy feature in the lower portion of the screen is a command shell window. This gives the user an integrated command-line control within the IDE proper without the need to pop open another window for a shell application.
  4. Unless you want to customize the UI or listen to the audio tutorials, close the Welcome tab.
  5. Create a new file by clicking on the + sign.
    How to do it…
  6. Copy and paste the following code in the new window:
    var b = require('bonescript');
    var ledPin = "USR0";
    
    b.pinMode(ledPin, b.OUTPUT);
    
    var state = b.LOW;
    b.digitalWrite(ledPin, state);
    
    setInterval(toggle, 1000);
    
    function toggle() {
        if(state == b.LOW) state = b.HIGH;
        else state = b.LOW;
        b.digitalWrite(ledPin, state);
    }

For you Arduino users, the code feels kind of familiar, right? For those of you who are brand new to physical computing and hardware, let's break down some of the lines before executing the script.

At the outset of the code, we needed to establish certain ground rules, namely that we will be using the functionality of BoneScript. So, we had to include the BoneScript library with all its functionalities and coding shortcuts with the following script:

var b = require('bonescript');

Next, we needed to establish which pin or pins we intended to use (USR0) and then create a variable of it so as to not have to write the pin name repeatedly throughout the script. The following code helped us do this:

var ledPin = "USR0";

Once we had an anointed pin to play with, we needed to set its mode—in other words, initialize it—so that we could interact with it and declare whether it will be an input or output pin. If we were controlling a button, the pin would need to be recognized as an input; however, in our case we had an LED, which needed to be in the output mode. Note also b., which is used in the notation. This was used to tell the code interpreter—Node.js—that the subsequent function will be found in the BoneScript library. For this, we used the following code:

b.pinMode(ledPin, b.OUTPUT);

With the pin initialized, we declared whether the pin began in a state of being on (HIGH) or off (LOW). At this point, we also needed to tell the interpreter that we would actually be writing to the pin, as opposed to collecting data from it or reading it, with the following script:

var state = b.LOW;
b.digitalWrite(ledPin, state);

Our task moved on to telling the interpreter how often we will be doing a particular thing. In our case, we toggled the light on and off in 1000-millisecond intervals by executing the following script:

setInterval(toggle, 1000);

It was great that we knew the frequency of our toggle event, but as we did not yet tell the interpreter what the toggle term actually does, we had to explain its meaning. We described it as a function that alternates between the states of HIGH and LOW, which was applied to the pin that we declared at the beginning of our script with the following lines of code:

function toggle() {
    if(state == b.LOW) state = b.HIGH;
    else state = b.LOW;
    b.digitalWrite(ledPin, state);
}

Okay! For the moment, that's enough of a script breakdown. If you have not already jumped ahead, let's continue with the recipe's steps and take a look at how these lines actually affect the LED.

Now, perform the following steps:

  1. Create a new directory called projects and save the file in it, naming the file major_tom_blinks.js.
    How to do it…
  2. Now, run the file in the terminal window at the bottom of the IDE. First, ensure that you navigate to the proper directory using the following lines of code:
    $ cd projects
    $ node major_tom_blinks.js
    
  3. The USR0 LED (which is the on board LED closest to the reset button) should now have changed its constant "heartbeat"-patterned blink to slower, steadier 1-second blink intervals.
  4. Now, press Ctrl + z or click on the Stop button in the terminal pane window at the bottom of the IDE. Don't forget to do this; otherwise, you're likely to run into some errors or confusion in the upcoming recipes.
  5. Finally, it's a good idea to reset the LEDs so that you end up where you started. For now, the easiest way to do this is by running the restore script at http://beagleboard.org/Support/BoneScript/demo_blinkled/.

See also

You can find more information at http://beagleboard.org/Support/BoneScript.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.45