Skip to content

Sample code + GenericBehaviour and a better callback interface - same needed for InputPinConfiguration #97

@hidegh

Description

@hidegh

When I started to use the project the Chaser demo was the 1st I've based my code. Then I've experimented with low-level access. Now what I've missed with the high-level objects (especially when configuring pins) was the possibility to do a bit more in the callback function, what needed is just to access the connection the given pin is later registered...

I was not able to define an input pin which on callback would turn on/off a different led (without messing up my code and over-using global variables). What I miss is: a connection property on that event.

Also a generic behavior was missing. So I have created one, where the special callback can be added via an action - also the callback is having a bit more inputs... It was just a test, assume behavior should not be used this way, yet the initialization and execution action having access to the connections enables to do more without extra global variables.

What I miss is good documentation on those high-level classes.

Similar callback for the InputPinConfiguration object would be super. So here's the code:

using System;
using Raspberry.IO.GeneralPurpose;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Raspberry.IO.GeneralPurpose.Behaviors;

namespace LibR3IO
{
    /// <summary>
    /// Based on: https://github.com/raspberry-sharp/raspberry-sharp-io/blob/638f67fc8466d53d60168f2e896e51d77ed6823b/Tests/Test.Gpio.Chaser/Program.cs
    /// </summary>
    public class HighLevelCode
    {
        public void Run()
        {
            Console.WriteLine("Starting GPIO program (HI)...");
            Console.WriteLine();
            Console.WriteLine("Settings up for:");
            Console.WriteLine("- mode   l: " + Raspberry.Board.Current.Model.ToString());
            Console.WriteLine("- connector pin out: " + GpioConnectionSettings.ConnectorPinout.ToString());

            var buttonHandlerVersion = 3;

            var ledGreenPP = (ProcessorPin)16;
            var ledRedPP = (ProcessorPin)20;
            var buttonRedPP = (ProcessorPin)27;

            var ledGreenPPConfig = new OutputPinConfiguration(ledGreenPP).Disable();
            var ledRedPPConfig = new OutputPinConfiguration(ledRedPP).Disable();
            var buttonRedPPConfig = new InputPinConfiguration(buttonRedPP)
                .Name("Switch")
                //.Revert()
                //.Switch()
                //.Enable()
                .OnStatusChanged(b =>
                {
                    // NOTE: don't have access to connection here, so can't write real event-code, need to use behavior...
                    Console.WriteLine("Button switched {0}", b ? "on" : "off");
                });

            // NOTE: use behaviour for blinking and timing instead of threads?
            var buttonBehavior = new GenericBehavior(
                new[] { ledRedPPConfig },
                (@this) => {
                    Console.WriteLine("Behaviour action initializing");

                    if (buttonHandlerVersion == 1)
                    {
                        @this.Connection.PinStatusChanged += (object sender, PinStatusEventArgs e) =>
                        {
                            if (e.Configuration.Pin == buttonRedPP && e.Enabled)
                            {
                                Console.WriteLine($"Behavior BUTTON {e.Configuration.Pin} change to: {e.Enabled}");
                                @this.Connection.Pins[ledRedPP].Toggle();
                            }
                        };
                    }
                    else if (buttonHandlerVersion == 2)
                    {
                        var ledButtonValue = false;

                        @this.Connection.Pins[buttonRedPP].StatusChanged += (object sender, PinStatusEventArgs e) =>
                        {
                            Console.WriteLine($"Behavior BUTTON {e.Configuration.Pin} change to: {e.Enabled}");
                            if (e.Enabled)
                            {
                                ledButtonValue = !ledButtonValue;
                                @this.Connection.Pins[ledRedPP].Toggle();
                            }
                        };
                    }
                },
                (@this, step) => { Console.WriteLine("Behaviour action executing"); },
                int.MaxValue
                );

            //
            // Main
            var driver = GpioConnectionSettings.DefaultDriver;
            var settings = new GpioConnectionSettings() { Driver = driver };

            using (var connection = new GpioConnection(settings, ledGreenPPConfig, ledRedPPConfig, buttonRedPPConfig))
            {
                Console.WriteLine("Starting GPIO connection...");

                //
                // Blinker
                var blinkerCancellationTokenSource = new CancellationTokenSource();
                var blinkerCancellationToken = blinkerCancellationTokenSource.Token;

                var taskBlinker = new Task(
                    () =>
                    {
                        Console.WriteLine("Starting blinker");

                        do
                        {
                            connection.Pins[ledGreenPP].Toggle();

                            /*
                            var value = connection.Pins[ledGreenPP].Enabled;
                            Console.WriteLine("Green led is: " + value);
                            */

                            Thread.Sleep(1000);

                        } while (!blinkerCancellationToken.IsCancellationRequested);

                    },
                    blinkerCancellationToken
                );

                taskBlinker.Start();

                //
                // Button

                // Button events via behavior
                connection.Start(buttonBehavior);

                // custom button event code
                if (buttonHandlerVersion == 3)
                {
                    var ledButtonValue = false;

                    connection.Pins[buttonRedPP].StatusChanged += (object sender, PinStatusEventArgs e) =>
                    {
                        Console.WriteLine("Button value changed to: " + e.Enabled);

                        if (e.Enabled)
                        {
                            ledButtonValue = !ledButtonValue;
                            connection.Pins[ledRedPP].Toggle();
                        }
                    };
                }

                //
                // Exit
                Console.WriteLine("Press ENTER to exit");
                Console.ReadLine();

                //
                // Cleanup
                connection.Stop(buttonBehavior);

                blinkerCancellationTokenSource.Cancel();
            }
        }

    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions