/// <summary>
        /// Export the GPIO setting the direction.
        /// </summary>
        /// <param name="pin">
        /// The pin to export.
        /// </param>
        /// <param name="mode">
        /// The I/0 mode of the pin.
        /// </param>
        private static void internal_ExportPin(Int32 pin, PinMode mode)
        {
            Initialize();

            // If the pin is already exported, check it's in the proper direction.
            if (ExportedPins.ContainsKey(pin))
            {
                // If the direction matches, return out of the function. If not,
                // change the direction.
                if (ExportedPins[pin] == mode)
                {
                    return;
                }
            }

            // Set the direction on the pin and update the exported list.
            // BCM2835_GPIO_FSEL_INPT = 0
            // BCM2835_GPIO_FSEL_OUTP = 1
            uint pindir = (mode == PinMode.IN) ? (uint)0 : (uint)1;

            UnsafeNativeMethods.bcm2835_gpio_fsel((uint)pin, pindir);
            if (mode == PinMode.IN)
            {
                // BCM2835_GPIO_PUD_OFF = 0b00 = 0
                // BCM2835_GPIO_PUD_DOWN = 0b01 = 1
                // BCM2835_GPIO_PUD_UP = 0b10 = 2
                UnsafeNativeMethods.bcm2835_gpio_set_pud((uint)pin, 0);
            }
            ExportedPins[pin] = mode;
        }
Exemple #2
0
        /// <summary>
        /// Exports the GPIO setting the direction. This creates the
        /// /sys/class/gpio/gpioXX directory.
        /// </summary>
        /// <param name="pin">
        /// The GPIO pin.
        /// </param>
        /// <param name="mode">
        /// The I/O mode.
        /// </param>
        /// <param name="pinnum">
        /// The pin number.
        /// </param>
        /// <param name="pinname">
        /// The name of the pin.
        /// </param>
        private static void internal_ExportPin(Int32 pin, PinMode mode, String pinnum, String pinname)
        {
            String pinpath = GPIO_PATH + "gpio" + pinnum;
            String m       = Enum.GetName(typeof(PinMode), mode);

            // If the pin is already exported, check it's in the proper direction.
            if (ExportedPins.ContainsKey(pin))
            {
                // If the direction matches, return out of the function. If not,
                // change the direction.
                if (ExportedPins[pin] == mode)
                {
                    return;
                }
                else
                {
                    // Set the direction on the pin and update the exported list.
                    File.WriteAllText(pinpath + "/direction", m);
                    ExportedPins[pin] = mode;
                    return;
                }
            }

            // Export.
            if (!Directory.Exists(pinpath))
            {
                Debug.WriteLine("Exporting pin " + pinnum);
                File.WriteAllText(GPIO_PATH + "export", pinnum);
            }

            // Set I/O direction.
            Debug.WriteLine("Setting direction on pin " + pinname + "/gpio" + pin.ToString() + " as " + m);
            File.WriteAllText(pinpath + "/direction", m);

            // Update the pin.
            ExportedPins[pin] = mode;
        }