예제 #1
0
파일: Level1.java 프로젝트: Soletaken/RPi
  public static void main(String[] args) throws InterruptedException {
    System.out.println("works");
    GpioController gpio = GpioFactory.getInstance();
    GpioPinDigitalOutput pin1 =
        gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLed", PinState.HIGH);
    pin1.setShutdownOptions(true, PinState.LOW);

    Thread.sleep(5000);
    System.out.println("pin low");
    pin1.low();

    Thread.sleep(5000);
    System.out.println("pin toggle");
    pin1.toggle();

    Thread.sleep(5000);
    System.out.println("pin pulse");
    pin1.pulse(1000, true);

    Thread.sleep(2000);
    System.out.println("pin blink");
    pin1.blink(500, 5000);

    Thread.sleep(2000);
    System.out.println("koniec");
    gpio.shutdown();
  }
  @RequestMapping("/shutdown.html")
  public String shutdown() {

    gpio.shutdown();
    leftBackMotor.low();
    leftFrontMotor.low();
    rightBackMotor.low();
    rightFrontMotor.low();
    gpio.unprovisionPin(leftBackMotor);
    gpio.unprovisionPin(rightBackMotor);
    gpio.unprovisionPin(rightFrontMotor);
    gpio.unprovisionPin(leftFrontMotor);

    return "Program was shutdown correctly";
  }
예제 #3
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();
  }
예제 #4
0
 /**
  * This method can be called to forcefully shutdown all GPIO controller monitoring, listening,
  * and task threads/executors.
  */
 @Override
 public synchronized void shutdown() {
   delegate.shutdown();
 }
  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");
  }
  /**
   * @param args --none-
   * @throws InterruptedException
   */
  public static void main(String[] args) throws InterruptedException {

    // 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 -->", "IFTTT Maker Channel Trigger Example");

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

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

    // provision gpio pin as an input pin
    final GpioPinDigitalInput input =
        gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, "MyInputPin", PinPullResistance.PULL_DOWN);

    // provide 500ms of pin debounce protection
    input.setDebounce(500);
    ;

    // -----------------------------------------
    // Add the IFTTT Maker Channel Trigger
    // (This is where the magic happens)
    // -----------------------------------------
    input.addTrigger(
        new IFTTTMakerChannelTrigger(
            IFTTT_MAKER_CHANNEL_API_KEY, // <<-- PROVIDE YOUR ACCOUNT SPECIFIC IFTTT MAKER CHANNEL
                                         // API KEY
            IFTTT_MAKER_CHANNEL_EVENT_NAME, // <<-- PROVIDE THE IFTTT MAKER CHANNEL EVENT NAME
                                            // (defined in your IFTTTT recipe)
            PinState.HIGH, // <<-- OPTIONALLY DEFINE A SPECIFIC STATE TO TRIGGER ON

            // OPTIONALLY REGISTER A TRIGGER CALLBACK LISTENER
            // (Note: this callback parameter is not required for basic functionality)
            new IFTTTMakerChannelTriggerListener() {
              @Override
              public boolean onTriggered(IFTTTMakerChannelTriggerEvent event) {

                // The IFTTT Maker Channel API accepts three value parameters (value1, value2, and
                // value3)
                // By default, Pi4J applies the following values to each:
                //
                // "value1" = {pin-name}
                // "value2" = {pin-state-value} (as an Integer; 0==LOW, 1==HIGH)
                // "value3" = {json-payload}  Example:
                // {
                //    "pin": {
                //        "name": "MyInputPin",
                //        "address": "0",
                //        "provider": "RaspberryPi GPIO Provider",
                //        "mode": "input",
                //        "direction": "IN",
                //        "pull": "down"
                //    },
                //    "state": {
                //        "name": "HIGH",
                //        "value": "1",
                //        "is-high": "true",
                //        "is-low": "false"
                //    },
                //    "timestamp": "2016-04-15T17:32:49.666-0400"
                // }
                //
                // However, you can override any of these defaults in your callback listener by
                // applying new string values via the 'setValueX()' methods on the event object.
                //
                // Example:
                if (event.getValue2().equals("1")) {
                  event.setValue2("ON");
                } else {
                  event.setValue2("OFF");
                }

                // display event trigger details on screen
                console.println(" --> IFTTT MAKER CHANNEL EVENT TRIGGER");
                console.println("     - GPIO PIN            : " + event.getPin());
                console.println("     - PIN STATE           : " + event.getState());
                console.println("     - IFTTT EVENT NAME    : " + event.getEventName());
                console.println("     - IFTTT EVENT VALUE 1 : " + event.getValue1());
                console.println("     - IFTTT EVENT VALUE 2 : " + event.getValue2());
                console.println("     - IFTTT EVENT VALUE 3 : " + event.getValue3());
                console.emptyLine();

                // MAKE SURE TO RETURN 'true' TO CONTINUE WITH THE IFTTT MAKER CHANNEL API CALL
                // (you can optionally return 'false' if you want to abort the IFTTT API call)
                return true;
              }
            }));

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

    // prompt user that we are ready
    console.println(
        "Successfully provisioned ["
            + input
            + "] with PULL resistance = ["
            + input.getPullResistance()
            + "]");
    console.emptyLine();

    // wait for user to exit by pressing CTRL-C
    console.waitForExit();

    // 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();
  }
