InputProvider.RaiseButton Help

Hi Guys,

I’ve created a class that derives from GHIElectronics.TinyCLR.UI.Window and overridden the OnButtonDown and OnButtonUp methods but neither of them seems to be getting called when I feed in events via InputProvider.RaiseButton.

Any ideas on what i’m doing wrong?

Window code:

using System.Diagnostics;
using GHIElectronics.TinyCLR.UI;
using GHIElectronics.TinyCLR.UI.Input;

namespace RaiseButtonProblem
{
    public class MainWindow : Window
    {
        override protected void OnButtonUp(ButtonEventArgs e)
        {
            // never gets called...

            Debug.WriteLine("OnButtonUp");
        }

        override protected void OnButtonDown(ButtonEventArgs e)
        {
            // never gets called...

            Debug.WriteLine("OnButtonDown");
        }
    }
}

Main application code:

using System;
using GHIElectronics.TinyCLR.UI;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.UI.Input;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Display;

namespace RaiseButtonProblem
{
    public class Program : Application
    {
        private GpioPin _gpioPin1;

        Program(DisplayController d) : base(d)
        {
        }

        public static void Main()
        {
            var displayController = GetDisplayController();

            var program = new Program(displayController);

             // Updated as per Johns suggestion

            var mainWindow = new MainWindow();

            mainWindow.Visibility = Visibility.Visible; 

            Buttons.Focus(mainWindow);

            program.Run(mainWindow);
        }

        override protected void OnStartup(EventArgs e)
        {
            var gpioController = GpioController.GetDefault();

            _gpioPin1 = gpioController.OpenPin(G120.GpioPin.P2_10);

            _gpioPin1.SetDriveMode(GpioPinDriveMode.InputPullDown);

            _gpioPin1.ValueChanged += _gpioPin1_ValueChanged;

            base.OnStartup(e);
        }

        private void _gpioPin1_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            // Debugger shows that event is being
            // raised here when the button is pressed.

            InputProvider.RaiseButton(
                HardwareButton.Up,
                e.Edge == GpioPinEdge.RisingEdge,
                e.Timestamp);
        }

        private static DisplayController GetDisplayController()
        {
            var displayController = DisplayController.GetDefault();

            var settings = new ParallelDisplayControllerSettings
            {
                Width = 800,
                Height = 480,
                DataFormat = DisplayDataFormat.Rgb565,
                PixelClockRate = 30 * 1000 * 1000,
                PixelPolarity = false,
                DataEnablePolarity = true,
                DataEnableIsFixed = false,
                HorizontalFrontPorch = 40,
                HorizontalBackPorch = 88,
                HorizontalSyncPulseWidth = 48,
                HorizontalSyncPolarity = false,
                VerticalFrontPorch = 13,
                VerticalBackPorch = 32,
                VerticalSyncPulseWidth = 3,
                VerticalSyncPolarity = false
            };

            displayController.SetConfiguration(settings);

            displayController.Enable();

            return displayController;
        }
    }
}

Try calling Buttons.Focus, passing in your MainWindow object. Doing this in Main before calling program.Run is a good spot.

Thanks John,

I really thought that was going to work but i’m still getting the same result. Any other ideas?

Note sure if this is relevant but i’m also getting blue instead of red when I set Background = new SolidBrush(Colors.Red);

Mono

So the call to focus is needed. This case is just one of the exceptions. Focus only sets the focus if the target element is currently visible, otherwise it’s a nop. The window is not visible by default and is not set to visible until the call to Run. So our call to Focus came too early. The easiest way forward is to set mainWindow.Visibility to Visibility.Visible before the call to Focus.

Regarding the color swap, that is a known issue that has already been fixed for the next release. Thanks for the catch though!

Thanks John,

That fixed it … Everything is now working as expected.

I guess it makes sense that you cannot set the focus to a non-visible element.