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:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.io.gpio.GpioPinInput;
034import com.pi4j.io.gpio.PinState;
035import com.pi4j.io.gpio.event.*;
036import com.pi4j.io.gpio.trigger.GpioTrigger;
037
038import java.util.ArrayList;
039import java.util.Collection;
040
041public class GpioEventDispatchTaskImpl implements Runnable {
042
043    private final GpioPinInput pin;
044    private final PinEvent event;
045
046    public GpioEventDispatchTaskImpl(GpioPinInput pin, PinEvent event) {
047        this.event = event;
048        this.pin = pin;
049    }
050
051    @Override
052    public void run() {
053        // only process listeners and triggers if the received interrupt event
054        // matches the pin number being tracked my this class instance
055        if (this.pin.getPin().equals(event.getPin())) {
056            if (event.getEventType() == PinEventType.DIGITAL_STATE_CHANGE) {
057                PinState state = ((PinDigitalStateChangeEvent) event).getState();
058
059                // create a copy of the listeners collection
060                Collection<GpioPinListener> listeners  = new ArrayList<>(pin.getListeners());
061
062                // process event callbacks for digital listeners
063                for (GpioPinListener listener : listeners) {
064                    if(listener != null && listener instanceof GpioPinListenerDigital) {
065                        ((GpioPinListenerDigital) listener)
066                            .handleGpioPinDigitalStateChangeEvent(new GpioPinDigitalStateChangeEvent(
067                                    event.getSource(), pin, state));
068                    }
069                }
070
071                // create a copy of the triggers collection
072                Collection<GpioTrigger> triggers  = new ArrayList<>(pin.getTriggers());
073
074                // process triggers
075                for (GpioTrigger trigger : triggers) {
076                    if (trigger != null && trigger.hasPinState(state)) {
077                        trigger.invoke(pin, state);
078                    }
079                }
080            } else if (event.getEventType() == PinEventType.ANALOG_VALUE_CHANGE) {
081                double value = ((PinAnalogValueChangeEvent) event).getValue();
082
083                // create a copy of the listeners collection
084                Collection<GpioPinListener> listeners  = new ArrayList<>(pin.getListeners());
085
086                // process event callbacks for analog listeners
087                for (GpioPinListener listener : listeners) {
088                    if (listener != null && listener instanceof GpioPinListenerAnalog) {
089                        ((GpioPinListenerAnalog) listener)
090                            .handleGpioPinAnalogValueChangeEvent(new GpioPinAnalogValueChangeEvent(
091                                    event.getSource(), pin, value));
092                    }
093                }
094            }
095        }
096    }
097}