예제 #1
0
 public bool TryOpenPin(Int32 pinNumber,
                        GpioSharingMode sharingMode,
                        GpioPin pin,
                        GpioOpenStatus openStatus)
 {
     return(false);
 }
        /// <summary>
        /// Opens the pin at the specified position.
        /// </summary>
        /// <param name="pin">Pin to open.</param>
        /// <param name="sharingMode">The pin's sharing mode.</param>
        /// <returns>A new IGPioPin instance for the specified pin.</returns>
        public IGpioPin OpenPin(int pin, GpioSharingMode sharingMode = GpioSharingMode.Exclusive)
        {
            IGpioPin gpioPin = null;
            if(pin < 64)
            {
                var sysPin = GpioController.GetDefault().OpenPin(pin, sharingMode);
                gpioPin = new SysGpioPin(sysPin);
            }
            else
            {
                if(_expanders.Count > 0)
                {
                    foreach(int startPin in _expanders.Keys)
                    {
                        if(pin >= startPin && pin < startPin + _expanders[startPin].NumberOfPins)
                        {
                            gpioPin = _expanders[startPin].OpenPin(pin - startPin);
                        }
                    }
                }
            }

            if(gpioPin == null) throw new NotSupportedException();
            return gpioPin;
        }
        public bool TryOpenPin(int pinNumber, GpioSharingMode sharingMode, out IGpioPin pin, out GpioOpenStatus openStatus)
        {
            pin = null;

            if ((pinNumber < 0) || (pinNumber > PinCount - 1))
            {
                openStatus = GpioOpenStatus.UnknownError;
                return(false);
            }

            if ((_gpioPin[pinNumber] == null) || (this._gpioPin[pinNumber].IsDisposed))
            {
                this._gpioPin[pinNumber] = new MCP23017GpioPin(this, pinNumber, sharingMode);
            }
            else if ((sharingMode == GpioSharingMode.Exclusive) || (this._gpioPin[pinNumber].SharingMode == GpioSharingMode.Exclusive))
            {
                openStatus = GpioOpenStatus.SharingViolation;
                return(false);
            }

            pin        = this._gpioPin[pinNumber];
            openStatus = GpioOpenStatus.PinOpened;

            return(true);
        }
        public IGpioPin OpenPin(int pinNumber, GpioSharingMode sharingMode)
        {
            if (TryOpenPin(pinNumber, sharingMode, out var gpioPin, out var openStatus))
            {
                return(gpioPin);
            }

            throw new Exception("");
        }
예제 #5
0
        public XGpio(int pinNumber, GpioController gpio, GpioSharingMode sharingMode, GpioPinDriveMode driveMode)
        {
            _pinNumber = pinNumber;
            _gpio = gpio;
            _sharingMode = sharingMode;
            _driveMode = driveMode;

            _init();
        }
예제 #6
0
파일: XGpio.cs 프로젝트: valoni/xIOT
        public XGpio(int pinNumber, GpioController gpio, GpioSharingMode sharingMode, GpioPinDriveMode driveMode)
        {
            _pinNumber   = pinNumber;
            _gpio        = gpio;
            _sharingMode = sharingMode;
            _driveMode   = driveMode;

            _init();
        }
예제 #7
0
        /// <summary>
        /// Opens the specified general-purpose I/O (GPIO) pin in the specified mode.
        /// </summary>
        /// <param name="pinNumber">The pin number of the GPIO pin that you want to open. Some pins may not be available
        ///     in user mode. For information about how the pin numbers correspond to physical pins, see the
        ///     documentation for your circuit board.</param>
        /// <param name="sharingMode">The mode in which you want to open the GPIO pin, which determines whether other
        ///     connections to the pin can be opened while you have the pin open.</param>
        /// <returns>The opened GPIO pin.</returns>
        public GpioPin OpenPin(int pinNumber, GpioSharingMode sharingMode)
        {
            GpioPin pin = new GpioPin();
            if (!pin.Init(pinNumber))
            {
                throw new InvalidOperationException();
            }

            return pin;
        }
