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