Imagine plugging a USB keyboard into a small box then plugging the small box into a computer. Pressing keys on the keyboard produces MIDI CC messages.
Image plugging a USB keyboard into a small box then plugging the small box into two computers. Pressing keys on the keyboard sends keystrokes to both computers.
The USB metamorph project explores connecting one ore more USB controllers, host and device, in useful ways. The Adafruit Trinket M0 makes it cheap and easy to experiment with multiple USB controllers. The interconnection will use UART for simplicity but SPI and I2C interfaces are available on the Trinket M0. The SAMD21 native USB port is USB On-The-Go (OTG) which means it supports host and device modes. To use USB host mode, all that is needed is a simple cable.
The Arduino USB host documentation refers to Arduino Due with no mention of the Zero/M0 SAMD21 boards. However, the USB host examples for Due work on Trinket M0. The Arduino USB Host is limited to USB keyboard, mouse, and ADK at the moment.
The Trinket M0 is very different from the much older Trinket. The older Trinket does not have USB host capability.
The other Arduino USB host feature is based on the MAX3421E chip that many more drivers for other USB devices but is more complex. This type of USB host is not used in this project.
The Trinket M0 is based on the Atmel SAMD21 chip so other boards based on the same chip probably have the same USB Host features but only the Trinket M0 has been tested.
Examples of SAMD21 boards usable with the Arduino IDE.
- Arduino Zero, Arduino M0, and Arduino M0 Pro
- Arduino MKR series
- SparkFun SAMD21 mini breakout
- Adafruit Feather M0 series, Trinket M0
The following cables are two examples. Similar cables are available from a large number of vendors.
This cable is required to plug in a keyboard or mouse. When this cable is used, serial console output no longer available.
For serial console output, a USB serial cable is highly recommended. Make sure the cable uses 3.3V logic levels and 5V USB power out. The following cables are two examples. Similar cables are available from a large number of vendors.
Plug this into the Trinket M0 UART Tx and Rx for serial console. This cable can also be used to power the Trinket M0 and the keyboard or mouse. Connect the 5V output from the cable to the Trinket M0 USB pin. And connect ground to ground.
Upload and run the example. Unplug the Trinket M0 from the PC. Plug in the OTG USB host cable to the Trinket M0. Plug in the USB keyboard to the OTG USB host cable. Connect the USB serial cable to the PC. This will power up the Trinket M0 and the USB keyboard. Typing on the keyboard should produce output on the serial console.
NOTE: When the Trinket M0 is running in USB host mode, be sure to double click the reset button when you want to upload a new sketch.
USB keyboard > OTG to host cable > SAMD > UART Tx > UART Rx > 32u4 > USB > computer
USB keyboard or numeric keypad USB OTG to host cable SAMD mini board such as Adafruit Trinket M0 32u4 3.3V mini board such as SparkFun Pro Micro or Adafruit Itsy Bitsy Proto board, hookup wire, solder, etc.
32u4 SAMD
==== ====
GND ------- GND
USB 5V ---- USB 5V
TX -------- RX
RX -------- TX
The USB host sketch is named KeyboardHm0 which stands for Keyboard Host for M0. This program receives USB keyboard events from the USB OTG port in host mode and sends them out a UART port.
The USB device sketch is named USBSerPassThruLine. This program receives USB keyboard events from a UART port and sends them out the 32u4 USB device/gadget port.
Swapping keys could be done on the SAMD since it has lots of memory for code and data compared to the 32u4.
Example 1: Swap Windows and ALT keys.
Example 2: Swap Caps Lock and Ctrl.
Example 3: Dvorak key mapping.
A simple variation of the pass through is to send the keyboard events to more than one 32u4 at the same time.
32u4 A SAMD 32u4 B 32u4 C 32u4 ...
====== ==== ====== ====== ========
GND ------- GND --- GND --- GND --- GND
USB 5V ---- USB 5V
TX -------- RX
RX -------- TX ---- RX ---- RX ---- RX
TBD, maybe
TBD, maybe