我一直在玩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
然后在输出我的验证消息后(知道了!您的答案是:“on”)即使 LED 完美点亮,我也会遇到(示例 2)。
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-5 的事情,而且只是一个普通的旧 JavaScript 或 Node 问题。
有任何想法吗?