0

我一直在玩Raspberry PiNode以获得乐趣。我想进行一个简单的实验,如果我抓住一些用户输入来打开和关闭 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 问题。

有任何想法吗?

4

2 回答 2

0

好的,追溯我以前的陈述看起来你的问题可能是

led.stop().off();

在 Johny-5 的例子中,他们展示了

led.stop();
led.off();

http://johnny-five.io/examples/led/

祝你好运,快乐的黑客:-)

于 2018-02-12T19:09:08.603 回答
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-5 的交互有关。这个在哪里运行,这个代码是否在 pi 上执行,如果是,你是直接在 pi 上输入文本还是远程输入。如果远程输入,您输入文本的机器的平台是什么?

于 2018-02-12T19:48:00.290 回答