001/**
002 * Copyright 2017, Digi International Inc.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015 */
016package com.digi.xbee.api.io;
017
018import java.util.HashMap;
019
020/**
021 * Enumerates the different Input/Output modes that an IO line can be 
022 * configured with.
023 * 
024 * @see IOLine
025 */
026public enum IOMode {
027
028        // Enumeration types.
029        DISABLED(0, "Disabled"),
030        SPECIAL_FUNCTIONALITY(1, "Firmware special functionality"),
031        PWM(2, "PWM output"),
032        ADC(2, "Analog to Digital Converter"),
033        DIGITAL_IN(3, "Digital input"),
034        DIGITAL_OUT_LOW(4, "Digital output, Low"),
035        DIGITAL_OUT_HIGH(5, "Digital output, High");
036        
037        // Variables
038        private final static HashMap <Integer, IOMode> lookupTable = new HashMap<Integer, IOMode>();
039        
040        private final int id;
041        
042        private final String name;
043        
044        static {
045                for (IOMode ioMode:values())
046                        lookupTable.put(ioMode.getID(), ioMode);
047        }
048        
049        /**
050         * Class constructor. Instantiates a new {@code IOMode} enumeration entry 
051         * with the given parameters.
052         * 
053         * @param id IO mode ID.
054         * @param name IO mode name.
055         */
056        private IOMode(int id, String name) {
057                this.id = id;
058                this.name = name;
059        }
060        
061        /**
062         * Returns the IO mode ID.
063         * 
064         * @return IO mode ID.
065         */
066        public int getID() {
067                return id;
068        }
069        
070        /**
071         * Returns the IO mode name.
072         * 
073         * @return IO mode name.
074         */
075        public String getName() {
076                return name;
077        }
078        
079        /**
080         * Returns the {@code IOMode} associated to the provided mode ID.
081         * 
082         * @param modeID The ID of the {@code IOMode} to retrieve.
083         * 
084         * @return The {@code IOMode} associated to the provided mode ID.
085         */
086        public static IOMode getIOMode(int modeID) {
087                return getIOMode(modeID, null);
088        }
089        
090        /**
091         * Returns the {@code IOMode} corresponding to the provided mode ID and 
092         * IO line.
093         * 
094         * @param modeID The ID of the {@code IOMode} to retrieve.
095         * @param ioLine The IO line to retrieve its {@code IOMode}.
096         * 
097         * @return The {@code IOMode} corresponding to the provided mode ID and 
098         *         IO line.
099         * 
100         * @see IOLine
101         */
102        public static IOMode getIOMode(int modeID, IOLine ioLine) {
103                // If IO line is provided and IO value is 2, check PWM capability.
104                if (modeID == ADC.getID()) {
105                        if (ioLine != null && ioLine.hasPWMCapability())
106                                return PWM;
107                        return ADC;
108                }
109                // Look for the value in the table.
110                if (lookupTable.containsKey(modeID))
111                        return lookupTable.get(modeID);
112                return null;
113        }
114        
115        /*
116         * (non-Javadoc)
117         * @see java.lang.Enum#toString()
118         */
119        @Override
120        public String toString() {
121                return name;
122        }
123}