Example #1
0
  public static void main(String[] args) throws InterruptedException {

    // START SNIPPET: usage-create-controller-snippet
    // create gpio controller instance
    final GpioController gpio = GpioFactory.getInstance();
    // END SNIPPET: usage-create-controller-snippet

    // START SNIPPET: usage-provision-input-pin-snippet
    // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
    // (configure pin edge to both rising and falling to get notified for HIGH and LOW state
    // changes)
    GpioPinDigitalInput myButton =
        gpio.provisionDigitalInputPin(
            RaspiPin.GPIO_02, // PIN NUMBER
            "MyButton", // PIN FRIENDLY NAME (optional)
            PinPullResistance.PULL_DOWN); // PIN RESISTANCE (optional)

    // END SNIPPET: usage-provision-input-pin-snippet
    // START SNIPPET: usage-provision-output-pin-snippet
    // provision gpio pins #04 as an output pin and make sure is is set to LOW at startup
    GpioPinDigitalOutput myLed =
        gpio.provisionDigitalOutputPin(
            RaspiPin.GPIO_04, // PIN NUMBER
            "My LED", // PIN FRIENDLY NAME (optional)
            PinState.LOW); // PIN STARTUP STATE (optional)
    // END SNIPPET: usage-provision-output-pin-snippet

    // START SNIPPET: usage-shutdown-pin-snippet
    // configure the pin shutdown behavior; these settings will be
    // automatically applied to the pin when the application is terminated
    // ensure that the LED is turned OFF when the application is shutdown
    myLed.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
    // END SNIPPET: usage-shutdown-pin-snippet

    // START SNIPPET: usage-control-pin-snippet
    // explicitly set a state on the pin object
    myLed.setState(PinState.HIGH);

    // use convenience wrapper method to set state on the pin object
    myLed.low();
    myLed.high();

    // use toggle method to apply inverse state on the pin object
    myLed.toggle();

    // use pulse method to set the pin to the HIGH state for
    // an explicit length of time in milliseconds
    myLed.pulse(1000);
    // END SNIPPET: usage-control-pin-snippet

    // START SNIPPET: usage-read-pin-snippet
    // get explicit state enumeration for the GPIO pin associated with the button
    PinState myButtonState = myButton.getState();

    // use convenience wrapper method to interrogate the button state
    boolean buttonPressed = myButton.isHigh();
    // END SNIPPET: usage-read-pin-snippet

    // START SNIPPET: usage-register-listener-snippet
    // create and register gpio pin listener
    myButton.addListener(new GpioUsageExampleListener());
    // END SNIPPET: usage-register-listener-snippet

    // START SNIPPET: usage-trigger-snippet
    // create a gpio synchronization trigger on the input pin
    // when the input state changes, also set LED controlling gpio pin to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myLed));
    // END SNIPPET: usage-trigger-snippet

    // keep program running until user aborts (CTRL-C)
    while (true) {
      Thread.sleep(500);
    }

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO
    // controller
  }
Example #2
0
  /**
   * [ARGUMENT/OPTION "--pin (#)" | "-p (#)" ] This example program accepts an optional argument for
   * specifying the GPIO pin (by number) to use with this GPIO listener example. If no argument is
   * provided, then GPIO #1 will be used. -- EXAMPLE: "--pin 4" or "-p 0".
   *
   * <p>[ARGUMENT/OPTION "--pull (up|down|off)" | "-l (up|down|off)" | "--up" | "--down" ] This
   * example program accepts an optional argument for specifying pin pull resistance. Supported
   * values: "up|down" (or simply "1|0"). If no value is specified in the command argument, then the
   * pin pull resistance will be set to PULL_UP by default. -- EXAMPLES: "--pull up", "-pull down",
   * "--pull off", "--up", "--down", "-pull 0", "--pull 1", "-l up", "-l down".
   *
   * @param args
   * @throws InterruptedException
   * @throws PlatformAlreadyAssignedException
   */
  public static void main(String[] args)
      throws InterruptedException, PlatformAlreadyAssignedException {

    // ####################################################################
    //
    // since we are not using the default Raspberry Pi platform, we should
    // explicitly assign the platform as the BananaPro platform.
    //
    // ####################################################################
    PlatformManager.setPlatform(Platform.BANANAPRO);

    // create Pi4J console wrapper/helper
    // (This is a utility class to abstract some of the boilerplate code)
    final Console console = new Console();

    // print program title/header
    console.title("<-- The Pi4J Project -->", "GPIO Input Example");

    // allow for user to exit program using CTRL-C
    console.promptForExit();

    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();

    // ####################################################################
    //
    // When provisioning a pin, use the BananaProPin class.
    //
    // ####################################################################

    // by default we will use gpio pin #01; however, if an argument
    // has been provided, then lookup the pin by address
    Pin pin =
        CommandArgumentParser.getPin(
            BananaProPin.class, // pin provider class to obtain pin instance from
            BananaProPin.GPIO_01, // default pin if no pin argument found
            args); // argument array to search in

    // by default we will use gpio pin PULL-UP; however, if an argument
    // has been provided, then use the specified pull resistance
    PinPullResistance pull =
        CommandArgumentParser.getPinPullResistance(
            PinPullResistance.PULL_UP, // default pin pull resistance if no pull argument found
            args); // argument array to search in

    // provision gpio pin as an input pin
    final GpioPinDigitalInput input = gpio.provisionDigitalInputPin(pin, "MyInput", pull);

    // set shutdown state for this pin: unexport the pin
    input.setShutdownOptions(true);

    // prompt user that we are ready
    console.println("Successfully provisioned [" + pin + "] with PULL resistance = [" + pull + "]");
    console.emptyLine();
    console.box("The GPIO input pin states will be displayed below.");
    console.emptyLine();

    // display pin state
    console.emptyLine();
    console.println(
        " ["
            + input.toString()
            + "] digital state is: "
            + ConsoleColor.conditional(
                input.getState().isHigh(), // conditional expression
                ConsoleColor.GREEN, // positive conditional color
                ConsoleColor.RED, // negative conditional color
                input.getState()));
    console.emptyLine();

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    gpio.shutdown();
  }