Exemplo n.º 1
0
        internal GPIOPin(uint number, GPIOPinSharingMode sharingMode, GPIOPinSharingMode openMode)
        {
            Disposed         = false;
            PinNumber        = number;
            _SharingMode     = sharingMode;
            _OpenMode        = openMode;
            Mode             = GPIOPinMode.Input;
            PinPath          = GPIOController.GPIO_PATH + "/gpio" + PinNumber;
            ConnectionStatus = ConnectionStatus.Connected;
            switch (_OpenMode)
            {
            case GPIOPinSharingMode.Exclusive:
                try
                {
                    File.WriteAllText(PinPath + GPIO_PIN_DIRECTION, GPIO_PIN_DIRECTION_IN);
                    File.WriteAllText(PinPath + GPIO_PIN_VALUE, "0");
                    File.WriteAllText(PinPath + GPIO_PIN_ACTIVE_LOW, "0");
                    File.WriteAllText(PinPath + GPIO_PIN_EDGE, "both");
                }
                catch
                {
                    Close(false);
                }
                break;

            default:
            case GPIOPinSharingMode.ReadOnlySharedAccess:

                break;
            }
        }
Exemplo n.º 2
0
        public IGPIOPin OpenPin(uint number, GPIOPinSharingMode shareMode)
        {
            bool success = InternalDevice.TryOpenPin((int)number, GpioSharingMode.Exclusive, out GpioPin pin, out GpioOpenStatus status);

            if (success)
            {
                return(new GPIOPin(pin, this));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Пытается открыть контакт GPIO в заданном режиме.
        /// </summary>
        /// <param name="pin">Номер контакта GPIO.</param>
        /// <param name="shareMode">Режим открытия контакта GPIO.</param>
        /// <returns></returns>
        public async Task <IGPIOPin> OpenPin(uint pin, GPIOPinSharingMode shareMode)
        {
            await Task.Yield();

            string pinPath = GPIO_PATH + "/gpio" + pin;

            switch (shareMode)
            {
            default:
            case GPIOPinSharingMode.Exclusive:
                if (Directory.Exists(pinPath))
                {
                    throw new Exception();
                }
                else
                {
                    File.WriteAllText(GPIO_EXPORT_PATH, pin.ToString());
                    if (Directory.Exists(pinPath))
                    {
                        return(new GPIOPin(pin, GPIOPinSharingMode.Exclusive, GPIOPinSharingMode.Exclusive));
                    }
                    else
                    {
                        throw new Exception();
                    }
                }

            case GPIOPinSharingMode.ReadOnlySharedAccess:
                if (Directory.Exists(pinPath))
                {
                    bool accessible = true;
                    try
                    {
                        File.ReadAllText(pinPath + "/value");
                    }
                    catch
                    {
                        accessible = false;
                    }
                    if (accessible)
                    {
                        return(new GPIOPin(pin, GPIOPinSharingMode.ReadOnlySharedAccess, GPIOPinSharingMode.ReadOnlySharedAccess));
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    File.WriteAllText(GPIO_EXPORT_PATH, pin.ToString());
                    if (Directory.Exists(pinPath))
                    {
                        return(new GPIOPin(pin, GPIOPinSharingMode.ReadOnlySharedAccess, GPIOPinSharingMode.Exclusive));
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
        }