rpi-gpio.js icon indicating copy to clipboard operation
rpi-gpio.js copied to clipboard

gpio.accessible() method

Open jjeff opened this issue 4 years ago • 3 comments

I'm creating a cross-platform Node app which uses GPIO pins when they're available. However, on other platforms, I'd like to save the overhead (and errors) of the rpi-gpio calls.

Would you be interested in a pull request for a method which would return false if GPIO is not available?

jjeff avatar Sep 13 '20 21:09 jjeff

Hi, I've been having a think about it. On one hand it seems like a huge edge-case and not something others will use, but on the other there is already some RPI platform detection code in the module (setRaspberryVersion), so perhaps there's a neat way to expose this info without adding much complication.

Did you have an approach in mind for detecting whether the GPIOs are available? What do you think about just exposing this already-collected RPI platform info?

JamesBarwell avatar Sep 29 '20 15:09 JamesBarwell

Here's what I'm currently doing:

function isGpioAccessible() {
  let fd;
  try {
    fd = fs.openSync('/sys/class/gpio/export', 'r+');
  } catch(e) {
    return false;
  } finally {
    if (fd) {
      fs.closeSync(fd);
    }
  }
  return true;
}

jjeff avatar Sep 29 '20 18:09 jjeff

Cool, yeah it's simple enough and fairly self-contained. Yeah please open a PR and I'll try and get it in soon.

JamesBarwell avatar Sep 30 '20 16:09 JamesBarwell