peter 发表于 2017-11-21 15:50:52

tinker-gpio 使用例子

原文链接:https://www.npmjs.com/package/tinker-gpio      



   tinker-gpio是一个简单的基于node.js的库来帮助访问华硕Tinker Board(Debian Jessie)的GPIO。 它围绕内置的fs模块松散地建模。
It works with:
[*]ASUS Tinker Board
var gpio = require("tinker-gpio");

gpio.open(16, "output", function(err) {                // Open pin 16 for output
    gpio.write(16, 1, function() {                        // Set pin 16 high (1)
      gpio.close(16);                                                // Close pin 16
    });
});你可以帮忙做啥static/image/hrline/line4.png
Ways you can help:- 查看拉请求并在tinkerboard上测试它们是否正确。
- 报告错误。
- 修复一个错误或添加一些功能都是非常好的,提交上来。关于引脚配置static/image/hrline/line4.png
    这有一些令人困惑的地方。 DEMO板的物理引脚没有按照任何特定的逻辑顺序布置。 他们中的大多数都给出了它使用的Rockchip芯片的引脚名称(rk3288)。 Tinker Board引脚头和Rockchip芯片引脚的物理布局之间没有逻辑关系。 操作系统可以识别Rockchip芯片的名称与Tinker上的物理引脚布局无关。 为了更容易识别,按Rockchip芯片的规格书来搞几乎是不可能的!
这个库简化了所有这些(希望),通过提取掉瑞芯微芯片的细节。 您只需要参照修补程序板上物理引脚布局上的引脚。 以下为引脚排列。 所有标有“GPIO”的引脚均可与此库一起使用,使用如下的引脚编号。

P1 - 3.3v125v
I2C SDA34--
I2C SCL56Ground
GPIO78TX
--910RX
GPIO1112GPIO
GPIO1314--
GPIO1516GPIO
--1718GPIO
SPI MOSI1920--
SPI MISO2122GPIO
SPI SCLK2324SPI CE0
--2526SPI CE1
Model A+ and Model B+ additional pins
ID_SD2728ID_SC
GPIO2930--
GPIO3132GPIO
GPIO3334--
GPIO3536GPIO
GPIO3738GPIO
--3940GPIO
That gives you several GPIO pins to play with: pins 7, 11, 12, 13, 15, 16, 18 and 22 (with A+ and B+ giving 29, 31, 32, 33, 35, 37, 38 and 40). 你应该提供这些物理的PIN号码给这个库,而不用在内部调用它们。 十分简单。

安装:如果还没有,请在tinkerboard上获取node和npm。
最简单的方法是:
sudo apt-get install nodejs npmTinkerboard的GPIO引脚要求您以root身份访问它们。 这是完全不安全的几个原因。 为了解决这个问题,你应该使用优秀的GPIO_API_for_C。
在你Tinkerboard上做以下事情:
wget http://dlcdnet.asus.com/pub/ASUS/mb/Linux/Tinker_Board_2GB/GPIO_API_for_C.zip
unzip GPIO_API_for_C.zip

cd GPIO_API_for_C/

sudo chmod +x build
sudo ./buildNext, cd to your project directory and use npm to install tinker-gpio in your project.
就这样,OK了
使用
open(pinNumber, , )
Aliased to .exportMakes pinNumber available for use.
[*]pinNumber:可用的引脚号。 请记住,pinNumber是修补器上的物理针号。
[*]options: 应该是一个字符串,例如输入或输入上拉。 您可以指定引脚方向是输入还是输出(或输入或输出)。 您还可以通过选择上拉或下拉(或上或下)来设置内部上拉/下拉电阻。 如果没有提供选项,则默认为输出。 如果没有指定方向(输入或输出)(例如,只有向上),则方向默认为输出。
[*]callback: 当引脚可用时将被调用。 如果出现错误,可能会收到第一个参数的错误。

close(pinNumber, )
Aliased to .unexportCloses pinNumber.
[*]pinNumber: The pin number to close. Again, pinNumber is the physical pin number on the Tinker.
[*]callback: (Optional) Will be called when the pin is closed. Again, may receive an error as the first argument.
setDirection(pinNumber, direction, )Changes the direction from input to output or vice-versa.
[*]pinNumber: As usual.
[*]direction: Either input or in or output or out.
[*]callback: Will be called when direction change is complete. May receive an error as usual.
getDirection(pinNumber, )Gets the direction of the pin. Acts like a getter for the method above.
[*]pinNumber: As usual
[*]callback: Will be called when the direction is received. The first argument could be an error. The second argument will either be in or out.
read(pinNumber, )Reads the current value of the pin. Most useful if the pin is in the input direction.
[*]pinNumber: As usual.
[*]callback: Will receive a possible error object as the first argument, and the value of the pin as the second argument. The value will be either 0 or 1 (numeric).
Example:gpio.read(16, function(err, value) {
    if(err) throw err;
    console.log(value);      // The current state of the pin
});write(pinNumber, value, )Writes value to pinNumber. Will obviously fail if the pin is not in the output direction.
[*]pinNumber: As usual.
[*]value: Should be either a numeric 0 or 1. Any value that isn't 0 or 1 will be coerced to be boolean, and then converted to 0 (false) or 1 (true). Just stick to sending a numeric 0 or 1, will you? ;)
[*]callback: Will be called when the value is set. Again, might receive an error.

Misc
运行测试: npm install && npm test你已所在的地方。
这个模块被创建,tinkerboard git 提交了和npm发布了所有这些!
The Tinker rocks!
页: [1]
查看完整版本: tinker-gpio 使用例子