Main Site Documentation

USBHostController_DeviceDisconnectedEvent in 4.3?


#1

Migrating from 4.2 to 4.3 i’m able to map:

USBHostController_DeviceConnectedEvent -> Controller_DeviceConnected
USBHostController_DeviceBadConnectionEvent -> Controller_DeviceConnectFailed

but i don’t find a event for: USBHostController_DeviceDisconnectedEvent

How do you handle a USB device that is removed?


#2

Did you find USB_Device? What namespaces are you using? I didn’t find it either.


#3

The 2 events i found in : GHI.Usb.Host.Controller.


#4

Did you find GHI.Usb.Device?


#5

Yes, there is a EventHandler but no Event?


#6

@ David@ Emrol - The Disconnected event is now part of the connected device itself: GHI.Usb.Device.Disconnected.


#7

@ John - How “should” this be used? Not documented yet and 0 results on the forum.

I can start digging but i ported my main project code from 4.2 to 4.3 and have another 280 errors to look at… so if anyone has used this yet :wink:


#8

using System.Threading;
using GHI.Usb.Host;
using GHI.Usb;
using Microsoft.SPOT;


public class Program
{
    static Mouse mouse;

    public static void Main()
    {

        // Subscribe to USBH event.
        Controller.DeviceConnected += Controller_DeviceConnected;

        // Sleep forever
        Thread.Sleep(Timeout.Infinite);
    }

    static void Controller_DeviceConnected(object sender, Controller.DeviceConnectedEventArgs e)
    {
        if (e.Device.Type == Device.DeviceType.Mouse)
        {
            Debug.Print("Mouse Connected");
            mouse = new Mouse(e.Device);
            mouse.CursorMoved += mouse_CursorMoved;
            mouse.ButtonChanged += mouse_ButtonChanged;
        }

    }

    static void mouse_CursorMoved(Mouse sender, Mouse.CursorMovedEventArgs e)
    {
        Debug.Print("(x, y) = (" + e.NewPosition.X + ", " +
                    e.NewPosition.Y + ")");
    }
} 

#9

@ Gus, GHI.USB.Device does not exist. So Device.DeviceType.Mouse does not compile.


#10

using System.Threading;
using GHI.Usb.Host;
using GHI.Usb;
using Microsoft.SPOT;


public class Program
{
    static Mouse mouse;

    public static void Main()
    {

        // Subscribe to USBH event.
        Controller.DeviceConnected += Controller_DeviceConnected;

        // Sleep forever
        Thread.Sleep(Timeout.Infinite);
    }

    static void Controller_DeviceConnected(object sender, Controller.DeviceConnectedEventArgs e)
    {
        if (e.Device.Type == Device.DeviceType.Mouse)
        {
            Debug.Print("Mouse Connected");
            mouse = new Mouse(e.Device);
            mouse.CursorMoved += mouse_CursorMoved;
            mouse.ButtonChanged += mouse_ButtonChanged;
            e.Device.Disconnected += Device_Disconnected;
        }

    }

    static void Device_Disconnected(Device sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    static void mouse_CursorMoved(Mouse sender, Mouse.CursorMovedEventArgs e)
    {
        Debug.Print("(x, y) = (" + e.NewPosition.X + ", " +
                    e.NewPosition.Y + ")");
    }
} 

#11

@ Mr. John Smith - you need both hardware and USB assemblies.


#12

@ Gus, sorry it’s in the library GHI.Hardware


#13

@ Gus - Thanks, at first it seems not logical why these where separated but i can see the logic behind it now, thanks again.