Exemple #1
0
        /// <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);
                    }
                }
            }
        }
Exemple #2
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]
                    });
                }
            }
        }
Exemple #3
0
        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);

            EventHandler <PinStatusEventArgs> handler;

            if (pinEvents.TryGetValue(configuration.Pin, out handler))
            {
                PinStatusChanged -= handler;
                pinEvents.Remove(configuration.Pin);
            }
        }
Exemple #4
0
        /// <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");
                }
            }
        }
Exemple #5
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;
            }
        }
        /// <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 = (int)configuration.Pin;
                inputPins.Set(pin, false);
                pinRawValues.Set(pin, false);
            }
        }
        /// <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);
                }
            }
        }
        /// <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");

            }
        }
 internal ConnectedPin(GpioConnection connection, PinConfiguration pinConfiguration)
 {
     this.connection = connection;
     Configuration = pinConfiguration;
 }
Exemple #10
0
        static void Main(string[] args)
        {
            try
            {
                var driver = args.GetDriver();
                var mainboard = Board.Current;

                if (!mainboard.IsRaspberryPi)
                {
                    Console.WriteLine("'{0}' is not a valid processor for a Raspberry Pi.", mainboard.Processor);
                    return;
                }

                // Declare outputs (leds)
                var leds = new PinConfiguration[]
                               {
                                   ConnectorPin.P1Pin26.Output().Name("Led1").Enable(),
                                   ConnectorPin.P1Pin24.Output().Name("Led2"),
                                   ConnectorPin.P1Pin22.Output().Name("Led3").Enable(),
                                   ConnectorPin.P1Pin15.Output().Name("Led4"),
                                   ConnectorPin.P1Pin13.Output().Name("Led5").Enable(),
                                   ConnectorPin.P1Pin11.Output().Name("Led6")
                               };

                // Assign a behavior to the leds
                var behavior = new ChaserBehavior(leds)
                                   {
                                       Loop = args.GetLoop(),
                                       RoundTrip = args.GetRoundTrip(),
                                       Width = args.GetWidth(),
                                       Interval = args.GetSpeed()
                                   };

                // Alternate behaviors...
                /*
                var random = new Random();
                var behavior = new PatternBehavior(leds, Enumerable.Range(0, 5).Select(i => random.Next(511)))
                                   {
                                       Loop = Helpers.GetLoop(args),
                                       RoundTrip = Helpers.GetRoundTrip(args),
                                       Interval = Helpers.GetSpeed(args)
                                   };*/

                /*
                var behavior = new BlinkBehavior(leds)
                                   {
                                       Count = args.GetWidth(),
                                       Interval = args.GetSpeed()
                                   };*/

                // Declare input (switchButton) interacting with the leds behavior
                var switchButton = ConnectorPin.P1Pin03.Input()
                    .Name("Switch")
                    .Revert()
                    .Switch()
                    .Enable()
                    .OnStatusChanged(b =>
                                         {
                                             behavior.RoundTrip = !behavior.RoundTrip;
                                             Console.WriteLine("Button switched {0}", b ? "on" : "off");
                                         });

                // Create connection
                Console.WriteLine("Running on Raspberry firmware rev{0}, board rev{1}, processor {2}", mainboard.Firmware, mainboard.Revision, mainboard.Processor);

                var settings = new GpioConnectionSettings {Driver = driver};

                using (var connection = new GpioConnection(settings, leds))
                {
                    Console.WriteLine("Using {0}, frequency {1:0.##}hz", settings.Driver.GetType().Name, 1000.0/args.GetSpeed());

                    Thread.Sleep(1000);

                    connection.Add(switchButton);
                    connection.Start(behavior); // Starting the behavior automatically registers the pins to the connection, if needed.

                    Console.ReadKey(true);

                    connection.Stop(behavior);
                }
            }
            catch(Exception ex)
            {
                var currentException = ex;
                while (currentException != null)
                {
                    Console.WriteLine("{0}: {1}", currentException.GetType().Name, currentException.Message);
                    currentException = currentException.InnerException;
                }
            }
        }
Exemple #11
0
 /// <summary>
 /// Blinks the specified pin.
 /// </summary>
 /// <param name="configuration">The pin configuration.</param>
 /// <param name="duration">The duration, in millisecond.</param>
 public void Blink(PinConfiguration configuration, decimal duration = -1)
 {
     Toggle(configuration);
     Sleep(duration);
     Toggle(configuration);
 }
 /// <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);
 }
        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] });
                }
            }
        }
 /// <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);
 }
