/// <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; }
/// <summary> /// Execution entry point. /// </summary> public static void Main() { // Gain access to all USB controllers. UsbController[] controllers = UsbController.GetControllers(); HardwareProvider hwProvider = new HardwareProvider(); // Set up all buttons to be monitored. buttons.Up = new InputPort(hwProvider.GetButtonPins(Button.VK_UP), true, Port.ResistorMode.Disabled); buttons.Down = new InputPort(hwProvider.GetButtonPins(Button.VK_DOWN), true, Port.ResistorMode.Disabled); buttons.Left = new InputPort(hwProvider.GetButtonPins(Button.VK_LEFT), true, Port.ResistorMode.Disabled); buttons.Right = new InputPort(hwProvider.GetButtonPins(Button.VK_RIGHT), true, Port.ResistorMode.Disabled); buttons.LeftMouseButton = new InputPort(hwProvider.GetButtonPins(Button.VK_BACK), true, Port.ResistorMode.Disabled); buttons.RightMouseButton = new InputPort(hwProvider.GetButtonPins(Button.VK_HOME), true, Port.ResistorMode.Disabled); buttons.Toggle = new InputPort(hwProvider.GetButtonPins(Button.VK_SELECT), true, Port.ResistorMode.Disabled); buttons.Done = new InputPort(hwProvider.GetButtonPins(Button.VK_MENU), true, Port.ResistorMode.Disabled); // Use the first available USB controller, if it exists. if (controllers.Length < 1) { Debug.Print("No USB controllers exist for this device - we're done."); return; } UsbController UsbPort = controllers[0]; UsbStream mouseStream = null; if (UsbPort.Status == UsbController.PortState.Running) { Debug.Print( "USB controller 0 is up and running - are you debugging with USB?"); Debug.Print( "Make sure your platform supports overriding the debug transport."); Thread.Sleep(500); } try { ConfigureUsbPort(UsbPort, true); mouseStream = UsbPort.CreateUsbStream(3, UsbStream.NullEndpoint); } catch (Exception e) { Debug.Print( "Mouse stream could not be created due to exception " + e.Message); Debug.Print( "Perhaps your native configuration does not contain endpoint 3?"); return; } // Be a mouse until the Done button is pressed. MouseLoop(UsbPort, mouseStream); mouseStream.Close(); }