Wednesday, November 30, 2011

A Netduino Keyboard Simulator

I realized that I haven't posted anything here in awhile...  So, to remedy this, here's my post on a Netduino keyboard simulator.

What this project does is to turn your Netduino into a "Human Interface Device" or HID.  I originally got the idea for this when I needed a fun project for a Halloween blog post at work.  A friend had sent me a link to a similar (but much more focused and useful) project: http://www.irongeek.com/i.php?page=security/programmable-hid-usb-keystroke-dongle

So, in their footsteps, and with some help from a library from the Netduino forums, we built a generic "virtual keyboard."

So, yeah - like hardware based macro keys I guess. :)





Gathering the Parts:


I use SparkFun (and they are great) - but you can probably get all of these parts from a variety of vendors.

Development Environment:

If you don't already have your Netduino dev environment setup, you can go here: http://netduino.com/downloads/ and follow the "getting started" guide to setup your development environment.

Netduino Serial Hardware Setup:

First, connect the ground of the FTDI to the Netduino ground.
Next, hook the FTDI rx pin to the Netduino dio1.
Finally hook the FTDI tx pin to Netduino dio0.

You will also want to hook up an external power source.

Connect a USB to the FTDI.

Connect a USB to the Netduino.

Netduino Serial Setup Software:

(Check your firmware version with MFDeploy - mine didn't need re-flashed.)

Once you get your parts, and your dev environment is all setup - you will need to switch your Netduino to use serial programming (because the debug info is sent over that channel and it will interfere with the USB stuff you are trying to do).

Follow these instructions to do this: http://forums.netduino.com/index.php?showtopic=945

Here's a video from Omar on the Netduino Forum that covers this too:


http://www.youtube.com/user/VCSandARM#p/u/4/-yNC1nU1Nv4

Our Program:

Once you have this setup and tested, you'll want to move on to write your program.  You'll want to download keyboard.cs from this thread (use the last one on the page): http://forums.netduino.com/index.php?/topic/2274-netduino-usb-hid-keyboard-updated-code/

Then create a new "solution" in Visual Studio, target it to your serial interfaced Netduino and add the keyboard.cs to the solution (instructions from Omar in the link above).

Finally, we add our little bit of code for this:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
using Netduino_USB_HID_Library;

namespace Netduino_Touch_Keyboard
{
    public class Program
    {
        public static void Main()
        {

            string setupResult = Keyboard.SetUp();

            Debug.Print(setupResult);

            if (setupResult != "Success")
                return;

            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            led.Write(true);
            Thread.Sleep(1000);
            led.Write(false);
            Thread.Sleep(1000);
            // setup our interrupt port (on-board button)
            // InterruptEdgeLow = Interrupt on button press only (not button up)
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);

            // assign our interrupt handler
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            // go to sleep until the interrupt wakes us (saves power)
            Thread.Sleep(Timeout.Infinite);

        }

        // the interrupt handler
        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            Keyboard.HoldDown(Keyboard.Modifiers.LeftGUI);
            Keyboard.SendChar('r');
            Keyboard.AllUp();
            Keyboard.SendString("notepad.exe");
            Thread.Sleep(2000);
            Keyboard.SendString("\n");
            Thread.Sleep(2000);
            Keyboard.SendString("Hello World!");
        }
    }
}

Compile and deploy this to the Netduino.

Then unplug (if it was already plugged into the USB) and re-plug the USB to the Netduino. This is needed because the app takes a bit to start the first time and the USB driver will fail if you plug it in too quickly - this isn't a problem in "production" since it will be externally powered and running already.

Now press the on-board button.

It should, open up a copy of notepad.exe then write "Hello World!" into it.

References:
Netduino USB HID Code and Example:
http://forums.netduino.com/index.php?/topic/2274-netduino-usb-hid-keyboard-updated-code/
Official thread on beta HID support:
http://forums.netduino.com/index.php?/topic/1514-driverless-pc-netduino-communication-using-usb/
Keyboard class from here:
http://forums.netduino.com/index.php?/topic/2372-netduino-usb-hid-touch-screen-keyboard/