/// <summary>
        /// Adds the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        public void Add(PinConfiguration pin)
        {
            lock (pinConfigurations)
            {
                if (pinConfigurations.ContainsKey(pin.Pin))
                {
                    throw new InvalidOperationException("This pin is already present on the connection");
                }
                if (!string.IsNullOrEmpty(pin.Name) && namedPins.ContainsKey(pin.Name))
                {
                    throw new InvalidOperationException("A pin with the same name is already present on the connection");
                }

                pinConfigurations.Add(pin.Pin, pin);

                if (!string.IsNullOrEmpty(pin.Name))
                {
                    namedPins.Add(pin.Name, pin);
                }

                lock (timer)
                {
                    if (IsOpened)
                    {
                        Allocate(pin);
                    }
                }
            }
        }
        private void Allocate(PinConfiguration configuration)
        {
            if (configuration.StatusChangedAction != null)
            {
                var handler = new EventHandler <PinStatusEventArgs>((sender, args) =>
                {
                    if (args.Configuration == configuration)
                    {
                        configuration.StatusChangedAction(args.Enabled);
                    }
                });
                pinEvents[configuration.Pin] = handler;
                PinStatusChanged            += handler;
            }

            Driver.Allocate(configuration.Pin, configuration.Direction);
            if (configuration is OutputPinConfiguration outputConfiguration)
            {
                this[configuration.Pin] = outputConfiguration.Enabled;
            }
            else
            {
                var inputConfiguration = (InputPinConfiguration)configuration;
                var pinValue           = Driver.Read(inputConfiguration.Pin);

                var pin = (ProcessorPins)((uint)1 << (int)inputConfiguration.Pin);
                inputPins   |= pin;
                pinRawValues = Driver.Read(inputPins);

                if (inputConfiguration.Resistor != PinResistor.None && (Driver.GetCapabilities() & GpioConnectionDriverCapabilities.CanSetPinResistor) > 0)
                {
                    Driver.SetPinResistor(inputConfiguration.Pin, inputConfiguration.Resistor);
                }

                if (inputConfiguration is SwitchInputPinConfiguration switchConfiguration)
                {
                    pinValues[inputConfiguration.Pin] = switchConfiguration.Enabled;
                    OnPinStatusChanged(new PinStatusEventArgs {
                        Configuration = inputConfiguration, Enabled = pinValues[inputConfiguration.Pin]
                    });
                }
                else
                {
                    pinValues[inputConfiguration.Pin] = inputConfiguration.GetEffective(pinValue);
                    OnPinStatusChanged(new PinStatusEventArgs {
                        Configuration = inputConfiguration, Enabled = pinValues[inputConfiguration.Pin]
                    });
                }
            }
        }
        private void Release(PinConfiguration configuration)
        {
            if (configuration.Direction == PinDirection.Output)
            {
                Driver.Write(configuration.Pin, false);
                OnPinStatusChanged(new PinStatusEventArgs {
                    Enabled = false, Configuration = configuration
                });
            }

            Driver.Release(configuration.Pin);

            if (pinEvents.TryGetValue(configuration.Pin, out var handler))
            {
                PinStatusChanged -= handler;
                pinEvents.Remove(configuration.Pin);
            }
        }
        /// <summary>
        /// Gets or sets the status of the specified pin.
        /// </summary>
        public bool this[PinConfiguration pin]
        {
            get { return(pinValues[pin.Pin]); }
            set
            {
                if (pin.Direction == PinDirection.Output)
                {
                    var pinValue = pin.GetEffective(value);
                    Driver.Write(pin.Pin, pinValue);

                    pinValues[pin.Pin] = value;
                    OnPinStatusChanged(new PinStatusEventArgs {
                        Enabled = value, Configuration = pin
                    });
                }
                else
                {
                    throw new InvalidOperationException("Value of input pins cannot be modified");
                }
            }
        }
        /// <summary>
        /// Removes the specified pin.
        /// </summary>
        /// <param name="configuration">The pin configuration.</param>
        public void Remove(PinConfiguration configuration)
        {
            lock (pinConfigurations)
            {
                lock (timer)
                {
                    if (IsOpened)
                    {
                        Release(configuration);
                    }
                }

                pinConfigurations.Remove(configuration.Pin);
                if (!string.IsNullOrEmpty(configuration.Name))
                {
                    namedPins.Remove(configuration.Name);
                }
                pinValues.Remove(configuration.Pin);

                var pin = (ProcessorPins)((uint)1 << (int)configuration.Pin);
                inputPins    &= ~pin;
                pinRawValues &= ~pin;
            }
        }
 /// <summary>
 /// Blinks the specified pin.
 /// </summary>
 /// <param name="configuration">The pin configuration.</param>
 /// <param name="duration">The duration.</param>
 public void Blink(PinConfiguration configuration, TimeSpan duration = new TimeSpan())
 {
     Toggle(configuration);
     Sleep(duration);
     Toggle(configuration);
 }
 /// <summary>
 /// Toggles the specified pin.
 /// </summary>
 /// <param name="configuration">The pin configuration.</param>
 public void Toggle(PinConfiguration configuration)
 {
     this[configuration] = !this[configuration];
 }
 /// <summary>
 /// Determines whether the connection contains the specified pin.
 /// </summary>
 /// <param name="configuration">The pin configuration.</param>
 /// <returns>
 ///   <c>true</c> if the connection contains the specified pin; otherwise, <c>false</c>.
 /// </returns>
 public bool Contains(PinConfiguration configuration)
 {
     return(pinConfigurations.ContainsKey(configuration.Pin));
 }
Пример #9
0
 /// <summary>
 /// Gets the status of the specified pin.
 /// </summary>
 public ConnectedPin this[PinConfiguration pin]
 {
     get { return(new ConnectedPin(connection, pin)); }
 }
 internal ConnectedPin(GpioConnection connection, PinConfiguration pinConfiguration)
 {
     this.connection = connection;
     Configuration   = pinConfiguration;
 }