Esempio n. 1
0
        /// <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;
        }
Esempio n. 2
0
 /// <summary>
 /// Unexports an exported pin.
 /// </summary>
 /// <param name="pin">
 /// The pin to unexport.
 /// </param>
 private static void internal_UnexportPin(Int32 pin)
 {
     Debug.WriteLine("Unexporting pin " + pin.ToString());
     // TODO Somehow reverse what we did in internal_ExportPin? Is there
     // a way to "free" the pin in the libbcm2835 library?
     UnsafeNativeMethods.bcm2835_gpio_write((uint)pin, (uint)PinState.Low);
     UnsafeNativeMethods.bcm2835_gpio_fsel((uint)pin, (uint)PinMode.TRI);
     ExportedPins.Remove(pin);
 }
Esempio n. 3
0
 /// <summary>
 /// Destroy this GPIO factory.
 /// </summary>
 public static void Destroy()
 {
     if (ExportedPins != null)
     {
         if (ExportedPins.Count > 0)
         {
             foreach (Int32 pin in ExportedPins.Keys)
             {
                 internal_UnexportPin(pin);
             }
             ExportedPins.Clear();
         }
     }
     UnsafeNativeMethods.bcm2835_close();
 }
Esempio n. 4
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;
        }
Esempio n. 5
0
 /// <summary>
 /// Unexport the GPIO. This removes the /sys/class/gpio/gpioXX directory.
 /// </summary>
 /// <param name="pin">
 /// The GPIO pin to unexport.
 /// </param>
 /// <param name="gpioNum">
 /// The GPIO number associated with the pin.
 /// </param>
 private static void internal_UnexportPin(Int32 pin, String gpioNum)
 {
     Debug.WriteLine("Unexporting pin " + pin.ToString());
     File.WriteAllText(GPIO_PATH + "unexport", gpioNum);
     ExportedPins.Remove(pin);
 }