001package com.pi4j.io.gpio.impl;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  PinImpl.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 - 2016 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 java.util.EnumSet;
034
035import com.pi4j.io.gpio.Pin;
036import com.pi4j.io.gpio.PinEdge;
037import com.pi4j.io.gpio.PinMode;
038import com.pi4j.io.gpio.PinPullResistance;
039
040public class PinImpl implements Pin {
041
042    private final int address;
043    private final String name ;
044    private final String provider;
045    private final EnumSet<PinPullResistance> supportedPinPullResistance;
046    private final EnumSet<PinMode> supportedPinModes;
047    private final EnumSet<PinEdge> supportedPinEdges;
048
049    public PinImpl(String provider, int address, String name, EnumSet<PinMode> modes, EnumSet<PinPullResistance> pullResistance, EnumSet<PinEdge> pinEdges) {
050        this.provider = provider;
051        this.address = address;
052        this.name = name;
053        this.supportedPinModes = modes;
054        this.supportedPinPullResistance = pullResistance;
055        this.supportedPinEdges = pinEdges;
056    }
057
058    public PinImpl(String provider, int address, String name, EnumSet<PinMode> modes, EnumSet<PinPullResistance> pullResistance) {
059        this(provider, address, name, modes, pullResistance, EnumSet.allOf(PinEdge.class));
060    }
061
062    public PinImpl(String provider, int address, String name, EnumSet<PinMode> modes) {
063        this(provider, address, name, modes, null);
064    }
065
066    @Override
067    public int getAddress() {
068        return address;
069    }
070
071    @Override
072    public String getName() {
073        return name;
074    }
075
076    @Override
077    public String getProvider() {
078        return provider;
079    }
080
081    @Override
082    public String toString() {
083        return name;
084    }
085
086    @Override
087    public EnumSet<PinMode> getSupportedPinModes() {
088        if (supportedPinModes == null) {
089            return EnumSet.noneOf(PinMode.class);
090        }
091        return supportedPinModes;
092    }
093
094    @Override
095    public EnumSet<PinPullResistance> getSupportedPinPullResistance() {
096        if (supportedPinPullResistance == null) {
097            return EnumSet.noneOf(PinPullResistance.class);
098        }
099        return supportedPinPullResistance;
100    }
101
102    @Override
103    public boolean supportsPinPullResistance(){
104        return supportedPinPullResistance != null && !supportedPinPullResistance.isEmpty();
105    }
106
107    @Override
108    public EnumSet<PinEdge> getSupportedPinEdges(){
109        if (supportedPinEdges == null) {
110            return EnumSet.allOf(PinEdge.class);
111        }
112        return supportedPinEdges;
113    }
114
115    @Override
116    public boolean supportsPinEdges(){
117        return supportedPinEdges != null && !supportedPinEdges.isEmpty();
118    }
119
120    @Override
121    public boolean supportsPinEvents(){
122        return supportsPinEdges();
123    }
124
125    @Override
126    public int compareTo(Pin o) {
127        if(this.getAddress() < o.getAddress())
128            return -1;
129        else if(this.getAddress() > o.getAddress())
130            return 1;
131        else
132            return  0;
133    }
134
135    @Override
136    public boolean equals(Object obj) {
137        // matching object instance
138        if (obj == this)
139            return true;
140
141        // reject is not a pin instance
142        if (!(obj instanceof Pin))
143            return false;
144
145        // match on pin provider, name and address
146        Pin pin = (Pin) obj;
147        return (pin.getProvider().equals(getProvider()) &&
148                pin.getName() == getName() &&
149                pin.getAddress() == getAddress());
150    }
151}