USBHost Module on Spider, How to initialize device without DeviceConnectedEvent

Hi,
Trying to get a common keyboard \ mouse connected to my Spider through a GHI USB host module works when i use example code from the tutorials section. Only thing is that alle examples on the web use the DeviceConnectedEvent to initialize a connection to the device it self.


  keyb = new USBH_Keyboard(device);
debug.print("id:"+ device.ID)
  keyb.KeyDown += new USBH_KeyboardEventHandler(keyb_KeyDown);

Problem is that, to define the keyboard (keyb), i need to tell the initialisation command which "device"it is, and the device variable is provided by the DeviceConnectedEvent .

When i remove and connect the keyboard from the usb port , the event fires and all is okay, but when i reboot the FEZ, i want to use the keyboard without reconnecting it again…

an idea if it can be done (and if yes, then HOW?)

regards Bert

@ andre.m - Thanks Andre

enumerating the HOST interface actually triggers the DeviceConnectedEvent . So this means that my program works as intented, now finding my device at startup, and also when plugged in afterwards. Thanks again.

One addition for others that search on this Post :

When used with gadgeteer (Spider in my case), and the Premium USB-host is attached in the designer, Key interrupts are intermittantly picked up correctly, (and probably other devices will also not work optimal). This is fixed by NOT adding the Premium USB-host in the designer, but add the references

using GHI.Premium.System;
using GHI.Premium.USBHost;

manually.

You actually have to add the reference to these dll’s in the Solution explorer as well…

I shall add a listing of my code that detects \ redetects Mouse or Keyboard plugged in on the GHI USG-Host Module, and shows mouse and keyboard output on my T35 display.

Il clean it up first and post it on the original thread.
regards
Bert

As promised : an example that works on a FEZ spider with USB-Host Moudule

Setup :
[ul]Spider
GHI-USBHOST module
T35 Module[/ul]

This Code detects e Keyboard or Mouse on the USB-Host Module, when (UN)plugged , but also at startup if the device is already in the USB-Port.
Functions tested an displayed on the T35 display:
[ul]Device Detection (plugging\Unplugging)
Keyboard characters entered
Barcode Scanner reading
Catching the Enter Key(s) from keyboard
Mouse movement
Mouse scaling (to limits of T35 screen bounds = 320x240)
Mouse button actions
Mouse Scrolling[/ul]

