Ejemplo n.º 1
0
        // Selections for the D2A actuator:
        // element 0 ->   Means change to the first wiper position.
        //
        // element 1 ->   Means change to the second wiper position.
        //
        //    .
        //    .
        //    .
        // last element 255? -> Means change to the last wiper position.

        /// <summary>
        /// Initializes the actuator </summary>
        /// <param name="Init"> The initialization string.
        /// </param>
        /// <exception cref="OneWireException">
        ///  </exception>
        public virtual void initActuator()
        {
            PotentiometerContainer pc = (PotentiometerContainer)DeviceContainer;
            int    numOfWiperSettings;
            int    resistance;
            double offset = 0.6; // this seems about right...
            double wiperResistance;
            string selectionString;

            // initialize the ActuatorSelections Vector
            // must first read the device
            byte[] state = pc.readDevice();
            // set current wiper number from xml tag "channel"
            pc.setCurrentWiperNumber(Channel, state);
            // now, write to device to set the wiper number
            pc.writeDevice(state);
            // now, extract some values to initialize the ActuatorSelections
            // get the number of wiper positions
            numOfWiperSettings = pc.numberOfWiperSettings(state);
            // get the resistance value in k-Ohms
            resistance = pc.potentiometerResistance(state);
            // calculate wiper resistance
            wiperResistance = (double)((double)(resistance - offset) / (double)numOfWiperSettings);
            // add the values to the ActuatorSelections Vector
            selectionString = resistance + " k-Ohms"; // make sure the first
            ActuatorSelections.Add(selectionString);  // element is the entire resistance
            for (int i = (numOfWiperSettings - 2); i > -1; i--)
            {
                double newWiperResistance = (double)(wiperResistance * (double)i);
                // round the values before putting them in drop-down list
                int roundedWiperResistance = (int)((newWiperResistance + offset) * 10000);
                selectionString = (double)((double)roundedWiperResistance / 10000.0) + " k-Ohms";
                ActuatorSelections.Add(selectionString);
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// Method main
    ///
    /// </summary>
    /// <param name="args">
    /// </param>
    /// <exception cref="OneWireException"> </exception>
    /// <exception cref="OneWireIOException">
    ///  </exception>
    public static void Main1(string[] args)
    {
        bool          usedefault   = false;
        DSPortAdapter access       = null;
        string        adapter_name = null;
        string        port_name    = null;

        if ((args == null) || (args.Length < 1))
        {
            try
            {
                access = OneWireAccessProvider.DefaultAdapter;

                if (access == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Debug.WriteLine("Couldn't get default adapter!");
                printUsageString();

                return;
            }

            usedefault = true;
        }

        if (!usedefault)
        {
            string[] st = args[0].Split(new char[] { '_' });

            if (st.Length != 2)
            {
                printUsageString();

                return;
            }

            adapter_name = st[0];
            port_name    = st[1];

            Debug.WriteLine("Adapter Name: " + adapter_name);
            Debug.WriteLine("Port Name: " + port_name);
        }

        if (access == null)
        {
            try
            {
                access = OneWireAccessProvider.getAdapter(adapter_name, port_name);
            }
            catch (Exception)
            {
                Debug.WriteLine("That is not a valid adapter/port combination.");

                System.Collections.IEnumerator en = OneWireAccessProvider.enumerateAllAdapters();

                while (en.MoveNext())
                {
                    DSPortAdapter temp = (DSPortAdapter)en.Current;

                    Debug.WriteLine("Adapter: " + temp.AdapterName);

                    System.Collections.IEnumerator f = temp.PortNames;

                    while (f.MoveNext())
                    {
                        Debug.WriteLine("   Port name : " + ((string)f.Current));
                    }
                }

                return;
            }
        }

        access.adapterDetected();
        access.targetAllFamilies();
        access.beginExclusive(true);
        access.reset();
        access.setSearchAllDevices();

        bool next = access.findFirstDevice();

        if (!next)
        {
            Debug.WriteLine("Could not find any iButtons!");

            return;
        }

        while (next)
        {
            OneWireContainer owc = access.DeviceContainer;

            Debug.WriteLine("====================================================");
            Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + "          =");
            Debug.WriteLine("====================================================");
            Debug.WriteLine("=");

            bool isPotContainer       = false;
            PotentiometerContainer pc = null;

            try
            {
                pc             = (PotentiometerContainer)owc;
                isPotContainer = true;
            }
            catch (InvalidCastException)
            {
                pc             = null;
                isPotContainer = false;         //just to reiterate
            }

            if (isPotContainer)
            {
                Debug.WriteLine("= This device is a " + owc.Name);
                Debug.WriteLine("= Also known as a " + owc.AlternateNames);
                Debug.WriteLine("=");
                Debug.WriteLine("= It is a Potentiometer Container");

                byte[] state       = pc.readDevice();
                bool   charge      = pc.isChargePumpOn(state);
                int    wiper       = pc.getCurrentWiperNumber(state);
                int    position    = pc.WiperPosition;
                bool   linear      = pc.isLinear(state);
                int    numPots     = pc.numberOfPotentiometers(state);
                int    numSettings = pc.numberOfWiperSettings(state);
                int    resistance  = pc.potentiometerResistance(state);

                Debug.WriteLine("=");
                Debug.WriteLine("= Charge pump is " + (charge ? "ON" : "OFF"));
                Debug.WriteLine("= This device has " + numPots + " potentiometer" + ((numPots > 1) ? "s" : ""));
                Debug.WriteLine("= This device has a " + (linear ? "linear" : "logarithmic") + " potentiometer");
                Debug.WriteLine("= It uses " + numSettings + " potentiometer wiper settings");
                Debug.WriteLine("= The potentiometer resistance is " + resistance + " kOhms");
                Debug.WriteLine("= CURRENT WIPER NUMBER  : " + wiper);
                Debug.WriteLine("= CURRENT WIPER POSITION: " + position);
                Debug.WriteLine("= Trying to toggle the charge pump...");
                pc.setChargePump(!charge, state);
                pc.writeDevice(state);

                state = pc.readDevice();

                if (charge == pc.isChargePumpOn(state))
                {
                    Debug.WriteLine("= Could not toggle charge pump.  Must have external power supplied.");
                }
                else
                {
                    Debug.WriteLine("= Toggled charge pump successfully");
                }

                //int newwiper = (int)((DateTime.Now.Ticks & 0x0ff00) >> 8);
                //pc.WiperPosition = newwiper;
                //Debug.WriteLine("= Setting wiper position to " + (newwiper));
                Debug.WriteLine("= CURRENT WIPER POSITION: " + pc.WiperPosition);
            }
            else
            {
                Debug.WriteLine("= This device is not a potentiometer device.");
                Debug.WriteLine("=");
                Debug.WriteLine("=");
            }

            next = access.findNextDevice();
        }
    }