001/**
002 * Copyright (c) 2014-2015 Digi International Inc.,
003 * All rights not expressly granted are reserved.
004 *
005 * This Source Code Form is subject to the terms of the Mozilla Public
006 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
007 * You can obtain one at http://mozilla.org/MPL/2.0/.
008 *
009 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
010 * =======================================================================
011 */
012package com.digi.xbee.api.io;
013
014import java.util.HashMap;
015
016/**
017 * Enumerates the different Input/Output modes that an IO line can be 
018 * configured with.
019 * 
020 * @see IOLine
021 */
022public enum IOMode {
023
024        // Enumeration types.
025        DISABLED(0, "Disabled"),
026        SPECIAL_FUNCTIONALITY(1, "Firmware special functionality"),
027        PWM(2, "PWM output"),
028        ADC(2, "Analog to Digital Converter"),
029        DIGITAL_IN(3, "Digital input"),
030        DIGITAL_OUT_LOW(4, "Digital output, Low"),
031        DIGITAL_OUT_HIGH(5, "Digital output, High");
032        
033        // Variables
034        private final static HashMap <Integer, IOMode> lookupTable = new HashMap<Integer, IOMode>();
035        
036        private final int id;
037        
038        private final String name;
039        
040        static {
041                for (IOMode ioMode:values())
042                        lookupTable.put(ioMode.getID(), ioMode);
043        }
044        
045        /**
046         * Class constructor. Instantiates a new {@code IOMode} enumeration entry 
047         * with the given parameters.
048         * 
049         * @param id IO mode ID.
050         * @param name IO mode name.
051         */
052        private IOMode(int id, String name) {
053                this.id = id;
054                this.name = name;
055        }
056        
057        /**
058         * Returns the IO mode ID.
059         * 
060         * @return IO mode ID.
061         */
062        public int getID() {
063                return id;
064        }
065        
066        /**
067         * Returns the IO mode name.
068         * 
069         * @return IO mode name.
070         */
071        public String getName() {
072                return name;
073        }
074        
075        /**
076         * Returns the {@code IOMode} associated to the provided mode ID.
077         * 
078         * @param modeID The ID of the {@code IOMode} to retrieve.
079         * 
080         * @return The {@code IOMode} associated to the provided mode ID.
081         */
082        public static IOMode getIOMode(int modeID) {
083                return getIOMode(modeID, null);
084        }
085        
086        /**
087         * Returns the {@code IOMode} corresponding to the provided mode ID and 
088         * IO line.
089         * 
090         * @param modeID The ID of the {@code IOMode} to retrieve.
091         * @param ioLine The IO line to retrieve its {@code IOMode}.
092         * 
093         * @return The {@code IOMode} corresponding to the provided mode ID and 
094         *         IO line.
095         * 
096         * @see IOLine
097         */
098        public static IOMode getIOMode(int modeID, IOLine ioLine) {
099                // If IO line is provided and IO value is 2, check PWM capability.
100                if (modeID == ADC.getID()) {
101                        if (ioLine != null && ioLine.hasPWMCapability())
102                                return PWM;
103                        return ADC;
104                }
105                // Look for the value in the table.
106                if (lookupTable.containsKey(modeID))
107                        return lookupTable.get(modeID);
108                return null;
109        }
110        
111        /*
112         * (non-Javadoc)
113         * @see java.lang.Enum#toString()
114         */
115        @Override
116        public String toString() {
117                return name;
118        }
119}