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 IO lines that can be found in the XBee devices. 
022 * 
023 * <p>Depending on the hardware and firmware of the device, the number of lines 
024 * that can be used as well as their functionality may vary. Refer to the 
025 * product manual to learn more about the IO lines of your XBee device.</p>
026 */
027public enum IOLine {
028
029        // Enumeration types.
030        DIO0_AD0("DIO0/AD0", 0, "D0", null),
031        DIO1_AD1("DIO1/AD1", 1, "D1", null),
032        DIO2_AD2("DIO2/AD2", 2, "D2", null),
033        DIO3_AD3("DIO3/AD3", 3, "D3", null),
034        DIO4_AD4("DIO4/AD4", 4, "D4", null),
035        DIO5_AD5("DIO5/AD5", 5, "D5", null),
036        DIO6("DIO6", 6, "D6", null),
037        DIO7("DIO7", 7, "D7", null),
038        DIO8("DIO8", 8, "D8", null),
039        DIO9("DIO9", 9, "D9", null),
040        DIO10_PWM0("DIO10/PWM0", 10, "P0", "M0"),
041        DIO11_PWM1("DIO11/PWM1", 11, "P1", "M1"),
042        DIO12("DIO12", 12, "P2", null),
043        DIO13("DIO13", 13, "P3", null),
044        DIO14("DIO14", 14, "P4", null),
045        DIO15("DIO15", 15, "P5", null),
046        DIO16("DIO16", 16, "P6", null),
047        DIO17("DIO17", 17, "P7", null),
048        DIO18("DIO18", 18, "P8", null),
049        DIO19("DIO19", 19, "P9", null);
050        
051        // Variables.
052        private final static HashMap <Integer, IOLine> lookupTableIndex = new HashMap<Integer, IOLine>();
053        
054        private final String name;
055        private final String atCommand;
056        private final String atPWMCommand;
057        
058        private final int index;
059        
060        static {
061                for (IOLine dio:values())
062                        lookupTableIndex.put(dio.getIndex(), dio);
063        }
064        
065        /**
066         * Class constructor. Instantiates a new {@code IOLine} enumeration entry 
067         * with the given parameters.
068         * 
069         * @param name The name of the IO line.
070         * @param index The index associated to the IO line.
071         * @param atCommand The AT command corresponding to the IO line.
072         * @param atPWMCommand The PWM AT command corresponding to the IO line 
073         *                     (if any).
074         */
075        private IOLine(String name, int index, String atCommand, String atPWMCommand) {
076                this.name = name;
077                this.index = index;
078                this.atCommand = atCommand;
079                this.atPWMCommand = atPWMCommand;
080        }
081        
082        /**
083         * Returns the name of the IO line.
084         * 
085         * @return The name of the IO line.
086         */
087        public String getName() {
088                return name;
089        }
090        
091        /**
092         * Returns the index of the IO line.
093         * 
094         * @return The index of the IO line.
095         */
096        public int getIndex() {
097                return index;
098        }
099        
100        /**
101         * Returns the configuration AT command associated to the IO line.
102         * 
103         * @return The configuration AT command associated to the IO line.
104         */
105        public String getConfigurationATCommand() {
106                return atCommand;
107        }
108        
109        /**
110         * Returns whether or not the IO line has PWM capability.
111         * 
112         * @return {@code true} if the provided IO line has PWM, {@code false} 
113         *         otherwise.
114         */
115        public boolean hasPWMCapability() {
116                return atPWMCommand != null;
117        }
118        
119        /**
120         * Returns the PWM AT command associated to the IO line.
121         * 
122         * @return The PWM AT command associated to the IO line.
123         */
124        public String getPWMDutyCycleATCommand() {
125                return atPWMCommand;
126        }
127        
128        /**
129         * Returns the {@code IOLine} associated to the given index.
130         * 
131         * @param index The index corresponding to the {@code IOLine} to retrieve.
132         * 
133         * @return The {@code IOLine} associated to the given index.
134         */
135        public static IOLine getDIO(int index) {
136                if (lookupTableIndex.containsKey(index))
137                        return lookupTableIndex.get(index);
138                return null;
139        }
140        
141        /*
142         * (non-Javadoc)
143         * @see java.lang.Enum#toString()
144         */
145        @Override
146        public String toString() {
147                return name;
148        }
149}