예제 #8
0
        /// <summary>
        /// Opens the specified general-purpose I/O (GPIO) pin in the specified mode.
        /// </summary>
        /// <param name="pinNumber">The pin number of the GPIO pin that you want to open. Some pins may not be available
        ///     in user mode. For information about how the pin numbers correspond to physical pins, see the
        ///     documentation for your circuit board.</param>
        /// <param name="sharingMode">The mode in which you want to open the GPIO pin, which determines whether other
        ///     connections to the pin can be opened while you have the pin open.</param>
        /// <returns>The opened GPIO pin.</returns>
        public GpioPin OpenPin(int pinNumber, GpioSharingMode sharingMode)
        {
            GpioPin pin = new GpioPin();

            if (!pin.Init(pinNumber))
            {
                throw new InvalidOperationException();
            }

            return(pin);
        }
예제 #9
0
        /// <summary>
        /// Opens the specified general-purpose I/O (GPIO) pin in the specified mode.
        /// </summary>
        /// <param name="pinNumber">The pin number of the GPIO pin that you want to open. The pin number must be
        /// <list type="bullet">
        /// <item><term>in range</term></item>
        /// <item><term>available to usermode applications</term></item>
        /// </list>
        /// <para>Pin numbers start at 0, and increase to the maximum pin number, which is one less than the value returned by GpioController.PinCount.</para>
        /// <para>Which pins are available to usermode depends on the circuit board on which the code is running.For information about how pin numbers correspond to physical pins, see the documentation for your circuit board.</para>
        /// </param>
        /// <param name="sharingMode">The mode in which you want to open the GPIO pin, which determines whether other connections to the pin can be opened while you have the pin open.</param>
        /// <returns>The opened GPIO pin.</returns>
        /// <remarks>The following exceptions can be thrown by this method:
        /// <list type="bullet">
        /// <item><term>E_INVALIDARG (0x80070057)</term>
        /// <description>An invalid parameter was specified. This error will be returned if the pin number is out of range.
        /// Pin numbers start at 0 and increase to the maximum pin number, which is one less than the value returned by <see cref="PinCount"/>.</description></item>
        /// <item><term>HRESULT_FROM_WIN32(ERROR_NOT_FOUND) (0x80070490)</term>
        /// <description>The pin is not available to usermode applications; it is reserved by the system. See the documentation for your circuit board to find out which pins are available to usermode applications.</description></item>
        /// <item><term>HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION) (0x80070020)</term>
        /// <description>The pin is currently open in an incompatible sharing mode. For example:
        /// <list type="bullet">
        /// <item><term>The pin is already open in GpioSharingMode.Exclusive mode.</term></item>
        /// <item><term>The pin is already open in GpioSharingMode.SharedReadOnly mode when you request to open it in GpioSharingMode.Exclusive mode. </term></item>
        /// </list></description></item>
        /// <item><term>HRESULT_FROM_WIN32(ERROR_GPIO_INCOMPATIBLE_CONNECT_MODE) (0x80073bde)</term>
        /// <description>The pin is currently muxed to a different function; for example I2C, SPI, or UART. Ensure the pin is not in use by another function.</description></item>
        /// <item><term>HRESULT_FROM_WIN32(ERROR_GEN_FAILURE) (0x8007001f)</term>
        /// <description>The GPIO driver returned an error. Ensure that the GPIO driver is running and configured correctly.</description></item>
        /// </list>
        /// </remarks>
        public Gpio​Pin OpenPin(int pinNumber, GpioSharingMode sharingMode)
        {
            //Note : sharingMode ignored at the moment because we do not handle shared accessed pins

            var pin = new Gpio​Pin(pinNumber);

            if (pin.Init())
            {
                return(pin);
            }

            throw new System.InvalidOperationException();
        }
예제 #10
0
        /// <summary>
        /// Opens the specified general-purpose I/O (GPIO) pin in the specified mode, and gets a status value that can
        /// be used to handle a failure to open the pin programmatically.
        /// </summary>
        /// <param name="pinNumber">The pin number of the GPIO pin that you want to open. Some pins may not be available
        ///     in user mode. For information about how the pin numbers correspond to physical pins, see the
        ///     documentation for your circuit board.</param>
        /// <param name="sharingMode">The mode in which you want to open the GPIO pin, which determines whether other
        ///     connections to the pin can be opened while you have the pin open.</param>
        /// <param name="pin">The opened GPIO pin if the open status is GpioOpenStatus.Success; otherwise null.</param>
        /// <param name="openStatus">An enumeration value that indicates either that the attempt to open the GPIO pin
        ///     succeeded, or the reason that the attempt to open the GPIO pin failed.</param>
        /// <returns>True if the pin could be opened; otherwise false.</returns>
        public bool TryOpenPin(int pinNumber, GpioSharingMode sharingMode, out GpioPin pin, out GpioOpenStatus openStatus)
        {
            GpioPin newPin = new GpioPin();
            if (!newPin.Init(pinNumber))
            {
                pin = null;
                openStatus = GpioOpenStatus.PinUnavailable;
                return true;
            }

            pin = newPin;
            openStatus = GpioOpenStatus.PinOpened;
            return true;
        }
