The Pro Trinket's USB connector is used for uploading sketches, but the connector is not a full USB port due to lack of dedicated hardware.  You can, though, use the connection to emulate some basic USB 1.1 devices via an Arduino software library presented in this tutorial. For example, via the USB 1.1 protocol, you can build low speed USB Human Interface Devices (or HID). Examples of HIDs are keyboards, mice, joysticks, gamepads, etc.

Previous Pro Trinket HID tutorials:

Pro Trinket can emulate a both a computer mouse and keyboard at the same time. The code is a bit larger but adds the functionality of the previous two libraries.

This tutorial will show use of the library using a rotary encoder switch as a digital volume knob and mute switch for multimedia PCs.

Software Library

The Pro Trinket HID library is available on GitHub. You can download the code from the link below.  See the tutorial All About Arduino Libraries on installing the code into your Arduino Integrated Development Environment (IDE) for your use.

The functions available in the library are shown below.  All of the definitions are in the ProTrinketHidCombo.h file in the library.  There are definitions for the common PC keyboard keys including control and shift variants.  Special to this library are multimedia and special keys.

The mouse part of the library differs from the Pro Trinket HID mouse library in the moveMouse function provides movement and mouse clicks but does not emulate the scroll wheel.  The arguments are the x and y values to move the mouse and whether the buttons are to be pressed - the predefined values for mouse presses are also in the .h file.

// Starts the USB driver, place in the Arduino IDE setup routine:
void TrinketHidCombo.begin(); // start the USB device engine and enumerate

TrinketHidCombo.isConnected(); // checks if USB is connected, 0 if not connected
// this (or "press" something) must be called at least once every 10ms
TrinketHidCombo.poll();  // this (or "press" something) must be called at least once every 10ms
// presses up to 6 keys, and modifiers (modifiers are keys like shift, CTRL, etc)
TrinketHidCombo.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6);
TrinketHidCombo.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5);
TrinketHidCombo.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4);
TrinketHidCombo.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3);
TrinketHidCombo.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2);
TrinketHidCombo.pressKey(uint8_t modifiers, uint8_t keycode1);
// presses a list of keys
TrinketHidCombo.pressKeys(uint8_t modifiers, uint8_t* keycodes, uint8_t sz);
// type out a single ASCII character
TrinketHidCombo.typeChar(uint8_t ascii);

// Press special keyboard key types
TrinketHidCombo.pressMultimediaKey(uint8_t key);
TrinketHidCombo.pressSystemCtrlKey(uint8_t key);
// returns the state of the three LEDs on a keyboard (caps/num/scroll lock)
// inherit from "Print", these two write functions are implemented
size_t val = TrinketHidCombo.write(uint8_t);
using Print::write;
// other "print" and "println" functions are automatically available
using Print::print;
using Print::println;
// helps translate ASCII characters into keycode and modifier combinations, 
// while taking into account whether or not caps lock is on
ASCII_to_keycode(uint8_t ascii, uint8_t ledState, uint8_t* modifier, uint8_t* keycode);

// Here is the mouse function. This is a bit different from the Pro Trinket mouse library 
//     as this does not have the scroll wheel argument.
//  Pass the following values:
//  x - the number of pixels to move in the horizontal direction (-127 to 127)
//      Note: this is in relation to the current mouse position, not absolute 
//      screen location
//  y - the pixels to move in the vertical direction (-127 to 127)
//  buttonMask - the following values may be ANDed to press mouse buttons:
//      MOUSEBTN_LEFT_MASK		0x01  // Left button
//      MOUSEBTN_RIGHT_MASK		0x02  // Right buttom
//      MOUSEBTN_MIDDLE_MASK    0x04  // Middle Button
void TrinketHidCombo.mouseMove(signed char x, signed char y, uint8_t buttonMask); // makes a mouse movement, must be called at least once every 10ms, even if no movement
// Examples: 
TrinketHidCombo.mouseMove(5,-5,0);  // Move 5 pixels up, 5 left from current location
TrinketHidCombo.move(0,0,MOUSEBTN_LEFT_MASK); // Click left mouse button

// See the file ProTrinketHidCombo.h in the library for all the possible
//    predefined key codes.  Some special codes available:
// multimedia keys
// MMKEY_VOL_UP			    0xE9
// MMKEY_STOP				0xB7
// MMKEY_MUTE				0xE2
// MMKEY_KB_EXECUTE		    0x74
// MMKEY_KB_HELP			0x75
// MMKEY_KB_MENU			0x76
// MMKEY_KB_STOP			0x78
// MMKEY_KB_AGAIN			0x79
// MMKEY_KB_CUT			    0x7B
// system control keys

This guide was first published on Jan 28, 2015. It was last updated on Jan 28, 2015.

This page (Overview) was last updated on Jan 27, 2015.

Text editor powered by tinymce.