예제 #7
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".
   *
   * @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 BananaPi platform.
    //
    // ####################################################################
    PlatformManager.setPlatform(Platform.BANANAPI);

    // 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 Output 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 BananaPiPin 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(
            BananaPiPin.class, // pin provider class to obtain pin instance from
            BananaPiPin.GPIO_01, // default pin if no pin argument found
            args); // argument array to search in

    // provision gpio pin as an output pin and turn on
    final GpioPinDigitalOutput output =
        gpio.provisionDigitalOutputPin(pin, "My Output", PinState.HIGH);

    // set shutdown state for this pin: keep as output pin, set to low state
    output.setShutdownOptions(false, PinState.LOW);

    // create a pin listener to print out changes to the output gpio pin state
    output.addListener(
        new GpioPinListenerDigital() {
          @Override
          public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
            // display pin state on console
            console.println(
                " --> GPIO PIN STATE CHANGE: "
                    + event.getPin()
                    + " = "
                    + ConsoleColor.conditional(
                        event.getState().isHigh(), // conditional expression
                        ConsoleColor.GREEN, // positive conditional color
                        ConsoleColor.RED, // negative conditional color
                        event.getState())); // text to display
          }
        });

    // prompt user that we are ready
    console.println(" ... Successfully provisioned output pin: " + output.toString());
    console.emptyLine();
    console.box("The GPIO output pin states will cycle HIGH and LOW states now.");
    console.emptyLine();

    // notify user of current pin state
    console.println(
        "--> ["
            + output.toString()
            + "] state was provisioned with state = "
            + ConsoleColor.conditional(
                output.getState().isHigh(), // conditional expression
                ConsoleColor.GREEN, // positive conditional color
                ConsoleColor.RED, // negative conditional color
                output.getState())); // text to display

    // wait
    Thread.sleep(500);

    // --------------------------------------------------------------------------

    // tset gpio pin state to LOW
    console.emptyLine();
    console.println("Setting output pin state is set to LOW.");
    output.low(); // or ... output.setState(PinState.LOW);

    // wait
    Thread.sleep(500);

    // --------------------------------------------------------------------------

    // tset gpio pin state to HIGH
    console.emptyLine();
    console.println("Setting output pin state from LOW to HIGH.");
    output.setState(PinState.HIGH); // or ... output.high();

    // wait
    Thread.sleep(500);

    // --------------------------------------------------------------------------

    // toggle the current state of gpio pin (from HIGH to LOW)
    console.emptyLine();
    console.println("Toggling output pin state from HIGH to LOW.");
    output.toggle();

    // wait
    Thread.sleep(500);

    // --------------------------------------------------------------------------

    // pulse gpio pin state for 1 second HIGH and then return to LOW
    console.emptyLine();
    console.println("Pulsing output pin state HIGH for 1 second.");
    output.pulse(1000, true); // set second argument to 'true' use a blocking call
    Thread.sleep(50);

    // --------------------------------------------------------------------------

    // blink gpio pin state for 1 second between HIGH and LOW states
    console.emptyLine();
    console.println(
        "Blinking output pin state between HIGH and LOW for 3 seconds with a blink rate of 250ms.");
    Future future = output.blink(250, 3000);

    // --------------------------------------------------------------------------

    // wait for blinking to finish; we are notified in a future object
    while (!future.isDone()) {
      Thread.sleep(50);
    }

    // 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();
  }
예제 #8
0
  public static void main(String args[]) throws InterruptedException {

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

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

    // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
    final GpioPinDigitalInput myButton =
        gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

    // create custom Olimex GPIO provider
    final OlimexAVRIOGpioProvider olimexProvider =
        new OlimexAVRIOGpioProvider(Serial.DEFAULT_COM_PORT);

    // provision gpio input pin #01 from Olimex
    final GpioPinDigitalInput myInput =
        gpio.provisionDigitalInputPin(olimexProvider, OlimexAVRIOPin.IN_01);

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

    // register gpio pin listener for each input pin
    myButton.addListener(listener);
    myInput.addListener(listener);

    // setup gpio pins #04, #05, #06 as an output pins and make sure they are all LOW at startup
    GpioPinDigitalOutput myRelays[] = {
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_01, "RELAY #1", PinState.LOW),
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_02, "RELAY #2", PinState.LOW),
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_03, "RELAY #3", PinState.LOW),
      gpio.provisionDigitalOutputPin(
          olimexProvider, OlimexAVRIOPin.RELAY_04, "RELAY #4", PinState.LOW)
    };

    // create a gpio control trigger on the input pin ; when the input goes HIGH, also set gpio pin
    // #04 to HIGH
    myButton.addTrigger(new GpioSetStateTrigger(PinState.HIGH, myRelays[0], PinState.HIGH));

    // create a gpio control trigger on the input pin ; when the input goes LOW, also set gpio pin
    // #04 to LOW
    myButton.addTrigger(new GpioSetStateTrigger(PinState.LOW, myRelays[0], PinState.LOW));

    // create a gpio synchronization trigger on the input pin; when the input changes, also set gpio
    // pin #05 to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myRelays[1]));

    // create a gpio synchronization trigger on the input pin; when the input changes, also set gpio
    // pin #05 to same state
    myButton.addTrigger(new GpioSyncStateTrigger(myRelays[2]));

    // create a gpio pulse trigger on the input pin; when the input goes HIGH, also pulse gpio pin
    // #06 to the HIGH state for 1 second
    myButton.addTrigger(new GpioPulseStateTrigger(PinState.HIGH, myRelays[3], 1000));

    System.out.println(
        " ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

    // keep program running until user aborts (CTRL-C)
    // or we reach 60 seconds
    for (int seconds = 0; seconds < 60; seconds++) {
      Thread.sleep(1000);
    }

    System.out.println(" ... exiting program.");

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