Main Site Documentation

Reading keyboard input is uselessly slow with USB host module and FEZ Spider!


#1

Recently, I ordered a USB Host module for my FEZ Spider. I tested some basic code to read keyboard input, but it’s way too slow :frowning: . It also doesn’t detect half of my keystrokes when I type at a standard speed!

I get the same results when I test with two different keyboards. Can anybody help me out?

using GHI.Premium.System;
using GHI.Premium.USBHost;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace ReadKeyboard
{
    public partial class Program
    {
		private USBH_Keyboard _keyboard;

        void ProgramStarted()
        {
            Debug.Print("Program Started");
			USBHostController.DeviceBadConnectionEvent += device => Debug.Print("Failed to connect.");
			USBHostController.DeviceConnectedEvent += UsbHostControllerOnDeviceConnectedEvent;
			USBHostController.DeviceDisconnectedEvent += UsbHostControllerOnDeviceDisconnectedEvent;
        }

		private void UsbHostControllerOnDeviceConnectedEvent(USBH_Device device)
		{
			if (device.TYPE == USBH_DeviceType.Keyboard)
			{
				Debug.Print("Keyboard connected");
				_keyboard = new USBH_Keyboard(device);
				_keyboard.KeyDown += KeyboardOnKeyDown;
			}
			else
			{
				Debug.Print("Other device connected");
			}
		}

		private void KeyboardOnKeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
		{
			if (args.KeyAscii == 0) //no ASCII rep
			{
				Debug.Print("Non-ASCII char pressed");
			}
			else
			{
				Debug.Print(args.KeyAscii + " pressed");
			}
		}

		private void UsbHostControllerOnDeviceDisconnectedEvent(USBH_Device device)
		{
			Debug.Print("Device disconnected");
			_keyboard.KeyDown -= KeyboardOnKeyDown;
		}
    }
}


#2

I’ve replaced the KeyDown event handler with this:

private void KeyboardOnKeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
		{
			if (args.KeyAscii == 'g')
			{
				Debug.Print("Typed");
			}
     }

It doesn’t seem to fix the problem though. I tried to type a few sentences with each one containing “g” one time, but my program only picked it up half of the time.


#3

@ interopbyt - Sometimes Debug.Print can be pretty slow. To make sure that isn’t the issue, every time the event is raised increment an integer and in another thread or timer, periodically (every 5-10 seconds) print the value.


#4

Okay, so this time I added a timer which prints the keystroke count every ten seconds. I typed “hello world” between every interval.

Here’s the output:

[quote]
Program Started
Keyboard connected
UsbHost ERROR : USB device is not supported by the Gadgeteer driver. More devices are supported by the GHI USB Host driver. Remove the USB Host from the designer, and proceed without using Gadgeteer code.
The thread ‘’ (0x3) has exited with code 0 (0x0).
8
7
8
3
5[/quote]

Updated code:

using GHI.Premium.System;
using GHI.Premium.USBHost;
using Microsoft.SPOT;

namespace ReadKeyboard
{
	public partial class Program
	{
		private USBH_Keyboard _keyboard;
		private int _keystrokes;

		void ProgramStarted()
		{
			Debug.Print("Program Started");
			USBHostController.DeviceBadConnectionEvent += device => Debug.Print("Failed to connect.");
			USBHostController.DeviceConnectedEvent += UsbHostControllerOnDeviceConnectedEvent;
			USBHostController.DeviceDisconnectedEvent += UsbHostControllerOnDeviceDisconnectedEvent;

			var timer = new Gadgeteer.Timer(10000); //10 secs
			timer.Tick += timer1 =>
			{
				Debug.Print(_keystrokes.ToString());
				_keystrokes = 0;
			};
			timer.Start();
		}

		private void UsbHostControllerOnDeviceConnectedEvent(USBH_Device device)
		{
			if (device.TYPE == USBH_DeviceType.Keyboard)
			{
				Debug.Print("Keyboard connected");
				_keyboard = new USBH_Keyboard(device);
				_keyboard.KeyDown += KeyboardOnKeyDown;
			}
		}

		private void KeyboardOnKeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
		{
			_keystrokes++;
		}

		private void UsbHostControllerOnDeviceDisconnectedEvent(USBH_Device device)
		{
			Debug.Print("Device disconnected");
			_keyboard.KeyDown -= KeyboardOnKeyDown;
		}
	}
}

I don’t think it’s because of the UsbHost error, because 1) the code recognizes the keyboard at the beginning and 2) another one of my keyboards don’t cause the UsbHost error, but the program still cannot capture its keystrokes properly. It’s probably just a misc. device that the keyboard is also acting as.

The faster I type, the faster the loss rate. :frowning: Why is this happening?


#5

Debug your application using mfdeploy instead of vs…
Launch mfdeploy choose USB choose your board then file>> connect and start typing…