/// <summary>
 /// Enumerates the specified pins.
 /// </summary>
 /// <param name="pins">The pins.</param>
 /// <returns>The pins.</returns>
 public static IEnumerable <ProcessorPin> Enumerate(this ProcessorPins pins)
 {
     return(((Enum.GetValues(typeof(ProcessorPin)) as ProcessorPin[]) ?? new ProcessorPin[0])
            .Distinct()
            .Where(p => (pins & (ProcessorPins)((uint)1 << (int)p)) != ProcessorPins.None)
            .ToArray());
 }
        /// <summary>
        /// Reads the status of the specified pins.
        /// </summary>
        /// <param name="pins">The pins.</param>
        /// <returns>
        /// The pins status.
        /// </returns>
        public ProcessorPins Read(ProcessorPins pins)
        {
            var pinGroupAddress = gpioAddress + (int)(Interop.BCM2835_GPLEV0 + (uint)0 * 4);
            var value           = SafeReadUInt32(pinGroupAddress);

            return((ProcessorPins)((uint)pins & value));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads the status of the specified pins.
        /// </summary>
        /// <param name="pins">The pins.</param>
        /// <returns>
        /// The pins status.
        /// </returns>
        public ProcessorPins Read(ProcessorPins pins)
        {
            var pinGroupAddress = gpioAddress + (int)(Interop.BCM2835_GPLEV0 + (uint)0 * 4);
            var value           = SafeReadUInt32(pinGroupAddress);

            return(ProcessorPinExtensionMethods.And(pins, value));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GpioConnection"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="pins">The pins.</param>
        public GpioConnection(GpioConnectionSettings settings, IEnumerable <PinConfiguration> pins)
        {
            this.settings = settings ?? new GpioConnectionSettings();
            Pins          = new ConnectedPins(this);


            inputPins    = new ProcessorPins(settings.PinCount);
            pinRawValues = new ProcessorPins(settings.PinCount);

            var pinList = pins.ToList();

            pinConfigurations = pinList.ToDictionary(p => p.Pin);

            namedPins = pinList.Where(p => !string.IsNullOrEmpty(p.Name)).ToDictionary(p => p.Name);

            timer = Timer.Create();

            timer.Interval = this.settings.PollInterval;
            timer.Action   = CheckInputPins;

            if (this.settings.Opened)
            {
                Open();
            }
        }
 /// <summary>
 /// Reads the status of the specified pins.
 /// </summary>
 /// <param name="pins">The pins.</param>
 /// <returns>
 /// The pins status.
 /// </returns>
 public ProcessorPins Read(ProcessorPins pins)
 {
     return(pins.Enumerate()
            .Select(p => Read(p) ? (ProcessorPins)((uint)1 << (int)p) : ProcessorPins.None)
            .Aggregate(
                ProcessorPins.None,
                (a, p) => a | p));
 }
        /// <summary>
        /// Reads the status of the specified pins.
        /// </summary>
        /// <param name="pins">The pins.</param>
        /// <returns>
        /// The pins status.
        /// </returns>
        public ProcessorPins Read(ProcessorPins pins)
        {
            var result = new ProcessorPins(pins.Count);

            foreach (var p in pins.Enumerate())
            {
                result.Set(p, Read((ProcessorPin)p));
            }
            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// </summary>
        public ProcessorPins Read(ProcessorPins pins)
        {
            // TODO: implement on $KE,RD,ALL
            var result = new ProcessorPins(pins.Count);

            foreach (var p in pins.Enumerate())
            {
                result.Set(p, Read((ProcessorPin)p));
            }
            return(result);
        }
        /// <summary>
        /// Logical AND opertion on pins with int
        /// </summary>
        public static ProcessorPins And(ProcessorPins pins, uint value)
        {
            var result = new ProcessorPins(32);

            foreach (var p in pins.Enumerate())
            {
                var val = ((uint)(1 << (int)p) & value);
                result.Set(p, val > 0 ? true : false);
            }
            return(result);
        }
Ejemplo n.º 9
0
        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);
            var outputConfiguration = configuration as OutputPinConfiguration;

            if (outputConfiguration != null)
            {
                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    = inputPins | pin;
                pinRawValues = Driver.Read(inputPins);

                if (inputConfiguration.Resistor != PinResistor.None)
                {
                    Driver.SetPinResistor(inputConfiguration.Pin, inputConfiguration.Resistor);
                }

                var switchConfiguration = inputConfiguration as SwitchInputPinConfiguration;
                if (switchConfiguration != null)
                {
                    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]
                    });
                }
            }
        }
Ejemplo n.º 10
0
        private void CheckInputPins()
        {
            var newPinValues = Driver.Read(inputPins);

            var changes = newPinValues ^ pinRawValues;

            if (changes == ProcessorPins.None)
            {
                return;
            }

            var notifiedConfigurations = new List <PinConfiguration>();

            foreach (var np in changes.Enumerate())
            {
                var processorPin = (ProcessorPins)((uint)1 << (int)np);
                var oldPinValue  = (pinRawValues & processorPin) != ProcessorPins.None;
                var newPinValue  = (newPinValues & processorPin) != ProcessorPins.None;

                if (oldPinValue != newPinValue)
                {
                    var pin       = (InputPinConfiguration)pinConfigurations[np];
                    var switchPin = pin as SwitchInputPinConfiguration;

                    if (switchPin != null)
                    {
                        if (pin.GetEffective(newPinValue))
                        {
                            pinValues[np] = !pinValues[np];
                            notifiedConfigurations.Add(pin);
                        }
                    }
                    else
                    {
                        pinValues[np] = pin.GetEffective(newPinValue);
                        notifiedConfigurations.Add(pin);
                    }
                }
            }

            pinRawValues = newPinValues;

            // Only fires events once all states have been modified.
            foreach (var pin in notifiedConfigurations)
            {
                OnPinStatusChanged(new PinStatusEventArgs {
                    Configuration = pin, Enabled = pinValues[pin.Pin]
                });
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Clears pin attached to this connection.
        /// </summary>
        public void Clear()
        {
            lock (pinConfigurations)
            {
                foreach (var pinConfiguration in pinConfigurations.Values)
                {
                    Release(pinConfiguration);
                }

                pinConfigurations.Clear();
                namedPins.Clear();
                pinValues.Clear();

                pinRawValues = ProcessorPins.None;
                inputPins    = ProcessorPins.None;
            }
        }
Ejemplo n.º 12
0
        /// <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    = inputPins & ~pin;
                pinRawValues = pinRawValues & ~pin;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Reads the status of the specified pins.
        /// </summary>
        /// <param name="pins">The pins.</param>
        /// <returns>
        /// The pins status.
        /// </returns>
        public ProcessorPins Read(ProcessorPins pins)
        {
            var value = Interop.bcm2835_gpioperi_read(0);

            return((ProcessorPins)((uint)pins & value));
        }
 /// <summary>
 /// Enumerates the specified pins.
 /// </summary>
 /// <param name="pins">The pins.</param>
 /// <returns>The pins.</returns>
 public static IEnumerable <int> Enumerate(this ProcessorPins pins)
 {
     return(Enumerable.Range(0, pins.Count)
            .Where(p => (pins.Get(p)))
            .ToArray());
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Diff
 /// </summary>
 public ProcessorPins Diff(ProcessorPins pinRawValues)
 {
     return(new ProcessorPins(_pins.Xor(pinRawValues._pins)));
 }