nodejs中的异步树莓派gpio事件 [英] Async raspberry pi gpio events in nodejs

查看:54
本文介绍了nodejs中的异步树莓派gpio事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个按钮连接到 Raspberry Pi GPIO 端口:

I connected a button to the Raspberry Pi GPIO ports:

pi-gpio 节点 js 模块只允许拉取当前状态而不是等待对于状态更改:

The pi-gpio node js module allows only to pull the current status but not waiting for a state change:

gpio.read(16, function(err, value) {
    if(err) throw err;
    console.log(value);    // The current state of the pin
});

有没有办法异步绑定事件?

Is there any solution to bind events asynchronously?

在这个例子中,这将是每当按下按钮时.

In this example this would be whenever the button is pressed.

推荐答案

我建议使用名为 onoff 的模块.

I'd suggest using the module called onoff.

它能够查看值文件,同时不会因间隔轮询而增加 CPU 负担.

It has the ability to watch a value-file while not taxing the cpu with polling in intervals.

我使用 node.js 制作了一个小型测试应用程序来试用 LED 和按钮,您可以在这里查看.

I made a small test-app with node.js to try out leds and buttons with it, you can check it out here.

这篇关于nodejs中的异步树莓派gpio事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