项目作者: fivdi

项目描述 :
Extensible Linux IO Plugin for Johnny-Five
高级语言: JavaScript
项目地址: git://github.com/fivdi/linux-io.git
创建时间: 2017-02-05T00:44:59Z
项目社区:https://github.com/fivdi/linux-io

开源协议:MIT License

下载


Build Status

Linux-IO

Linux-IO is an extensible Linux
IO Plugin for
Johnny-Five. It extends
board-io to provide Linux
implementations for the following features that IO Plugins can support:

The initial motivation for implementing Linux-IO was to provide Linux
implementations of the I2C methods that Johnny-Five IO Plugins are required to
implement. Over the course of the last two years I was involved in adding I2C
functionality to a number of IO Plugins. In reality, more or less the same
code was added to each IO Plugin. The goal of Linux-IO is to make such code
reusable across Linux IO Plugins.

Linux-IO supports Node.js versions 10, 12, 14, 15 and 16.

Installation

  1. npm install linux-io

Johnny-Five Features Supported

The Johnny-Five features supported by a platform are summarized in tables on
the Platform Support page. The
features supported by Linux-IO shown in the following table:

Feature Support
Analog Read no
Digital Read yes
Digital Write yes
PWM no
Servo no
I2C yes
One Wire no
Stepper no
Serial/UART no
DAC no
Ping no

Usage

Here’s a minimalistic IO Plugin for the Raspberry Pi called
TinyRaspberryPiIO
that allows digital IO on GPIO4 and GPIO17 and I2C serial bus access on I2C
bus 1. The built-in LED can also be used.

  1. var LinuxIO = require('linux-io'),
  2. util = require('util');
  3. function TinyRaspberryPiIO() {
  4. if (!(this instanceof TinyRaspberryPiIO)) {
  5. return new TinyRaspberryPiIO();
  6. }
  7. LinuxIO.call(this, {
  8. pins: [{
  9. ids: ['P1-7', 'GPIO4'],
  10. gpioNo: 4,
  11. modes: [0, 1]
  12. }, {
  13. ids: ['P1-11', 'GPIO17'],
  14. gpioNo: 17,
  15. modes: [0, 1]
  16. }, {
  17. ids: ['LED0'],
  18. ledPath: '/sys/class/leds/led0',
  19. modes: [1]
  20. }],
  21. defaultI2cBus: 1,
  22. defaultLed: 'LED0'
  23. });
  24. setImmediate(function () {
  25. this.emit('connect');
  26. this.emit('ready');
  27. }.bind(this));
  28. }
  29. util.inherits(TinyRaspberryPiIO, LinuxIO);
  30. module.exports = TinyRaspberryPiIO;

If a button is connected to GPIO4 and an LED is connected to GPIO17, the
following
program
will turn the LED on when the button is pressed and turn
the LED off when the button is released.

  1. var five = require('johnny-five');
  2. var TinyRaspberryPiIO = require('./tiny-raspberry-pi-io');
  3. var board = new five.Board({
  4. io: new TinyRaspberryPiIO()
  5. });
  6. board.on('ready', function() {
  7. var led = new five.Led('GPIO17');
  8. var button = new five.Button('GPIO4');
  9. button.on('down', function() {
  10. led.on();
  11. });
  12. button.on('up', function() {
  13. led.off();
  14. });
  15. });

If an ADXL345 accelerometer is connected to I2C bus 1, the following
program
will print information provided by accelerometer.

  1. var five = require('johnny-five');
  2. var TinyRaspberryPiIO = require('./tiny-raspberry-pi-io');
  3. var board = new five.Board({
  4. io: new TinyRaspberryPiIO()
  5. });
  6. board.on('ready', function() {
  7. var accelerometer = new five.Accelerometer({
  8. controller: "ADXL345"
  9. });
  10. accelerometer.on("change", function() {
  11. console.log("accelerometer");
  12. console.log(" x : ", this.x);
  13. console.log(" y : ", this.y);
  14. console.log(" z : ", this.z);
  15. console.log(" pitch : ", this.pitch);
  16. console.log(" roll : ", this.roll);
  17. console.log(" acceleration : ", this.acceleration);
  18. console.log(" inclination : ", this.inclination);
  19. console.log(" orientation : ", this.orientation);
  20. console.log("--------------------------------------");
  21. });
  22. });

Examples

Additional examples for the Raspberry Pi, BeagleBone Black and C.H.I.P can be
found in the
example directory.

IO Plugins Based On Linux-IO