There are many examples in the docs for the Boards
(note the plural) class. I've copied the following directly:
The Boards
class constructs a collection object containing multiple board objects. If no arguments are passed, Board
objects will be created for every board detected in the order that the system enumerates them.
See also: Board
Parameters
- ports A list of port objects or port address strings. Port objects may have the following properties:
Property Name
Type
Value(s)
Description
Required
id
Number, String
Any
User definable identification
no
port
String
"/dev/ttyAM0", "COM1"
Path or name of device port/COM
no
Initializing Multiple Boards
The easiest way to initialize multiple board objects is to call the Boards
constructor function with new
. Don't worry about knowing your device's path or COM port, Johnny-Five will figure out which USBs are in use by compatible boards automatically.
// Create 2 board instances with IDs "A" & "B"
// (ports will be initialized in device enumeration order)
new five.Boards([ "A", "B" ]);
Or
// Create two board instances on ports
// "/dev/cu.usbmodem621" and
// "/dev/cu.usbmodem411"
new five.Boards([ "/dev/cu.usbmodem621", "/dev/cu.usbmodem411" ]);
Or
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports);
Board Ready
Once the board objects have been initialized, they must connect to the physical boards with a set of handshake steps, once this has completed, the boards are ready to communicate with the program. This process is asynchronous, and signified to the program via a "ready" event.
// Create 2 board instances with IDs "A" & "B"
new five.Boards([ "A", "B" ]).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
});
Override this by providing explicit port paths:
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
});
Usage
A basic, but complete example usage of the Boards
constructor:
// Create 2 board instances with IDs "A" & "B"
new five.Boards([ "A", "B" ]).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
// |this| is an array-like object containing references
// to each initialized board.
this.each(function(board) {
// Initialize an Led instance on pin 13 of
// each initialized board and strobe it.
new five.Led({ pin: 13, board: board }).strobe();
});
});
Override this by providing explicit port paths:
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
// |this| is an array-like object containing references
// to each initialized board.
this.each(function(board) {
// Initialize an Led instance on pin 13 of
// each initialized board and strobe it.
new five.Led({ pin: 13, board: board }).strobe();
});
});
NOTE When using multiple boards, all device classes must be initialized with an explicit reference to the board object that they will be associated to. This is illustrated in the previous code example.
API
each(callback(board, index)) Call a function once for each board object.
...
One thing not currently mentioned, which I will update to do so, is that this
inside of the ready
handler is an array-like object containing references to each initialized board, in the order that they were created:
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
// Create 2 board instances with IDs "A" & "B"
new five.Boards(ports).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
this[0]; // <-- this is board A reference
this[1]; // <-- this is board B reference
});