// Path: src/main/java/org/mandfer/sunfunpi4j/BaseSketch.java
// protected static final Logger logger = LoggerFactory.getLogger("Sketch");
// 
// Path: src/main/java/org/mandfer/sunfunpi4j/BaseSketch.java
// protected static void wiringPiSetup(){
//     if (Gpio.wiringPiSetup() == -1) {
//         String msg = "==>> GPIO SETUP FAILED";
//         logger.debug(msg);
//         throw new RuntimeException(msg);
//     }
// }


import java.util.logging.Logger;
import static org.mandfer.sunfunpi4j.BaseSketch.logger;
import static org.mandfer.sunfunpi4j.BaseSketch.wiringPiSetup;
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.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import java.util.logging.Level;

/**
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Marc Andreu
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package org.mandfer.sunfunpi4j;


/**
 *
 * @author marcandreuf
 */
public class Ex13_ButtonListener extends BaseSketch {    
    
    public static void main(String[] args) throws InterruptedException {
        Ex13_ButtonListener sketch = new Ex13_ButtonListener(GpioFactory.getInstance());
        sketch.run(args);
    }

    private GpioPinDigitalInput myButton;
    private GpioPinDigitalOutput ledPin;
    
    /**
     * @param gpio controller 
     */
    public Ex13_ButtonListener(GpioController gpio){
        super(gpio);
    }
    
    @Override
    protected void setup(String[] args) {
        wiringPiSetup();
        ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);        
        myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN);        
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                if(event.getState().isLow()){

// Path: src/main/java/org/mandfer/sunfunpi4j/BaseSketch.java
// protected static final Logger logger = LoggerFactory.getLogger("Sketch");
// 
// Path: src/main/java/org/mandfer/sunfunpi4j/BaseSketch.java
// protected static void wiringPiSetup(){
//     if (Gpio.wiringPiSetup() == -1) {
//         String msg = "==>> GPIO SETUP FAILED";
//         logger.debug(msg);
//         throw new RuntimeException(msg);
//     }
// }


// Path: src/main/java/org/mandfer/sunfunpi4j/Ex13_ButtonListener.java
import java.util.logging.Logger;
import static org.mandfer.sunfunpi4j.BaseSketch.logger;
import static org.mandfer.sunfunpi4j.BaseSketch.wiringPiSetup;
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.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import java.util.logging.Level;

/**
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Marc Andreu
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package org.mandfer.sunfunpi4j;


/**
 *
 * @author marcandreuf
 */
public class Ex13_ButtonListener extends BaseSketch {    
    
    public static void main(String[] args) throws InterruptedException {
        Ex13_ButtonListener sketch = new Ex13_ButtonListener(GpioFactory.getInstance());
        sketch.run(args);
    }

    private GpioPinDigitalInput myButton;
    private GpioPinDigitalOutput ledPin;
    
    /**
     * @param gpio controller 
     */
    public Ex13_ButtonListener(GpioController gpio){
        super(gpio);
    }
    
    @Override
    protected void setup(String[] args) {
        wiringPiSetup();
        ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);        
        myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN);        
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                if(event.getState().isLow()){

                    logger.debug("Button is pressed");
