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

Feature Request: Allow Specifying GPIO Chip for GpioDContext #439

Open
Haruka0522 opened this issue Jan 23, 2025 · 0 comments
Open

Feature Request: Allow Specifying GPIO Chip for GpioDContext #439

Haruka0522 opened this issue Jan 23, 2025 · 0 comments

Comments

@Haruka0522
Copy link

Current Behavior

Currently, the GpioDContext in pi4j-library-gpiod does not provide a way to explicitly specify the GPIO chip to use. It defaults to the first chip with a label containing "pinctrl" which might implicitly be /dev/gpiochip0 in many cases. However, this behavior is not guaranteed and may not be suitable for all use cases, especially when dealing with systems that have multiple GPIO chips or require using a specific chip other than the default.

Expected Behavior

It should be possible to explicitly specify the GPIO chip to be used by GpioDContext, either by its name (e.g., "gpiochip2") or by its number (e.g., 2). If no chip is specified, GpioDContext should fall back to a default behavior, such as selecting the first chip with "pinctrl" in its label (the current behavior).

Motivation

This enhancement would improve the flexibility and usability of GpioDContext in the following ways:
Support for Multiple GPIO Chips: Users working with systems having multiple GPIO chips can explicitly choose the desired chip.

Proposed Solution

Introduce a setChip() method to GpioDContext that allows users to specify the desired GPIO chip. This method should accept either the chip name (String) or the chip number (int).

Proposed Method Signatures:

public synchronized void setChip(String chipName);
public synchronized void setChip(int chipNumber);
public synchronized void setChip(String chipName) {
        if (this.gpioChip != null) {
            logger.warn("GpioD context already initialized with chip: {}.  Ignoring new chip request.", this.gpioChip.getName());
            return;
        }
        this.desiredChipName = chipName;
        logger.debug("GpioD chip name set to: {}", (chipName == null || chipName.isEmpty()) ? "[default]" : chipName);
    }

Proposed Changes to initialize():

The initialize() method should be modified to:

                if (this.desiredChipName != null && !this.desiredChipName.isEmpty()) {
                    if (chip.getName().equals(this.desiredChipName)) {
                        useThisChip = true;
                    }
                } else {
                    if (chip.getLabel().contains("pinctrl")) {
                        useThisChip = true;
                    }
                }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant