001    /*----------------------------------------------------------------------------*/
002    /* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003    /* Open Source Software - may be modified and shared by FRC teams. The code   */
004    /* must be accompanied by the FIRST BSD license file in the root directory of */
005    /* the project.                                                               */
006    /*----------------------------------------------------------------------------*/
007    
008    package edu.wpi.first.wpilibj;
009    
010    import edu.wpi.first.wpilibj.fpga.tSolenoid;
011    import edu.wpi.first.wpilibj.parsing.IDeviceController;
012    
013    /**
014     * SolenoidBase class is the common base class for the Solenoid and
015     * DoubleSolenoid classes.
016     */
017    public abstract class SolenoidBase extends SensorBase implements IDeviceController {
018    
019        protected int m_moduleNumber; ///< The number of the solenoid module being used.
020        protected static Resource m_allocated = new Resource(tSolenoid.kDO7_0_NumElements * kSolenoidChannels);
021    
022        private static tSolenoid m_fpgaSolenoidModule; ///< FPGA Solenoid Module object.
023        private static int m_refCount; ///< Reference count for the chip object.
024    
025        /**
026         * Constructor.
027         *
028         * @param moduleNumber The number of the solenoid module to use.
029         */
030        public SolenoidBase(final int moduleNumber) {
031            m_moduleNumber = moduleNumber;
032            checkSolenoidModule(m_moduleNumber);
033    
034            m_refCount++;
035            if (m_refCount == 1) {
036                m_fpgaSolenoidModule = new tSolenoid();
037            }
038        }
039    
040        /**
041         * Destructor.
042         */
043        public synchronized void free() {
044            if (m_refCount == 1) {
045                m_fpgaSolenoidModule.Release();
046                m_fpgaSolenoidModule = null;
047            }
048            m_refCount--;
049        }
050    
051        /**
052         * Set the value of a solenoid.
053         *
054         * @param value The value you want to set on the module.
055         * @param mask The channels you want to be affected.
056         */
057        protected synchronized void set(int value, int mask) {
058            byte currentValue = (byte)tSolenoid.readDO7_0(m_moduleNumber - 1);
059            // Zero out the values to change
060            currentValue = (byte)(currentValue & ~mask);
061            currentValue = (byte)(currentValue | (value & mask));
062            tSolenoid.writeDO7_0(m_moduleNumber - 1, currentValue);
063        }
064    
065        /**
066         * Read all 8 solenoids from the module used by this solenoid as a single byte
067         *
068         * @return The current value of all 8 solenoids on this module.
069         */
070        public byte getAll() {
071            return (byte)tSolenoid.readDO7_0(m_moduleNumber - 1);
072        }
073    
074        /**
075         * Read all 8 solenoids in the default solenoid module as a single byte
076         *
077         * @return The current value of all 8 solenoids on the default module.
078         */
079        public static byte getAllFromDefaultModule() {
080            return getAllFromModule(getDefaultSolenoidModule());
081        }
082    
083        /**
084         * Read all 8 solenoids in the specified solenoid module as a single byte
085         *
086         * @return The current value of all 8 solenoids on the specified module.
087         */
088        public static byte getAllFromModule(int moduleNumber) {
089            checkSolenoidModule(moduleNumber);
090            return (byte)tSolenoid.readDO7_0(moduleNumber - 1);
091        }
092    }