Exemple #16
0
 /// <summary>
 /// Gets the status of the specified pin.
 /// </summary>
 public ConnectedPin this[PinConfiguration pin]
 {
     get { return(new ConnectedPin(connection, 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);
 }
        static void Main(string[] args)
        {
            const ConnectorPin led1Pin = ConnectorPin.P1Pin26;
            const ConnectorPin led2Pin = ConnectorPin.P1Pin24;
            const ConnectorPin led3Pin = ConnectorPin.P1Pin22;
            const ConnectorPin led4Pin = ConnectorPin.P1Pin15;
            const ConnectorPin led5Pin = ConnectorPin.P1Pin13;
            const ConnectorPin led6Pin = ConnectorPin.P1Pin11;
            const ConnectorPin buttonPin = ConnectorPin.P1Pin03;

            Console.WriteLine("Chaser Sample: Sample a LED chaser with a switch to change behavior");
            Console.WriteLine();
            Console.WriteLine("\tLed 1: {0}", led1Pin);
            Console.WriteLine("\tLed 2: {0}", led2Pin);
            Console.WriteLine("\tLed 3: {0}", led3Pin);
            Console.WriteLine("\tLed 4: {0}", led4Pin);
            Console.WriteLine("\tLed 5: {0}", led5Pin);
            Console.WriteLine("\tLed 6: {0}", led6Pin);
            Console.WriteLine("\tSwitch: {0}", buttonPin);
            Console.WriteLine();

            var driver = args.GetDriver();

            // Declare outputs (leds)
            var leds = new PinConfiguration[]
                           {
                               led1Pin.Output().Name("Led1").Enable(),
                               led2Pin.Output().Name("Led2"),
                               led3Pin.Output().Name("Led3").Enable(),
                               led4Pin.Output().Name("Led4"),
                               led5Pin.Output().Name("Led5").Enable(),
                               led6Pin.Output().Name("Led6")
                           };

            // Assign a behavior to the leds
            var behavior = new ChaserBehavior(leds)
                               {
                                   Loop = args.GetLoop(),
                                   RoundTrip = args.GetRoundTrip(),
                                   Width = args.GetWidth(),
                                   Interval = TimeSpan.FromMilliseconds(args.GetSpeed())
                               };

            // Alternate behaviors...
            /*
            var random = new Random();
            var behavior = new PatternBehavior(leds, Enumerable.Range(0, 5).Select(i => random.Next(511)))
                               {
                                   Loop = Helpers.GetLoop(args),
                                   RoundTrip = Helpers.GetRoundTrip(args),
                                   Interval = Helpers.GetSpeed(args)
                               };*/

            /*
            var behavior = new BlinkBehavior(leds)
                               {
                                   Count = args.GetWidth(),
                                   Interval = args.GetSpeed()
                               };*/

            // Declare input (switchButton) interacting with the leds behavior
            var switchButton = buttonPin.Input()
                .Name("Switch")
                .Revert()
                .Switch()
                .Enable()
                .OnStatusChanged(b =>
                                     {
                                         behavior.RoundTrip = !behavior.RoundTrip;
                                         Console.WriteLine("Button switched {0}", b ? "on" : "off");
                                     });

            // Create connection
            var settings = new GpioConnectionSettings {Driver = driver};

            using (var connection = new GpioConnection(settings, leds))
            {
                Console.WriteLine("Using {0}, frequency {1:0.##}hz", settings.Driver.GetType().Name, 1000.0/args.GetSpeed());

                Thread.Sleep(1000);

                connection.Add(switchButton);
                connection.Start(behavior); // Starting the behavior automatically registers the pins to the connection, if needed.

                Console.ReadKey(true);

                connection.Stop(behavior);
            }
        }
        /// <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;
            }
        }
Exemple #20
0
 /// <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));
 }
 /// <summary>
 /// Blinks the specified pin.
 /// </summary>
 /// <param name="configuration">The pin configuration.</param>
 /// <param name="duration">The duration, in millisecond.</param>
 public void Blink(PinConfiguration configuration, decimal duration = -1)
 {
     Toggle(configuration);
     Sleep(duration);
     Toggle(configuration);
 }
Exemple #22
0
 /// <summary>
 /// Toggles the specified pin.
 /// </summary>
 /// <param name="configuration">The pin configuration.</param>
 public void Toggle(PinConfiguration configuration)
 {
     this[configuration] = !this[configuration];
 }
        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);

            EventHandler<PinStatusEventArgs> handler;
            if (pinEvents.TryGetValue(configuration.Pin, out handler))
            {
                PinStatusChanged -= handler;
                pinEvents.Remove(configuration.Pin);
            }
        }
Exemple #24
0
 internal ConnectedPin(GpioConnection connection, PinConfiguration pinConfiguration)
 {
     this.connection = connection;
     Configuration   = pinConfiguration;
 }