fivdi / pigpio

Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
MIT License
943 stars 89 forks source link

Running on non-pi #107

Closed vypxl closed 4 years ago

vypxl commented 4 years ago

Can there be any way to install and run this library on a development machine, which does not have pigpio installed? I'm thinking of some callback to be called if the library isn't detected so that the developer can automatically select the mock library for example. Especially because there is no easy way to exclude a package with npm install.

fivdi commented 4 years ago

Does it work if pigpio is defined as an optional dependency rather than a dependency and pigpio-mock is defined as a development dependency with something like this in package.json:

  "dependencies": {
    // Don't add pogpio as a dependency, add it as an optional dependency
  },

  "optionalDependencies": {
    "pigpio": "^3.1.1"
  },

  "devDependencies": {
    "pigpio-mock": "^0.0.1",
  },

Note that there will most likely be compile errors when an attempt is made to install the optional dependency pigpio on the development machine. However, these errors can be ignored and your application should install successfully.

fivdi commented 4 years ago

The installation section of the pigpio-mock readme shows how to distinguish between production and development environments at runtime.

vypxl commented 4 years ago

That's true, it only bugs me that I have to accept the compile error every time I install my packages.

fivdi commented 4 years ago

Yes, compile errors can be annoying.

If your development machine is a mac or PC with the appropriate C/C++ compiler installed there shouldn't be any compile errors as the code is only compiled on Linux machines. As far as I remember the compiler still needs to be installed though.

vypxl commented 4 years ago

My workaround on my Linux dev machine is, that I have pigpio installed there, only that when I start it, it obviously gives me an error when I try to start it. It just feels like a hack.

My suggestion is, that this package could detect if it is being installed on a pi or not, and if not, does not throw an error if the build fails.

fivdi commented 4 years ago

My suggestion is, that this package could detect if it is being installed on a pi or not, and if not, does not throw an error if the build fails.

This isn't possible. If the build fails the package isn't installed so it can't "not throw an error if the build fails". The error comes from Node.js because require('pigpio') fails as the package isn't installed.

vypxl commented 4 years ago

Ok, maybe I misunderstood that part.

But what I think is possible, would be to add a condition to the binding.gyp file which checks /proc/cpuinfo and if it contains the string 'Raspberry Pi', like grep "Raspberry Pi" /proc/cpuinfo. Then one could use a different dummy source file without the reference to the pigpio library.

I understand if this is out of scope though.

fivdi commented 4 years ago

Try npm install fivdi/pigpio#compile to see if it works for you.

vypxl commented 4 years ago

Works like a charm, this is exactly what I hoped for!

I would only change pigpio.js#L6 to


const pigpio = (() => {
----------------^^-----```
fivdi commented 4 years ago

Why would you change that line?

A side effect of this enhancement is that pigpio can now have a travis build :smile:

vypxl commented 4 years ago

I would change it, because using () means 'no function parameters' and a _ is an unused function parameter.

Nice thing with Travis :grin:

fivdi commented 4 years ago

Ok. Thanks for the tip.

I'll release a new version of pigpio tomorrow.

vypxl commented 4 years ago

Thanks for your quick help :)

fivdi commented 4 years ago

pigpio@3.2.0 which includes this enhancement has now been published on npm.