// This class maps GPIOs to Buttons processable by Microsoft.SPOT.Presentation
        public GPIOButtonInputProvider(PresentationSource source)
        {
            // Set the input source.
            this.source = source;
            // Register our object as an input source with the input manager and get back an
            // InputProviderSite object which forwards the input report to the input manager,
            // which then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);
            // Create a delegate that refers to the InputProviderSite object's ReportInput method
            callback = new ReportInputCallback(site.ReportInput);
            Dispatcher = Dispatcher.CurrentDispatcher;

            // Allocate button pads and assign the (emulated) hardware pins as input
            // from specific buttons.
            ButtonPad[] buttons = new ButtonPad[]
            {
                // Associate the buttons to the pins as setup in the emulator/hardware
                new ButtonPad(this, Button.Left  , Meridian.Pins.GPIO6),
                new ButtonPad(this, Button.Right , Meridian.Pins.GPIO8),
                new ButtonPad(this, Button.Up    , Meridian.Pins.GPIO5),
                new ButtonPad(this, Button.Select, Meridian.Pins.GPIO7),
                new ButtonPad(this, Button.Down  , Meridian.Pins.GPIO9),
            };

            this.buttons = buttons;
        }
        /// <summary>
        /// Maps GPIOs to Buttons processable by Microsoft.SPOT.Presentation.
        /// </summary>
        /// <param name="source"></param>
        public GPIOButtonInputProvider(PresentationSource source)
        {
            // Set the input source.
            this.source = source;
            
            // Register the object as an input source with the input manager and 
            // get back an InputProviderSite object.  The InputProviderSite 
            // object forwards the input report to the input manager.  The input 
            // manager then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);

            // Create a delegate that refers to the InputProviderSite object's 
            // ReportInput method.
            callback = new DispatcherOperationCallback(delegate(object report)
                    {
                        InputReportArgs args = (InputReportArgs)report;
                        return site.ReportInput(args.Device, args.Report);
                    });
            Dispatcher = Dispatcher.CurrentDispatcher;

            // Create a hardware provider.
            HardwareProvider hwProvider = new HardwareProvider();

            // Create the pins that are needed for the buttons.  Default their 
            // values for the emulator.
            Cpu.Pin pinLeft = Cpu.Pin.GPIO_Pin0;
            Cpu.Pin pinRight = Cpu.Pin.GPIO_Pin1;
            Cpu.Pin pinUp = Cpu.Pin.GPIO_Pin2;
            Cpu.Pin pinSelect = Cpu.Pin.GPIO_Pin3;
            Cpu.Pin pinDown = Cpu.Pin.GPIO_Pin4;

            // Use the hardware provider to get the pins.  If the left pin is 
            // not set, assume none of the pins are set and set the left pin 
            // back to the default emulator value.
            if ((pinLeft = hwProvider.GetButtonPins(Button.VK_LEFT)) ==
                Cpu.Pin.GPIO_NONE)
                pinLeft = Cpu.Pin.GPIO_Pin0;
            else
            {
                pinRight = hwProvider.GetButtonPins(Button.VK_RIGHT);
                pinUp = hwProvider.GetButtonPins(Button.VK_UP);
                pinSelect = hwProvider.GetButtonPins(Button.VK_SELECT);
                pinDown = hwProvider.GetButtonPins(Button.VK_DOWN);
            }

            // Allocate button pads and assign the (emulated) hardware pins as 
            // input from specific buttons.
            ButtonPad[] buttons = new ButtonPad[]
            {
                // Associate the buttons with the pins as discovered or set 
                // above.
                new ButtonPad(this, Button.VK_LEFT  , pinLeft  ),
                new ButtonPad(this, Button.VK_RIGHT , pinRight ),
                new ButtonPad(this, Button.VK_UP    , pinUp    ),
                new ButtonPad(this, Button.VK_SELECT, pinSelect),
                new ButtonPad(this, Button.VK_DOWN  , pinDown  ),
            };

            this.buttons = buttons;
        }
        // This class maps GPIOs to Buttons processable by Microsoft.SPOT.Presentation
        public GPIOButtonInputProvider(PresentationSource source)
        {
            // Set the input source.
            this.source = source;
            // Register our object as an input source with the input manager and get back an
            // InputProviderSite object which forwards the input report to the input manager,
            // which then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);
            // Create a delegate that refers to the InputProviderSite object's ReportInput method
            callback = new ReportInputCallback(site.ReportInput);
            Dispatcher = Dispatcher.CurrentDispatcher;

            // Create a hardware provider
            HardwareProvider hwProvider = new HardwareProvider();

            // Create the pins we will need for the buttons
            // Default their values for the emulator
            Cpu.Pin pinLeft = Cpu.Pin.GPIO_Pin0;
            Cpu.Pin pinRight = Cpu.Pin.GPIO_Pin1;
            Cpu.Pin pinUp = Cpu.Pin.GPIO_Pin2;
            Cpu.Pin pinSelect = Cpu.Pin.GPIO_Pin3;
            Cpu.Pin pinDown = Cpu.Pin.GPIO_Pin4;

            // Use the hardware provider to get the pins
            // If the left pin is not set then assume none of them are
            // and set the left pin back to the default emulator value
            if ((pinLeft = hwProvider.GetButtonPins(Button.VK_LEFT)) == Cpu.Pin.GPIO_NONE)
                pinLeft = Cpu.Pin.GPIO_Pin0;
            else
            {
                pinRight = hwProvider.GetButtonPins(Button.VK_RIGHT);
                pinUp = hwProvider.GetButtonPins(Button.VK_UP);
                pinSelect = hwProvider.GetButtonPins(Button.VK_SELECT);
                pinDown = hwProvider.GetButtonPins(Button.VK_DOWN);
            }

            // Allocate button pads and assign the (emulated) hardware pins as input
            // from specific buttons.
            ButtonPad[] buttons = new ButtonPad[]
            {
                // Associate the buttons to the pins as discovered or set above
                new ButtonPad(this, Button.VK_LEFT  , pinLeft),
                new ButtonPad(this, Button.VK_RIGHT , pinRight),
                new ButtonPad(this, Button.VK_UP    , pinUp),
                new ButtonPad(this, Button.VK_SELECT, pinSelect),
                new ButtonPad(this, Button.VK_DOWN  , pinDown),
            };

            this.buttons = buttons;
        }
        public GPIOButtonInputProvider(PresentationSource source, ButtonDefinition[] ButtonDefinitions)
        {
            this.RepeatDelay = 500;
            this.RepeatPeriod = 150;
            this.PresentationSource = source;
            this.InputSite = InputManager.CurrentInputManager.RegisterInputProvider(this);
            this.ReportInputFunc = new DispatcherOperationCallback( ReportInputCallback );

            this.Dispatcher = Microsoft.SPOT.Dispatcher.CurrentDispatcher;
            this.Buttons = new GpioButtonHandler[ButtonDefinitions.Length];
            for( int i = 0; i < ButtonDefinitions.Length; i++ )
            {
                ButtonDefinition btnDef = ButtonDefinitions[i];
                this.Buttons[i] = new GpioButtonHandler( this, btnDef);
            }
        }
        /// <summary>
        /// Creates new instance of input provider
        /// </summary>
        /// <param name="source">Presentation source</param>
        /// <param name="controller">TV Remote Receiver controller driver</param>
        /// <param name="address">Address of device. Use -1 for everything.</param>
        /// <param name="buttons">Command to WPF button maping</param>
        public TVRemoteInputProvider(PresentationSource source, TVRemoteReceiver controller, int address, TVRemoteButtonPad[] buttons)
        {
            // Set the input source.
            this.source = source;
            // Register our object as an input source with the input manager and get back an
            // InputProviderSite object which forwards the input report to the input manager,
            // which then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);
            // Create a delegate that refers to the InputProviderSite object's ReportInput method
            callback = new ReportInputCallback(site.ReportInput);
            Dispatcher = Dispatcher.CurrentDispatcher;

            deviceAddress = address;
            this.buttons = buttons;

            ButtonAction = RawButtonActions.ButtonUp;

            controller.DataReceived += new TVRemoteReceiver.TVRemoteDataHandler(controller_DataReceived);
        }
        /// <summary>
        ///     Registers an input provider with the input manager.
        /// </summary>
        /// <param name="inputProvider">
        ///     The input provider to register.
        /// </param>
        public InputProviderSite RegisterInputProvider(object inputProvider)
        {
            VerifyAccess();

            // Create a site for this provider, and keep track of it.
            InputProviderSite site = new InputProviderSite(this, inputProvider);

            int idx = _inputProviders.IndexOf(inputProvider);

            if (idx < 0)
            {
                _inputProviders.Add(inputProvider);
                _inputProviderSites.Add(site);
            }
            else
            {
                // NOTE -- should we dispose the old one?
                _inputProviders[idx]     = inputProvider;
                _inputProviderSites[idx] = site;
            }

            return(site);
        }
        // This class maps GPIOs to Buttons processable by Microsoft.SPOT.Presentation
        public GPIOButtonInputProvider(PresentationSource source)
        {
            // Set the input source.
            this.source = source;
            // Register our object as an input source with the input manager and get back an
            // InputProviderSite object which forwards the input report to the input manager,
            // which then places the input in the staging area.
            site = InputManager.CurrentInputManager.RegisterInputProvider(this);
            // Create a delegate that refers to the InputProviderSite object's ReportInput method
            callback = new ReportInputCallback(site.ReportInput);
            Dispatcher = Dispatcher.CurrentDispatcher;

            // Allocate button pads and assign the (emulated) hardware pins as input 
            // from specific buttons.
            ButtonPad[] buttons = new ButtonPad[]
            {
                // Associate the buttons to the pins as setup in the emulator/hardware
                new ButtonPad(this, Button.VK_LEFT  , (Cpu.Pin)86),
                new ButtonPad(this, Button.VK_RIGHT , (Cpu.Pin)87),
                new ButtonPad(this, Button.VK_UP    , (Cpu.Pin)88),
                new ButtonPad(this, Button.VK_SELECT, (Cpu.Pin)89),
                new ButtonPad(this, Button.VK_DOWN  , (Cpu.Pin)90),

                // For a hardware board that has predefined buttons,  can use the 
                // Microsoft.SPOT.Hardware.GetButtonPin to retrieve the GPIO pin number associate 
                // with button name
                //      new ButtonPad(this, Button.Left  , hwProvider.GetButtonPins(Button.Left)),
                //      new ButtonPad(this, Button.Right , hwProvider.GetButtonPins(Button.Right)),
                //      new ButtonPad(this, Button.Up    , hwProvider.GetButtonPins(Button.Up)),
                //      new ButtonPad(this, Button.Select, hwProvider.GetButtonPins(Button.Select)),
                //      new ButtonPad(this, Button.Down  , hwProvider.GetButtonPins(Button.Down)),
                
                
            };

            this.buttons = buttons;
        }
        /// <summary>
        ///     Registers an input provider with the input manager.
        /// </summary>
        /// <param name="inputProvider">
        ///     The input provider to register.
        /// </param>
        public InputProviderSite RegisterInputProvider(object inputProvider)
        {
            VerifyAccess();

            // Create a site for this provider, and keep track of it.
            InputProviderSite site = new InputProviderSite(this, inputProvider);

            int idx = _inputProviders.IndexOf(inputProvider);
            if (idx < 0)
            {
                _inputProviders.Add(inputProvider);
                _inputProviderSites.Add(site);
            }
            else
            {
                // NOTE -- should we dispose the old one?
                _inputProviders[idx] = inputProvider;
                _inputProviderSites[idx] = site;
            }

            return site;
        }