internal static void OpenPin(GpioController controller, int pinNumber, bool asOutput, ref GpioPin pin)
 {
     GpioOpenStatus statusResult;
     if (!controller.TryOpenPin(pinNumber, GpioSharingMode.Exclusive, out pin, out statusResult))
         throw new Exception(string.Format("There was an exception opening GPIO pin {0}. The operation returned a status of {1}", pinNumber, statusResult.ToString()));
     // Pins default to open as input.
     if (asOutput)
     {
         pin.SetDriveMode(GpioPinDriveMode.Output);
         pin.Write(GpioPinValue.High);
     }
     else
         pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
 }
Ejemplo n.º 2
0
        private void InitializeGpio()
        {
             _gpioController = GpioController.GetDefault();
            List<int> keysToRemove = new List<int>();

            foreach (int key in GpioPinDictionary.Keys)
            {
                GpioPin gpioPin;
                GpioOpenStatus status;
                _gpioController.TryOpenPin(key, GpioSharingMode.SharedReadOnly, out gpioPin, out status);

                if (status == GpioOpenStatus.PinOpened)
                {
                    Pin pin = GpioPinDictionary[key];
                    pin.GpioPin = gpioPin;
                    pin.IsActive = gpioPin.Read() == GpioPinValue.High;                    
                    gpioPin.ValueChanged += GpioPinOnValueChanged;
                } else if (status == GpioOpenStatus.PinUnavailable)
                {
                    keysToRemove.Add(key);
                }
            }

            foreach (int i in keysToRemove)
            {
                GpioPinDictionary[i].IsUnavailable = true;                
                GpioPinDictionary.Remove(i);
            }
        }