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.
http://192.168.7.2:3000/ide.html
. The IDE will open to a window like this: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:
projects
and save the file in it, naming the file major_tom_blinks.js
.$ cd projects $ node major_tom_blinks.js
You can find more information at http://beagleboard.org/Support/BoneScript.
18.116.62.45