001package com.pi4j.io.gpio.impl;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  GpioEventMonitorImpl.java  
009 * 
010 * This file is part of the Pi4J project. More information about 
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2013 Pi4J
015 * %%
016 * Licensed under the Apache License, Version 2.0 (the "License"); you
017 * may not use this file except in compliance with the License. You may obtain a copy of the License
018 * at
019 * 
020 * http://www.apache.org/licenses/LICENSE-2.0
021 * 
022 * Unless required by applicable law or agreed to in writing, software distributed under the License
023 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
024 * or implied. See the License for the specific language governing permissions and limitations under
025 * the License.
026 * #L%
027 */
028
029import com.pi4j.io.gpio.GpioPinInput;
030import com.pi4j.io.gpio.PinState;
031import com.pi4j.io.gpio.event.GpioPinListener;
032import com.pi4j.io.gpio.event.GpioPinAnalogValueChangeEvent;
033import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
034import com.pi4j.io.gpio.event.GpioPinListenerAnalog;
035import com.pi4j.io.gpio.event.GpioPinListenerDigital;
036import com.pi4j.io.gpio.event.PinAnalogValueChangeEvent;
037import com.pi4j.io.gpio.event.PinEvent;
038import com.pi4j.io.gpio.event.PinEventType;
039import com.pi4j.io.gpio.event.PinDigitalStateChangeEvent;
040import com.pi4j.io.gpio.event.PinListener;
041import com.pi4j.io.gpio.trigger.GpioTrigger;
042
043public class GpioEventMonitorImpl implements PinListener {
044    private final GpioPinInput pin;
045
046    public GpioEventMonitorImpl(GpioPinInput pin) {
047        this.pin = pin;
048    }
049    
050    @Override
051    public void handlePinEvent(PinEvent event) {
052        // only process listeners and triggers if the received interrupt event
053        // matches the pin number being tracked my this class instance 
054        if (this.pin.getPin().equals(event.getPin())) {
055            if (event.getEventType() == PinEventType.DIGITAL_STATE_CHANGE) {
056                PinState state = ((PinDigitalStateChangeEvent)event).getState();
057                
058                // process event callbacks for digital listeners
059                for (GpioPinListener listener : pin.getListeners()) {
060                    if (listener instanceof GpioPinListenerDigital) {                      
061                        ((GpioPinListenerDigital)listener).handleGpioPinDigitalStateChangeEvent(new GpioPinDigitalStateChangeEvent(event.getSource(), pin, state));
062                    }
063                }
064    
065                // process triggers
066                for (GpioTrigger trigger : pin.getTriggers()) {
067                    if (trigger.hasPinState(state)) {
068                        trigger.invoke(pin, state);
069                    }
070                }
071            } else if(event.getEventType() == PinEventType.ANALOG_VALUE_CHANGE) {
072                double value = ((PinAnalogValueChangeEvent)event).getValue();
073
074                // process event callbacks for analog listeners
075                for (GpioPinListener listener : pin.getListeners()) {
076                    if (listener instanceof GpioPinListenerAnalog) {                     
077                        ((GpioPinListenerAnalog)listener).handleGpioPinAnalogValueChangeEvent(new GpioPinAnalogValueChangeEvent(event.getSource(), pin, value));
078                    }
079                }
080            }
081        }
082    }
083}