001package com.pi4j.io.gpio;
002
003import java.util.EnumSet;
004
005/*
006 * #%L
007 * **********************************************************************
008 * ORGANIZATION  :  Pi4J
009 * PROJECT       :  Pi4J :: Java Library (Core)
010 * FILENAME      :  PinState.java  
011 * 
012 * This file is part of the Pi4J project. More information about 
013 * this project can be found here:  http://www.pi4j.com/
014 * **********************************************************************
015 * %%
016 * Copyright (C) 2012 - 2013 Pi4J
017 * %%
018 * Licensed under the Apache License, Version 2.0 (the "License");
019 * you may not use this file except in compliance with the License.
020 * You may obtain a copy of the License at
021 * 
022 *      http://www.apache.org/licenses/LICENSE-2.0
023 * 
024 * Unless required by applicable law or agreed to in writing, software
025 * distributed under the License is distributed on an "AS IS" BASIS,
026 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
027 * See the License for the specific language governing permissions and
028 * limitations under the License.
029 * #L%
030 */
031
032/**
033 * Pin edge definition.
034 *
035 * @author Robert Savage (<a
036 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
037 */
038public enum PinState {
039
040    LOW(0, "LOW"), 
041    HIGH(1, "HIGH"); 
042
043    private final int value;
044    private final String name;
045
046    private PinState(int value, String name) {
047        this.value = value;
048        this.name = name;
049    }
050
051    public boolean isHigh() {
052        return (this == HIGH);
053    }
054
055    public boolean isLow() {
056        return (this == LOW);
057    }
058    
059    public int getValue() {
060        return value;
061    }
062
063    public String getName() {
064        return name;
065    }
066    
067    @Override
068    public String toString() {
069        return name;        
070    }    
071    
072    public static PinState getState(int state) {
073        for (PinState item : PinState.values()) {
074            if (item.getValue() == state) {
075                return item;
076            }
077        }
078        return null;
079    }
080
081    public static PinState getInverseState(PinState state) {
082        return (state == HIGH ? LOW : HIGH);
083    }
084    
085    public static PinState getState(boolean state) {
086        if (state == true) {
087            return PinState.HIGH;
088        } else {
089            return PinState.LOW;
090        }
091    }
092
093    public static PinState[] allStates() {
094        return PinState.values();
095    }    
096    
097    public static EnumSet<PinState> all() {
098        return EnumSet.of(PinState.HIGH, PinState.LOW);
099    }        
100}