// Path: photorama-raspberry-pi-webapp/src/main/java/org/onebeartoe/electronics/photorama/ConfigurationServlet.java
// public static final String CAMERA_KEY = "camera";
// 
// Path: photorama-raspberry-pi/src/main/java/org/onebeartoe/electronics/photorama/PhotoBoothButtonTester.java
// public static final Pin buttonPin = RaspiPin.GPIO_02;


import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import static org.onebeartoe.electronics.photorama.ConfigurationServlet.CAMERA_KEY;
import static org.onebeartoe.electronics.photorama.PhotoBoothButtonTester.buttonPin;
import org.onebeartoe.system.Sleeper;
            System.out.println("*Provisioning GPIO.");
            gpio = GpioFactory.getInstance();
            servletContext.setAttribute(PHOTO_BOOTH_GPIO_CONTROLLER_KEY, gpio);
            
            GpioPinDigitalInput photoBoothButton = gpio.provisionDigitalInputPin(buttonPin, 
                                                                             "photo booth button", 
                                                                             PinPullResistance.PULL_UP);   // works with the Adafruit massive arcade button
//                                                                           PinPullResistance.PULL_DOWN); // works with a tactile/simple push button
            
            System.out.println("*GPIO provisioned :)");
            photoBoothButton.addListener(this);
            servletContext.setAttribute(PHOTO_BOOTH_BUTTON_KEY, photoBoothButton);
        }
    }
    
    @Override
    public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) 
    {
        PinState state = event.getState();
        if(state == PinState.LOW)
        {
            System.out.println("Photo booth button pin state changed to LOW.");
            if(takingSnapshots)
            {
                String message = "The photobooth button was pressed, while already taking photos.";
                System.out.println(message);
            }
            else
            {
                ServletContext servletContext = getServletContext();
// Path: photorama-raspberry-pi-webapp/src/main/java/org/onebeartoe/electronics/photorama/ConfigurationServlet.java
// public static final String CAMERA_KEY = "camera";
// 
// Path: photorama-raspberry-pi/src/main/java/org/onebeartoe/electronics/photorama/PhotoBoothButtonTester.java
// public static final Pin buttonPin = RaspiPin.GPIO_02;


// Path: photorama-raspberry-pi-webapp/src/main/java/org/onebeartoe/electronics/photorama/PhotoBoothServlet.java
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import static org.onebeartoe.electronics.photorama.ConfigurationServlet.CAMERA_KEY;
import static org.onebeartoe.electronics.photorama.PhotoBoothButtonTester.buttonPin;
import org.onebeartoe.system.Sleeper;

            System.out.println("*Provisioning GPIO.");
            gpio = GpioFactory.getInstance();
            servletContext.setAttribute(PHOTO_BOOTH_GPIO_CONTROLLER_KEY, gpio);
            
            GpioPinDigitalInput photoBoothButton = gpio.provisionDigitalInputPin(buttonPin, 
                                                                             "photo booth button", 
                                                                             PinPullResistance.PULL_UP);   // works with the Adafruit massive arcade button
//                                                                           PinPullResistance.PULL_DOWN); // works with a tactile/simple push button
            
            System.out.println("*GPIO provisioned :)");
            photoBoothButton.addListener(this);
            servletContext.setAttribute(PHOTO_BOOTH_BUTTON_KEY, photoBoothButton);
        }
    }
    
    @Override
    public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) 
    {
        PinState state = event.getState();
        if(state == PinState.LOW)
        {
            System.out.println("Photo booth button pin state changed to LOW.");
            if(takingSnapshots)
            {
                String message = "The photobooth button was pressed, while already taking photos.";
                System.out.println(message);
            }
            else
            {
                ServletContext servletContext = getServletContext();
                Camera camera = (Camera) servletContext.getAttribute(CAMERA_KEY);