我正在用javascript编写arduino板。我正在尝试使用johnny-five库连接多个arduino板。我跟着johnny-five documentation,我可以同时击中两个板上的led 13。
但是我的问题是我想一次控制一个LED。如何在每块电路板上专门初始化LED?
答案 0 :(得分:3)
Boards
(注意复数)类的the docs中有很多例子。我直接复制了以下内容:
Boards
类构造一个包含多个板对象的集合对象。如果没有传递参数,将按照系统枚举它们的顺序为每个检测到的板创建Board
个对象。
另请参阅:Board
初始化多个板对象的最简单方法是使用Boards
调用new
构造函数。不要担心知道设备的路径或COM端口,Johnny-Five会自动确定兼容板正在使用哪些USB。
// Create 2 board instances with IDs "A" & "B"
// (ports will be initialized in device enumeration order)
new five.Boards([ "A", "B" ]);
或者
// Create two board instances on ports
// "/dev/cu.usbmodem621" and
// "/dev/cu.usbmodem411"
new five.Boards([ "/dev/cu.usbmodem621", "/dev/cu.usbmodem411" ]);
或者
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports);
一旦板对象初始化,它们必须通过一组握手步骤连接到物理板,一旦完成,板就可以与程序通信了。此过程是异步的,并通过“就绪”事件表示给程序。
// 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)
});
通过提供显式端口路径来覆盖它:
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)
});
Boards
构造函数的基本但完整的示例用法:
// 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();
});
});
通过提供显式端口路径来覆盖它:
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();
});
});
注意当使用多个电路板时,必须使用与它们相关联的电路板对象的显式引用来>来初始化所有设备类。这在前面的代码示例中进行了说明。
每个(回调(电路板,索引))为每个电路板对象调用一次函数。
...
目前尚未提及的一件事,我将更新这样做,是this
处理程序内的ready
是一个类似于数组的对象,包含对每个已初始化的板的引用,顺序如下:他们被创造出来了:
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
});
答案 1 :(得分:-1)
我猜你可以使用id
来访问每个主板。或者您可以实例化两个具有不同名称的板,例如:
var five = require("johnny-five"),
boardNumberOne = new five.Board();
boardNumberTwo = new five.Board();