using System; //Needed
using System.Collections;
using System.Threading; //Needed
using Microsoft.SPOT; //Needed
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHI.Premium.System;  //Added manually
using GHI.Premium.USBHost;  //Added manually
using GT = Gadgeteer; //Needed
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace GadgeteerApp1
{
    public partial class Program
    {
        static Font small = Resources.GetFont(Resources.FontResources.small);
        static GT.Color White = GT.Color.White;
        static GT.Color Cyan = GT.Color.Cyan;
        static GT.Color Green = GT.Color.Green ;
        static GT.Color Red = GT.Color.Red ;
        static GT.Color Yellow = GT.Color.Yellow;
        static int MouseWheelPos;
        static USBH_Mouse mouse;
        static USBH_Keyboard keyb;
        static string keyBuf;
        static uint DisplayRow;
        static string btnPressed;
    
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            USBHostController.DeviceConnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent);
            USBHostController.DeviceDisconnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceDisconnectedEvent);
            USBH_Device[] usbdevice = USBHostController.GetDevices(); // THIS Triggers the DeviceConnected event 
         }
       
        void USBHostController_DeviceDisconnectedEvent(USBH_Device device)
        {
            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("USB - Device Disconnected", small, Cyan, 5, 5);
        }

        void USBHostController_DeviceConnectedEvent(USBH_Device device)
        {
            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("ID:........." + device.ID, small, Red, 5, 5);
            display.SimpleGraphics.DisplayText("Interface .." + device.INTERFACE_INDEX.ToString(), small, Cyan, 5, 15);
            display.SimpleGraphics.DisplayText("Port:......." + device.PORT_NUMBER.ToString(), small, Green, 5, 25);
            display.SimpleGraphics.DisplayText("Prod.ID:...." + device.PRODUCT_ID.ToString(), small, Yellow, 5, 35);
            display.SimpleGraphics.DisplayText("Type:........" + device.TYPE.ToString(), small, White, 5, 45);
            display.SimpleGraphics.DisplayText("Vendor:......" + device.VENDOR_ID.ToString(), small, Green, 5, 55);
            Thread.Sleep(2000); // Just to display Device info for 2 seconds 
            display.SimpleGraphics.Clear();
            keyb = new USBH_Keyboard(device); // *** see commment below
            keyb.KeyDown += new USBH_KeyboardEventHandler(keyb_KeyDown); // *** see commment below
            int val = Convert.ToInt32(device.TYPE.ToString());  //What type of USB device is found
            switch (val)
            {
                case 2:  // means Keyboard is connected
                    display.SimpleGraphics.DisplayText("Keyboard Found", small, Cyan, 5, 5);
                    keyBuf = ">";// empty keyboard buffer
                    DisplayRow = 250;
                    // It looks logical to put lines (marked ***) here in stead of outside the switch section, but the keyboard wont be attached here.
                    // it is found, but the keyboard leds dont light, and intterupts dont fire....
                    // anybody that knows WHY....Please let me know... b.otte@ alodata.nl 
                    break;
                case 3: // means Mouse is connected
                    mouse = new USBH_Mouse(device);
                    mouse.MouseMove += new USBH_MouseEventHandler(mouse_MouseMove);
                    mouse.MouseDown += new USBH_MouseEventHandler(mouse_MouseDown);
                    mouse.MouseWheel += new USBH_MouseEventHandler(mouse_MouseWheel);
                    display.SimpleGraphics.DisplayText("Mouse Found", small, Green, 1, 0);
                    mouse.SetCursorBounds(0, 320, 0, 240);
                    break;
               }
            // TODO : Get multiple devices on a USB-HUB detected and defined.....
            }
    
       // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        void keyb_KeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            int a = Convert.ToInt32(args.Key.ToString());
            if (a == 40||a == 88) //check for enter (code 40) or (88 = numkey-enter)
            {
                if (DisplayRow > 230)
                {
                    DisplayRow = 0;
                    display.SimpleGraphics.Clear();
                }
                display.SimpleGraphics.DisplayText(keyBuf, small, Red, 0, DisplayRow);
                keyBuf = "";
                DisplayRow = DisplayRow + 10;
                }
            else  // dont show the ENTER character
               {
                keyBuf = keyBuf + args.KeyAscii;
               }
        }

        void mouse_MouseWheel(USBH_Mouse sender, USBH_MouseEventArgs args)
        {
            display.SimpleGraphics.Clear();
            int mwp = Convert.ToInt32(args.DeltaPosition.ScrollWheelValue.ToString());
            if (mwp > 0) // mwp is the number of pixels moved since last scrollinterrupt
            {
                MouseWheelPos = MouseWheelPos + mwp;
                display.SimpleGraphics.DisplayText("Scroll Up :  " + MouseWheelPos, small, Yellow, 5, 150);
            }
            else
            {
                MouseWheelPos = MouseWheelPos - mwp;
                display.SimpleGraphics.DisplayText("Scroll Down :  " + MouseWheelPos, small, Yellow, 5, 150);
            }
        }

        void mouse_MouseDown(USBH_Mouse sender, USBH_MouseEventArgs args)
        {
            display.SimpleGraphics.Clear();
            int btn = Convert.ToInt32(args.ChangedButton.ToString());
            switch (btn)
            {
                case 0:
                    btnPressed = "Left Button";
                    break;
                case 1:
                    btnPressed = "Right Button";
                    break;
                case 2:
                    btnPressed = "Mouse Wheel pressed";
                    break;
            }
                display.SimpleGraphics.DisplayText(btnPressed, small, Yellow, 5, 150);
          }

        void mouse_MouseMove(USBH_Mouse sender, USBH_MouseEventArgs args)
        {
            display.SimpleGraphics.Clear();
            uint xPos = Convert.ToUInt32(sender.Cursor.X.ToString());
            uint yPos = Convert.ToUInt32(sender.Cursor.Y.ToString());
            display.SimpleGraphics.SetPixel(White, xPos + 1, yPos);
            display.SimpleGraphics.SetPixel(White, xPos + 2, yPos);
            display.SimpleGraphics.SetPixel(White, xPos - 1, yPos);
            display.SimpleGraphics.SetPixel(White, xPos - 2, yPos);
            display.SimpleGraphics.SetPixel(White, xPos, yPos - 1);
            display.SimpleGraphics.SetPixel(White, xPos, yPos - 2);
            display.SimpleGraphics.SetPixel(White, xPos, yPos + 1);
            display.SimpleGraphics.SetPixel(White, xPos, yPos + 2);
            display.SimpleGraphics.SetPixel(Red, xPos, yPos);
        }
    }
}

regards
Bert

@ Breto62 We really liked your example, so I added it to the USB Host tutorial at https://www.ghielectronics.com/docs/36/usb-host. If you’d like me to take it down, just let me know.