Interim Solution to KeyboardConnected error USBHost Module

The previous post: Interim Solution to Ethernet_J11D Failure led to a Eureka moment for me.  I was running my daily slog, not in the bath like Archimedes, but it occurred to me that other problems might be solved by using the NETMF API provided by GHI Electronics instead of their newer Gadgeteer SDK.  I hit a dead end  with the USBHost module similar to the problem with the Ethernet_J11D module.  In this case the UsbHost.KeyboardConnected event throws an error when using the USBHost module.

The solution for now is to use the USBHost module, but not the .NET Gadgeteer version of the driver.  Plug the module into the mainboard, but don’t put the module in the designer, or you’ll get some strange behavior.  The rest of this is easy.  You’ll need references to GHIElectronics.NETMF.System and GHIElectronics.NETMF.USBHost.

Here is the code:

using Microsoft.SPOT;
using GT = Gadgeteer;
using GHIElectronics.NETMF.USBHost;

namespace Keyboard
{
    public partial class Program
    {
        USBH_Keyboard keyboard;
        uint xLocation;
        uint yLocation;

        void ProgramStarted()
        {
            USBHostController.DeviceConnectedEvent +=
                new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent);
            USBHostController.DeviceDisconnectedEvent +=
                new USBH_DeviceConnectionEventHandler(USBHostController_DeviceDisconnectedEvent);

            xLocation = 25;
            yLocation = 25;

            display.SimpleGraphics.DisplayText("Plug keyboard...",
                Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Blue, 5, 5);
            Debug.Print("Program Started");
        }

        void USBHostController_DeviceDisconnectedEvent(GHIElectronics.NETMF.USBHost.USBH_Device device)
        {
            Debug.Print("Device disconnected...");
            Debug.Print("ID: " + device.ID + ", Interface: " +
                        device.INTERFACE_INDEX + ", Type: " + device.TYPE);

            display.SimpleGraphics.DisplayRectangle(GT.Color.Black, 1, GT.Color.Black, 0, 0, 200, 25);
            display.SimpleGraphics.DisplayText("Device disconnected",
                Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Red, 5, 5);
        }

        void USBHostController_DeviceConnectedEvent(GHIElectronics.NETMF.USBHost.USBH_Device device)
        {
            Debug.Print("Device connected...");
            Debug.Print("ID: " + device.ID + ", Interface: " +
                    device.INTERFACE_INDEX + ", Type: " + device.TYPE);

            if (device.TYPE == USBH_DeviceType.Keyboard)
            {
                keyboard = new USBH_Keyboard(device);
                keyboard.KeyDown += new USBH_KeyboardEventHandler(keyboard_KeyDown);

                display.SimpleGraphics.DisplayRectangle(GT.Color.Black, 1, GT.Color.Black, 0, 0, 200, 25);
                display.SimpleGraphics.DisplayText("Keyboard connected",
                       Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Orange, 5, 5);
            }
        }

        void keyboard_KeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            if (args.Key == USBH_Key.Enter)
            {
                yLocation += 15;
                xLocation = 25;
            }

            if (args.Key == USBH_Key.BackSpace)
            {
                xLocation -= 8;
                display.SimpleGraphics.DisplayRectangle(GT.Color.Black, 1,
                                    GT.Color.Black, xLocation, yLocation, 8, 15);
                return;
            }

            if (xLocation < (display.Width - 25))
            {
                display.SimpleGraphics.DisplayText(args.KeyAscii.ToString(),
                    Resources.GetFont(Resources.FontResources.NinaB),
                    GT.Color.Green, xLocation, yLocation);
                xLocation += 8;
            }
            else
            {
                yLocation = yLocation + 15;
                xLocation = 25;

                display.SimpleGraphics.DisplayText(args.KeyAscii.ToString(),
                    Resources.GetFont(Resources.FontResources.NinaB),
                    GT.Color.Green, xLocation, yLocation);

                xLocation += 8;
            }
        }
    }
}

Advertisement
  1. Soluzione temporanea per l’errore su KeyboardConnected con il modulo USBHost « Integral Design

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: