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