0

Automatically optional too, simply by testing for the presence of the library, avoiding making the user pass arguments or define environment variables or the like.

I'm working on making a Raspberry Pi project of mine, a project that depends on GPIO, compatible with the Raspberry Pi 5. (The Raspberry Pi 5, in general, has caused a lot of grief due to a new GPIO chipset.)

Having reached the point where I'd solved all of my own GPIO issues, I had left to handle one library of someone else's in need of upgrade. I could, of course, just take my own fork of their project and go my own merry way now that I have working code, but I'd like to offer what I've done as a pull request.

There's one thing now that could be a sticking point, however, which I'd like to avoid if possible. I've introduced a new dependency on the libgpiod library.

I'm pretty sure the original author wouldn't want to deal with that. I'd like for this new code to compile on a Raspberry Pi 3 or 4 without libgpiod having been installed, but take full advantage of libgpiod if present.

Here's my binding.gyp file:

{
  "targets": [
    {
      "variables": {
        "dht_verbose%": "false"
      },
      "target_name": "node_dht_sensor",
      "sources": [
        "src/bcm2835/bcm2835.c",
        "src/node-dht-sensor.cpp",
        "src/dht-sensor.cpp",
        "src/util.cpp",
        "src/abstract-gpio.cpp", // <-- My code to handle GPIO using either bcm2835 or libgpiod
      ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      "dependencies": [
        "<!(node -p \"require('node-addon-api').gyp\")"
      ],
      'libraries': [
        '-lgpiod' // <-- New dependency
      ],
      "defines": [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
      "conditions": [
        ["dht_verbose=='true'", {
          "defines": [ "VERBOSE" ]
        }]
      ]
    }
  ]
}

Here's the start of my abstract-gpio.cpp:

#include "abstract-gpio.h"

#include <algorithm>
#include "bcm2835/bcm2835.h"
#include <fstream>
#include <gpiod.h> // <-- What to do here to make this optional? Can node-gyp
#include <regex>   //     conditionally create a symbol for #ifndef-ing here?
#include <stdio.h>
#include <string>

My C++ skills are rusty at best. Until now I hadn't touched my own C++ code for five years, and five years ago I hadn't touched C++ for well over a decade.

node-gyp? I've merely followed examples and simple recipes at this point.

The big trick is that consumers of this library will use this library via npm install node-dht-sensor. I don't want the user to have to worry about libgpiod unless they're using a Raspberry Pi and have a good reason to install it.

Is there some combination of node-gyp options and perhaps, if needed, dummy libgpiod C++ stubs (that won't interfere with the real libgpiod if present) that can solve this problem for me?

Update:

Here's a somewhat, but not fully satisfactory solution, which requires a user to install the module like this:

npm install node-dht-sensor --use_libgpiod=true

...if they need (or want... this does work on a RPi 4 too) to use libgpiod, allowing the standard installation without the extra parameter to impose no new library dependencies.

{
  "targets": [
    {
      "variables": {
        "dht_verbose%": "false",
        "use_libgpiod%" : "false"
      },
      "target_name": "node_dht_sensor",
      "sources": [
        "src/bcm2835/bcm2835.c",
        "src/node-dht-sensor.cpp",
        "src/dht-sensor.cpp",
        "src/util.cpp",
        "src/abstract-gpio.cpp",
      ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      "dependencies": [
        "<!(node -p \"require('node-addon-api').gyp\")"
      ],
      "defines": [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
      "conditions": [
        ["dht_verbose=='true'", {
          "defines": [ "VERBOSE" ]
        }],
        ["use_libgpiod=='true'", {
          "defines": [ "USE_LIBGPIOD" ],
          'libraries': [
            '-lgpiod'
          ]
        }]
      ]
    }
  ]
}

What I'd really like to do, however, if it's at all possible, is to replace "use_libgpiod=='true'" with something automatic, the moral equivalent of "available(libgpiod)" or "exists('/usr/include/gpiod.h')", in case any such node-gyp capability exists which simply can't be easily found in the documentation.

4
  • Here I am, trying to do my best in the spirit of supporting open-source coding projects (I could just be happy with where I am now and not try to help the original author), and someone down-votes me for what? Some fussy detail about the way I asked my question, or when I asked my question? Commented Feb 14 at 1:01
  • (I'm not the one who -) It's unclear what DHT you have and if kernel supports it already. Most likely you simply need to enable the respective IIO (rarely HWMON) driver and continue with the simple sysfs accesses or related libraries (IIO has a good support in modern browsers IIRC, but I'm never done anything in JS). Commented Feb 14 at 19:35
  • @0andriy, I'm afraid we're talking about different contexts of the initialism DHT. What I'm working on isn't about monitoring motherboard health, but ambient temperature and humidity in the context of a Raspberry Pi connected to an external sensor. At any rate, my PR to the node-dht-sensor project has been approved, so I guess I did good enough a job as is. I still wish I could free a user of the library from having to pass a command line argument to get the desired result, however. Commented Feb 16 at 4:55
  • No, I'm talking exactly the same: external DHT sensor connected to RPi board. Commented Feb 16 at 12:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.