WiringPi / WiringPi-Node

Node.js bindings to wiringPi
334 stars 94 forks source link

Development mode #12

Closed marvinroger closed 8 years ago

marvinroger commented 10 years ago

Hi,

Thanks for this amazing lib! However, I have a feature request that would greatly improve development. It would be great if the lib could detect if it's running on a RPi or not. If it is, then it builds normally using node-gyp. If it doesn't, it could fallback into development mode, which can be as simple as logging what method is called.

This would allow a RPi specific app to be runnable in another environment.

A solution may be:

I think this should be pretty easy to implement... However, I can't do a PR as I don't feel confident about native extensions. ;)

taoyuan commented 9 years ago

+1

acevedodamian commented 9 years ago

+1 !! that would be great

nekuz0r commented 9 years ago

Hi,

The idea of a dev mode can be very useful, i thought about it as well, but i think the request cannot be satisfied or requires more thinking.

The first thing making this not easy to achieve i can think of is lib wiringpi cannot be initialized outside of a RPI environment.

To get around this difficulty, an idea should be to not call lib wiringpi method when in dev mode, but on the other hand it introduces a few others;

If you have any ideas to work around theses issues i am all ears :)

nekuz0r commented 9 years ago

Any news about this requested feature ?

marvinroger commented 9 years ago

Sorry, I haven't seen your update.

I did not think about the methods returning value... One way could have been to let the developer set fake data. Imagine the dev creates an app logging light coming from a light sensor (no analog input on RPi, simplified for the example). Then he would do something like:

var wpi = require('wiring-pi');

if (wpi.mode == 'development') {
  setInterval(function() {
    var analogLight = Math.floor(Math.random()*(255+1)); // Generate random value between 0 and 255
    setFakeAnalogRead(0, analogLight);
  }, 1 * 1000);
}

This way, every second the pin value would change, just like a real sensor would do. This would work for the core functions like digitalRead and analogRead, but obviously not for things like SPI or I2C.

marvinroger commented 9 years ago

But I don't think this is something this lib should handle. This is basically mocking. The only problem as of now to use mocking is that wiring-pi don't build on non-RPi env.

Here is an updated solution:

I think this would be much cleaner and would allow to do:

var wpi = require('wiring-pi');

if (wpi.mode == 'development') {
  wpi = {
    analogRead: function(pin) {
      return Math.floor(Math.random()*(255+1));
    }
  };
}
nekuz0r commented 9 years ago

An idea came to my mind right now, maybe we should consider creating a different project written in pure js that mock wiring-pi.