Soluzione temporanea per l’errore su KeyboardConnected con il modulo USBHost (Italiano)

By Mike Dodaro. Translated by Marco Minerva from the original English version.

Come illustrato nel post Soluzione temporanea Soluzione temporanea per il corretto funzionamento del modulo Ethernet_J11D, diversi problemi possono essere risolti utilizzando direttamente le NETMF API fornite da GHI Electronics invece del più recente Gadgeteer SDK. Provando ad utilizzare il modulo USBHost, ad esempio, si incorre in un problema simile a quello del modulo Ethernet_J11D. In questo caso, si tratta di un errore lanciato dall’evento UsbHost.KeyboardConnected.

Anche in questo caso, in attesa di un aggiornamento ufficiale, la soluzione è utilizzare il modulo USBHost senza i relativi driver .NET  Gadgeteer. In fase di progettazione, collegate il modulo alla mainboard, ma non trascinatelo nel designer, altrimenti si otterranno strani comportamenti. Il resto è facile; ecco il codice:

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. Leave a comment

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: