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