Angl0r
Angl0r

Reputation: 17

Raspberry PI, Java and Pi4J Controlling GPIO cant get current PinState

Iám ciurrently Working on a Little Programm wich should check the current state of an explizit GPIO Pin and than toggle ist. For this iám using java and PI4J. When my Programm starts, the LED is turned of. But as soon as i get it as a variable it automaticly sets the state to HIGH. Dos anyone have an idea how to avoid this ? my code so far:

final GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalOutput led =gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT);


            PinState ledStatus = led.getState();
            if (ledStatus.isHigh())
            {
                led.setShutdownOptions(true, PinState.LOW);
                System.out.println("Set shutdownOption LOW");
            }
            else
            {
                led.setShutdownOptions(true, PinState.HIGH);
                System.out.println("Set shutdownOption HIGH");
            } 

         if(ledStatus.isHigh())
        {
            System.out.println("LEDS aus...");
        }
        else 
        {
            System.out.println("LEDS an...");
        }
        led.toggle();

This Works quite fine. But as i said, before i start the programm... the LED is off! as soon as i get to this line : GpioPinDigitalOutput led =gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT); The LED is ON! i need to figure out a way to get the state before the app is running , change it and then exit my programm with the LED toggled.

Thanks for your Help :)

Upvotes: 0

Views: 582

Answers (1)

silmx
silmx

Reputation: 486

try using the provisionDigitalMultipurposePin method to set first mode INPUT in order to read the state, and then switch to OUTPUT mode to change the pin state.

GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalMultipurpose led = gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07, PinMode.DIGITAL_INPUT);

// read state

led.setMode(PinMode.DIGITAL_OUTPUT);

// write state

Upvotes: 1

Related Questions