예제 #11
0
        /// <summary>
        /// Opens the specified general-purpose I/O (GPIO) pin in the specified mode, and gets a status value that can
        /// be used to handle a failure to open the pin programmatically.
        /// </summary>
        /// <param name="pinNumber">The pin number of the GPIO pin that you want to open. Some pins may not be available
        ///     in user mode. For information about how the pin numbers correspond to physical pins, see the
        ///     documentation for your circuit board.</param>
        /// <param name="sharingMode">The mode in which you want to open the GPIO pin, which determines whether other
        ///     connections to the pin can be opened while you have the pin open.</param>
        /// <param name="pin">The opened GPIO pin if the open status is GpioOpenStatus.Success; otherwise null.</param>
        /// <param name="openStatus">An enumeration value that indicates either that the attempt to open the GPIO pin
        ///     succeeded, or the reason that the attempt to open the GPIO pin failed.</param>
        /// <returns>True if the pin could be opened; otherwise false.</returns>
        public bool TryOpenPin(int pinNumber, GpioSharingMode sharingMode, out GpioPin pin, out GpioOpenStatus openStatus)
        {
            GpioPin newPin = new GpioPin();

            if (!newPin.Init(pinNumber))
            {
                pin        = null;
                openStatus = GpioOpenStatus.PinUnavailable;
                return(true);
            }

            pin        = newPin;
            openStatus = GpioOpenStatus.PinOpened;
            return(true);
        }
 public static GpioPin InitGpioPin(this GpioController controller,
     int pinNumber,
     GpioPinDriveMode driverMode,
     GpioSharingMode sharingMode)
 {
     GpioPin pin;
     GpioOpenStatus openStatus;
     if (!controller.TryOpenPin(pinNumber, sharingMode, out pin, out openStatus))
         throw new IOException($"Unable to open Pin Number {pinNumber}!");
     pin.Write(GpioPinValue.Low);
     pin.SetDriveMode(driverMode);
     if (openStatus != GpioOpenStatus.PinOpened)
         throw new IOException($"Pin Number {pinNumber} was opened but has an open status of [{openStatus}]!");
     return pin;
 }
        public GpioPin OpenPin(int pinNumber, GpioSharingMode sharingMode = GpioSharingMode.Exclusive)
        {
            GpioPin        pin = null;
            GpioOpenStatus status;

            if (m_controller.TryOpenPin(pinNumber, sharingMode, out pin, out status))
            {
                if (Pins == null)
                {
                    Pins = new List <Pin>();
                }

                Pins.Add(new Pin(pinNumber, pin, status));
            }

            return(pin);
        }
 public GpioPin OpenPin(int pin, GpioSharingMode mode)
 {
     if (_pins.ContainsKey(pin))
     {
         var gpioPin = _pins[pin];
         if (gpioPin.SharingMode == mode)
         {
             return gpioPin;
         }
         throw new ArgumentException($"Pin '{pin}' is already configured in mode '{gpioPin.SharingMode}'");
     }
     else
     {
         var gpioPin = _gpioController?.OpenPin(pin, mode);
         _pins[pin] = gpioPin;
         return gpioPin;
     }
 }
예제 #15
0
        public Uln2003Driver(GpioController gpioController,
                             int wireIn1, int wireIn2, int wireIn3, int wireIn4,
                             GpioSharingMode sharingMode = GpioSharingMode.Exclusive, int intervalMs = 5)
        {
            var gpio = gpioController ?? GpioController.GetDefault();

            _gpioPins[0] = gpio.OpenPin(wireIn1, sharingMode);
            _gpioPins[1] = gpio.OpenPin(wireIn2, sharingMode);
            _gpioPins[2] = gpio.OpenPin(wireIn3, sharingMode);
            _gpioPins[3] = gpio.OpenPin(wireIn4, sharingMode);

            foreach (var gpioPin in _gpioPins)
            {
                gpioPin.Write(GpioPinValue.Low);
                gpioPin.SetDriveMode(GpioPinDriveMode.Output);
            }

            IntervalMs = intervalMs;
        }
