kelly / node-i2c

Node.js native bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

All callbacks are fired synchronously rather than asynchronously

fivdi opened this issue · comments

Callbacks should be fired asynchronously but node-i2c fires them synchronously. The following (synthetic) program demonstrates this:

var i2c = require('i2c');
var address = 0x39;
var wire = new i2c(address, {device: '/dev/i2c-1', debug: false});

function getId() {
  wire.writeByte(0x0a | 0x80, function(err) {
    wire.readByte(function (err, res) {
      getId();
    });
  });
}

getId();

The program should read the Id from an Adafruit TSL2561 Digital Luminosity/Lux/Light Sensor "forever", but after about 2 seconds the program terminates with the following error on a BeagleBone Black:

RangeError: Maximum call stack size exceeded

The issue is occuring because all methods that fire callbacks fire them before they return. The callbacks should be fired after the method returns. For example, the callback for readByte is fired here before readByte returns.

Feel free to submit a pull request to fix the issue.

PR #45 contains a fix for firing callbacks asynchronously rather than synchronously. With this fix the example program above functions as expected. Other than that I've done very little testing.

Great! Thanks for doing this. Appears to work. Need to make a few small formatting changes and will push a new release soon.