Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for GPIO monitoring callbacks #23

Open
varadero opened this issue Feb 25, 2024 · 6 comments
Open

Support for GPIO monitoring callbacks #23

varadero opened this issue Feb 25, 2024 · 6 comments
Labels
enhancement New feature or request gpiod-1.x all related to underlying libgpiod-1.x series help wanted Extra attention is needed pin-monitoring we need to be able to register callbacks for monitor pins

Comments

@varadero
Copy link

I know that GPIO monitoring callbacks is in the roadmap but it would be really great addition. I have tried node-libgpiod with Raspberry Pi 5 4GB (I just needed to change new Chip(0) to new Chip(4)) and it works great for setting the outputs but don't know how to do the "monitoring" (like detecting push buttons connected to some GPIO). I am willing to sponsor the addition of the callbacks to the library.

@sombriks sombriks added enhancement New feature or request pin-monitoring we need to be able to register callbacks for monitor pins labels Feb 25, 2024
@sombriks
Copy link
Owner

Hi @varadero i am doing some cleanup and after that will work on that for 1.x branch first. At some point we can discuss proper api design/ergonomics here, ;-)

@Garfonso
Copy link

I would be truly interested in that as well. Did you make any progress?

@sombriks
Copy link
Owner

sombriks commented Jun 1, 2024

Hello @Garfonso indeed this feature is one will be a big improvement, i just need testcases / gpiosim up and running so i can handle the missing bits of 1.x API. staty tuned!

@sombriks sombriks added help wanted Extra attention is needed gpiod-1.x all related to underlying libgpiod-1.x series labels Jun 30, 2024
@2bndy5
Copy link

2bndy5 commented Oct 29, 2024

Given that javascript was designed as an event-driven language, I would rank this with high priority. It seems you have much on your plate with this project, so patience is definitely worth the reward here.

@hmeerlo
Copy link

hmeerlo commented Nov 7, 2024

I don't understand how anybody is using GPIO with nodejs nowadays. Since Bookworm gpiod is the norm to use GPIO, but there is no way to use it properly in nodejs. This library can set and get values, but a very important part of GPIO is event monitoring. I have a reset button attached to a GPIO pin of which I need an event on the rising and falling edge. This was no problem with the old /sys/class/gpio/* interfaces, but with the new way of working it seems almost impossible to achieve. Or am I missing something?

@2bndy5
Copy link

2bndy5 commented Nov 7, 2024

This was no problem with the old /sys/class/gpio/* interfaces

Actually, there were problems with the sys FS interface. Most notably, nothing in sys FS interface prevented other threads from appropriating occupied resources (a GPIO chips' exposed pins).

Many of the C/C++ offerings (PiGPIO, MRAA, wiringPi) wrapped the sys FS interface into something that can only be described as "a best effort to shield end-users from the pitfalls of the sys FS interface." But, the limited design of the sys FS interface often required wrappers to have root access... This didn't bode well for environments (a python venv for example) that were designed to avoid messing with system-scoped resources.

The newer (ioctl-based) char dev interface does not require root privileges. FYI, the GPIO API is the latest of Linux kernel's peripheral APIs to switch to a char dev API. The Linux kernel's I2C and SPI interfaces have been using a (ioctl-based) char dev paradigm for years. You might find Linux kernel examples for I2C still using the older sys FS interface (like the undead wiringPi library does).

but with the new way of working it seems almost impossible to achieve. Or am I missing something?

I landed on this repo because npmjs.com showed search results that haven't been updated in years. It would seem that embedded Linux isn't a popular use case for node.js (despite the node-red project which used to ship with RPi OS).

The char dev API depends on using the Linux kernel's ioctl() (which can be differently implemented per OS/platform) and poll() (for event monitoring). The hard part is safely exposing C in node.js.

Alternative Idea

The authors of libgpiod (which is a convenience wrapper for the Linux kernel's char dev API) also ship a rust binding. I thought about wrapping that binding in a napi-rs project, but (like this node-gpiod project) it is not a simple undertaking. The only benefit is that a napi-rs project can ship compiled binaries/modules (*.node) as optional dependencies (per arch type) of a root package (which includes generated TS type definitions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gpiod-1.x all related to underlying libgpiod-1.x series help wanted Extra attention is needed pin-monitoring we need to be able to register callbacks for monitor pins
Projects
None yet
Development

No branches or pull requests

5 participants