예제 #16
0
        public static GpioPin InitGpioPin(this GpioController controller,
                                          int pinNumber,
                                          GpioPinDriveMode driverMode = GpioPinDriveMode.Input,
                                          GpioSharingMode sharingMode = GpioSharingMode.Exclusive)
        {
            GpioPin        pin;
            GpioOpenStatus openStatus;

            if (!controller.TryOpenPin(pinNumber, sharingMode, out pin, out openStatus))
            {
                throw new IOException($"Unable to open Pin Number {pinNumber}!");
            }
            pin.Write(GpioPinValue.Low);
            pin.SetDriveMode(driverMode);
            if (openStatus != GpioOpenStatus.PinOpened)
            {
                throw new IOException($"Pin Number {pinNumber} was opened but has an open status of [{openStatus}]!");
            }
            return(pin);
        }
        /// <summary>
        /// Connects to a GPIO pin if it exists.
        /// </summary>
        /// <param name="controllerIndex">Controller index.</param>
        /// <param name="pinNumber">Pin number.</param>
        /// <param name="driveMode">Drive mode.</param>
        /// <param name="sharingMode">Sharing mode.</param>
        /// <returns>Pin when controller and device exist, otherwise null.</returns>
        public static GpioPin ConnectGpio(int controllerIndex, int pinNumber, 
            GpioPinDriveMode driveMode = GpioPinDriveMode.Input, GpioSharingMode sharingMode = GpioSharingMode.Exclusive)
        {
            // Validate
            if (controllerIndex < 0) throw new ArgumentOutOfRangeException(nameof(controllerIndex));

            // Initialize
            Initialize();

            // Get controller (return null when doesn't exist)
            if (Gpio.Count < controllerIndex + 1)
                return null;
            var controller = Gpio[controllerIndex];

            // Connect to device (return null when doesn't exist)
            var pin = controller.OpenPin(pinNumber, sharingMode);
            if (pin == null)
                return null;

            // Configure and return pin
            if (pin.GetDriveMode() != driveMode)
                pin.SetDriveMode(driveMode);
            return pin;
        }
예제 #18
0
 internal MCP23017GpioPin(MCP23017GpioController gpioController, int pinNumber, GpioSharingMode sharingMode)
 {
     _gpioController = gpioController;
     PinNumber       = pinNumber;
     SharingMode     = sharingMode;
 }
예제 #19
0
 /// <summary>
 /// Opens the specified general-purpose I/O (GPIO) pin in the specified mode, and gets a status value that you can use to handle a failure to open the pin programmatically.
 /// </summary>
 /// <param name="pinNumber">The pin number of the GPIO pin that you want to open. Some pins may not be available in user mode. For information about how the pin numbers correspond to physical pins, see the documentation for your circuit board.</param>
 /// <param name="sharingMode">The mode in which you want to open the GPIO pin, which determines whether other connections to the pin can be opened while you have the pin open.</param>
 /// <param name="pin">The opened GPIO pin if the return value is true; otherwise null.</param>
 /// <param name="openStatus">An enumeration value that indicates either that the attempt to open the GPIO pin succeeded, or the reason that the attempt to open the GPIO pin failed.</param>
 /// <returns>True if the method successfully opened the pin; otherwise false.
 /// <para>If the method returns true, the pin parameter receives an instance of a GpioPin, and the openStatus parameter receives GpioOpenStatus.PinOpened.If the method returns false, the pin parameter is null and the openStatus parameter receives the reason that the operation failed.</para></returns>
 public bool TryOpenPin(int pinNumber, GpioSharingMode sharingMode, out Gpio​Pin pin, out GpioOpenStatus openStatus)
 public IGpioPin OpenPin(int pinNumber, GpioSharingMode sharingMode)
 {
     return(new GpioPinWrapper(Controller.OpenPin(pinNumber, sharingMode)));
 }
예제 #21
0
 public GpioPin OpenPin(Int32 pinNumber, GpioSharingMode sharingMode)
 {
     return(new GpioPin());
 }
 public bool TryOpenPin(int pinNumber, GpioSharingMode sharingMode, out GpioPin pin, out GpioOpenStatus openStatus)
 {
     return(Controller.TryOpenPin(pinNumber, sharingMode, out pin, out openStatus));
 }