Search code examples
javafxraspberry-pigpio

How to link buttons in javaFx to gpio pins on the rpi


I have som code that i found for accessing the gpio pins on the rpi with intellij. I also built some simple javaFx code, a simple window with 3 buttons. I want those 3 buttons to turn pin on, turn pin off, and shutdown window. I have all the code I need i just dont know how to put it toghether. Both codes works on their own. I am building an irregation system for plants and I have all the hardware working, In this first version I just want to use simple on off function.

This is my javafx code without imports, verry simple.

public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("Title of Window");
        button1 = new Button("Start");
        button2 = new Button("Stop");
        button3 = new Button("Exit");
        button1.setOnAction(e -> ); // Start gpio 
        button2.setOnAction(e -> ); // Stop gpio
        button3.setOnAction(e -> primaryStage.close());
        StackPane layout = new StackPane();
        layout.getChildren().add(button1);
        button1.setTranslateX(0);
        button1.setTranslateY(20);
        layout.getChildren().add(button2);
        button2.setTranslateX(50);
        button2.setTranslateY(20);
        layout.getChildren().add(button3);
        button3.setTranslateX(108);
        button3.setTranslateY(20);
        Scene scene = new Scene(layout, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
}
}

And this is the method I want to link button to 1 & 2.

public class gpio {

    public static void main(String[] args) throws InterruptedException {
        final GpioController gpio = GpioFactory.getInstance();
        final GpioPinDigitalOutput pin = 
    gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "MyLED", PinState.HIGH);
        pin.setShutdownOptions(true, PinState.LOW);

        // toggle the current state of gpio pin #01 (should turn on)
        pin.toggle();
        System.out.println("--> GPIO state should be: ON");
        // toggle the current state of gpio pin #01  (should turn off)
        pin.toggle();
        System.out.println("--> GPIO state should be: OFF");
}
}

code found here https://pi4j.com/1.2/example/control.html

This is pobably easy to solve but its a hard nut for me to crack so I was hoping to learn from someone more gifted in java then me, or if I could be pointed to someone eith similar problems.


Solution

  • Same thing for button2

    button1.setOnAction(new EventHandler<ActionEvent>() {
       @Override public void handle(ActionEvent e) {
            final GpioController gpio = GpioFactory.getInstance();
            final GpioPinDigitalOutput pin = 
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "MyLED", PinState.HIGH);
            pin.setShutdownOptions(true, PinState.LOW);
    
            // toggle the current state of gpio pin #01 (should turn on)
            pin.toggle();
            System.out.println("--> GPIO state should be: ON");
       }
     });
    

    PS : Don't forget the necessary imports