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