Fork me on GitHub
beagleboard.org

BeagleBone 101

BoneScript

Familiar Arduino function calls, exported to the browser

The buttons below will run code that will impact the LEDs on your BeagleBone. The exact code used in the browser is below and will send messages to your board using Socket.IO.

Turn LEDs on:

var b=require('bonescript');
b.pinMode('USR0', 'out');
b.pinMode('USR1', 'out');
b.pinMode('USR2', 'out');
b.pinMode('USR3', 'out');
b.digitalWrite('USR0', 1);
b.digitalWrite('USR1', 1);
b.digitalWrite('USR2', 1);
b.digitalWrite('USR3', 1);

Turn LEDs off:

var b=require('bonescript');
b.pinMode('USR0', 'out');
b.pinMode('USR1', 'out');
b.pinMode('USR2', 'out');
b.pinMode('USR3', 'out');
b.digitalWrite('USR0', 0);
b.digitalWrite('USR1', 0);
b.digitalWrite('USR2', 0);
b.digitalWrite('USR3', 0);

Restore LEDs to default state:

var b = require('bonescript');
var p = '/sys/class/leds/beaglebone:green:usr';
b.digitalWrite('USR0', b.LOW);
b.digitalWrite('USR1', b.LOW);
b.digitalWrite('USR2', b.LOW);
b.digitalWrite('USR3', b.LOW);
resetUSR0();
function resetUSR0() {
    b.writeTextFile(p+'0/trigger', 'heartbeat', resetUSR1);
}
function resetUSR1() {
    b.writeTextFile(p+'1/trigger', 'mmc0', resetUSR2);
}
function resetUSR2() {
    b.writeTextFile(p+'2/trigger', 'cpu0', resetUSR3);
}
function resetUSR3() {
    b.writeTextFile(p+'3/trigger', 'mmc1', complete);
}
function complete() {
}

Functions

The Bonescript library provides several functions useful for interacting with your hardware.

JavaScript

Performing physical computing tasks in JavaScript is a rather different than C on microcontrollers. JavaScript and the Node.JS interpreter like to do everything asynchronously using callbacks. An event loop runs waiting on whatever the next system-blocking event is, such as waiting for a keypress or a file load to complete. The callbacks are then executed to completion before other event handlers are run.

Timers

Timing operations in JavaScript are provided by setting timers with callback event handlers. A nice overview of JavaScript timers can be found on www.w3schools.com.

  • var timer = setTimeout(callback, milliseconds)
  • clearTimeout(timer)
  • var timer = setInterval(callback, milliseconds)
  • clearInterval(timer)

Libraries

The Bonescript library runs in Node.JS. You can run it directly on the board using the 'node' interpreter or the Cloud9 IDE that invokes the 'node' interpreter. You can also run it using the bonescript.js script within your browser via remote procedure calls using Socket.io and served up by the web server running on your BeagleBoard.

Access to the library functions is provided through the "require('bonescript')" function call. The call returns an object containing all of the functions and constants exported by the library. The Node.JS API documentation on modules provides more information on the usage of 'require' within the 'node' interpreter.

Other JavaScript topics

The Chrome browser has a rather nice JavaScript debugger you can use to examine your code. You might also get good benefit out of 'console.log()'.

Because JavaScript is dynamically typed, you might find the 'typeof operator' rather useful to determine the type of a variable at run-time. A nice overview of the JavaScript typeof operator can be found at developer.mozilla.org.

Resources

To learn more about Cloud9 IDE and to synchronize the software on your board with cloud-hosted services, see www.c9.io.

For more information on Node.JS, the JavaScript interpreter, see www.nodejs.org. Note that version 0.8.22 is what is currently installed on the default image and you can find the api documentation at www.nodejs.org/docs/v0.8.22/api.

For more information about the Bonescript library, see www.beagleboard.org/bonescript.