Exemple #1
0
        public async Task <bool> SetValue(GPIOPinValue value)
        {
            if (!Disposed)
            {
                await Task.Yield();

                switch (_OpenMode)
                {
                case GPIOPinSharingMode.Exclusive:
                    switch (Mode)
                    {
                    case GPIOPinMode.Output:
                        try
                        {
                            File.WriteAllText(PinPath + GPIO_PIN_VALUE, ((byte)value).ToString());
                            return(true);
                        }
                        catch (Exception ex)
                        {
                            if (ex is FileNotFoundException)
                            {
                                Close(true);
                            }
                            throw ex;
                        }

                    default:
                        throw new NotSupportedException("Указанный режим работы контакта GPIO не поддерживается.");
                    }

                default:
                case GPIOPinSharingMode.ReadOnlySharedAccess:
                    throw new NotSupportedException("Контакт GPIO открыт в режиме \"Только считывание\".");
                }
            }
            else
            {
                throw new ObjectDisposedException(ToString());
            }
        }
Exemple #2
0
        private void ReaderLoop()
        {
            while (!Disposed)
            {
                try
                {
                    string       value  = File.ReadAllText(PinPath + GPIO_PIN_VALUE).Replace("\n", "").Replace("\r", "").Replace(" ", "");
                    string       invert = File.ReadAllText(PinPath + GPIO_PIN_ACTIVE_LOW).Replace("\n", "").Replace("\r", "").Replace(" ", "");
                    GPIOPinValue _value;
                    switch (value)
                    {
                    case "0":
                        _value = invert == "0" ? GPIOPinValue.Low : GPIOPinValue.High;
                        if (Value != _value)
                        {
                            Value = _value;
                            _ValueChanged?.Invoke(this, GPIOPinEdge.FallingEdge);
                        }
                        break;

                    case "1":
                        _value = invert == "0" ? GPIOPinValue.High : GPIOPinValue.Low;
                        if (Value != _value)
                        {
                            Value = _value;
                            _ValueChanged?.Invoke(this, GPIOPinEdge.RisingEdge);
                        }
                        break;
                    }
                }
                catch (Exception ex)
                {
                    if (ex is FileNotFoundException)
                    {
                        Close(true);
                    }
                }
            }
        }