我一直在玩 Raspberry Pi 和 Node 以获得乐趣。我想了一个简单的实验,如果我抓住一些用户输入来打开和关闭LED。
const readline = require('readline');
const log = console.log;
const five = require('johnny-five');
const raspi = require('raspi-io');
const board = new five.Board({
io: new raspi(),
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
board.on('ready', function() {
const led = new five.Led('P1-7');
const recursiveAsyncReadLine = function () {
rl.question('Command: ', function (answer) {
switch(answer) {
case 'on':
log('Got it! Your answer was: "', answer, '"');
led.on();
break;
case 'off':
log('Got it! Your answer was: "', answer, '"');
led.stop().off();
break;
default:
}
recursiveAsyncReadLine();
});
};
recursiveAsyncReadLine();
});
它可以工作,但我得到2个奇怪的错误。在下面的控制台输出中,您将看到它提示输入...我输入我的输入,然后它以扭曲的文本字符串重复我的输入
(参见实施例1)。然后输出我的验证消息(得到它!你的答案是:“打开”)我遇到了ReferenceError: on is not defined
(例2),即使
LED完美亮起。
Command: on
oonn //Example 1
Got it! Your answer was: " on "
Command:
ReferenceError: on is not defined //Example 2
>> off
ooffff
Got it! Your answer was: " off "
Command:
ReferenceError: off is not defined
>> on
oonn
Got it! Your answer was: " on "
Command:
ReferenceError: on is not defined
>> off
ooffff
Got it! Your answer was: " off "
Command:
ReferenceError: off is not defined
>> on
oonn
Got it! Your answer was: " on "
Command:
ReferenceError: on is not defined
>> off
ooffff
Got it! Your answer was: " off "
Command:
ReferenceError: off is not defined
我认为这不是一个Raspberry Pi / Johnny-five,而只是一个普通的JavaScript或Node问题。
有什么想法吗?
答案 0 :(得分:0)
好的回顾我之前的陈述看起来好像是你的问题
led.stop().off();
在Johny-five的例子中,他们表示
led.stop();
led.off();
http://johnny-five.io/examples/led/
祝你好运,快乐的黑客行为: - )
答案 1 :(得分:0)
如果你减少到这个(对我有用)会发生什么?
const readline = require('readline');
const log = console.log;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const recursiveAsyncReadLine = function () {
rl.question('Command: ', function (answer) {
switch(answer) {
case 'on':
log('Got it! Your answer was: "', answer, '"');
break;
case 'off':
log('Got it! Your answer was: "', answer, '"');
break;
default:
}
recursiveAsyncReadLine();
});
};
recursiveAsyncReadLine();
如果有效,那至少告诉我们它与raspi-io或johnny-five的交互有关。此处运行的位置,此代码是否在pi上执行,如果是,您是直接在pi上输入文本还是远程输入。如果远程输入,您输入文本的机器的平台是什么?