Skip to content

Pi4J Comparison Code

tweej edited this page Jan 9, 2016 · 1 revision
// START SNIPPET: listen-gpio-snippet


/*
 * #%L
 * **********************************************************************
 * ORGANIZATION  :  Pi4J
 * PROJECT       :  Pi4J :: Java Examples
 * FILENAME      :  ListenGpioExample.java  
 * 
 * This file is part of the Pi4J project. More information about 
 * this project can be found here:  http://www.pi4j.com/
 * **********************************************************************
 * %%
 * Copyright (C) 2012 - 2015 Pi4J
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

/**
 * This example code demonstrates how to setup a listener
 * for GPIO pin state changes on the Raspberry Pi.  
 * 
 * @author Robert Savage
 */
public class ListenGpioExample {

	private static long beg;
	private static long accum=0; // microseconds
    
    public static void main(String args[]) throws InterruptedException {

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

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

        // create and register gpio pin listener
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
		        long now = System.nanoTime();

		        if(event.getState() == PinState.HIGH) {
        			long duration = (now - beg)/1000; // microseconds
        			accum += duration;
        			System.out.println("Latency: " + Long.toString(duration) + " microseconds");
        		}
            }
        });

        // provision gpio 28 as an output pin and make sure is is set to LOW at startup
        GpioPinDigitalOutput myPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02,   // PIN NUMBER
                                                     		        "My PIN",           // PIN FRIENDLY NAME (optional)
                                                          		    PinState.LOW);      // PIN STARTUP STATE (optional)
        
	    Thread.sleep(10);

    	int nIterations = 50;
	    for(int i = 0; i < nIterations; ++i) {
    		beg = System.nanoTime();
    		myPin.high();
    		Thread.sleep(30);
    
    		myPin.low();
    		Thread.sleep(30);
    	}

    	System.out.println("Average: " + Long.toString(accum/nIterations) + " microseconds");

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

// END SNIPPET: listen-gpio-snippet

Clone this wiki locally