示例#1
0
 @Override
 public void setState(boolean state, GpioPinDigitalOutput... pin) {
   delegate.setState(state, pin);
 }
  public static void main(String args[]) throws InterruptedException, IOException {

    System.out.println("<--Pi4J--> PiFace (MCP23017) GPIO Example ... started.");

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

    // create custom PiFace GPIO provider
    final PiFaceGpioProvider gpioProvider =
        new PiFaceGpioProvider(PiFaceGpioProvider.DEFAULT_ADDRESS, SpiChannel.CS0);

    // provision gpio input pins from PiFaceGpioProvider
    GpioPinDigitalInput myInputs[] = {
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_00),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_01),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_02),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_03),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_04),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_05),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_06),
      gpio.provisionDigitalInputPin(gpioProvider, PiFacePin.INPUT_07)
    };

    // create and register gpio pin listener
    gpio.addListener(
        new GpioPinListenerDigital() {
          @Override
          public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
            // display pin state on console
            System.out.println(
                " --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
          }
        },
        myInputs);

    // provision gpio output pins and make sure they are all LOW at startup
    GpioPinDigitalOutput myOutputs[] = {
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_00),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_01),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_02),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_03),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_04),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_05),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_06),
      gpio.provisionDigitalOutputPin(gpioProvider, PiFacePin.OUTPUT_07),
    };

    // keep program running for 20 seconds
    for (int count = 0; count < 10; count++) {
      gpio.setState(true, myOutputs);
      Thread.sleep(1000);
      gpio.setState(false, myOutputs);
      Thread.sleep(1000);
    }

    // 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();

    System.out.println("Exiting PiFaceGpioExample");
  }