*Everything written here is my personal opinion. I could be wrong. If I'm wrong, correct me.
I tried building a USB device (USB HID keyboard, mouse, gamepad (joystick)) using the small Arduino-compatible machine "Seeeduino XIAO" developed and sold by Seeed.
Note: When you make your own USB input device, for example, when you connect the made device to your PC, you additionally connect to the keyboard and mouse you are currently using.
If there is an unexpected output of the homemade USB device, the output will be entered to the PC as is, etc., so there may be an increase in accidents such as mouse inability to operate or random key input. In the worst case scenario, completely out of control.
If this happens, quickly and calmly unplug the USB cable from your homemade device.
Or add a switch to enable/disable the USB data output of the device to stop immediately when the device gets out of control.
step1・Use the standard features of the Arduino.
For "Arduino", these are the standards for "Arduino Leonardo" and "Arduino Micro" using the ATmega32U4 microcomputer chip with built-in USB capabilities, and the Arduino Zero and M0 series using SAMD21 (ARM Cortex M0+ core). Features provided make the Arduino board available as a USB keyboard and mouse.
The Arduino IDE's Built-in Sketch Samples that use these features work correctly on seeeduino XIAO. I moved. I think it might work.
*When using sketch examples, read the official instructions at least once.
Arduino official website > "Documentation" > "Built-in Examples"
https://www.arduino.cc/en/Tutorial/BuiltInExamples
Note: By the way, the "Rocker Mouse Control" in the "Built-in Example" still has inconsistencies between the wiring diagram and the sketch (program). The sketch uses two switches, one for mouse output on/off and one for left mouse click. The needle size is also different.
You can use the features described in the following references.
・USB keyboard >https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
・USB mouse >https://www.arduino.cc/reference/en/language/functions/usb/ mouse/
Note: If you use Arduino's standard USB features, set the "USB Stack" in the Arduino IDE tool to "Arduino".
step2・Use Arduino's external libraries.
It is a standard feature of the USB-enabled board of the "Arduino", but from the second half of 1.6.x of the Arduino IDE (except 1.7.x), the specification was changed to "PluggableUSB" and organized and improved. be
Since the change to PluggableUSB makes it easier to create USB-related libraries, USB MIDI libraries, USB JoyStick libraries, etc. have been created according to this "PluggableUSB" specification.
However, basically the goal is to use the ATmega32U4 board, and it is often not compatible with SAMD21, so caution and confirmation are required. After that, there's another one called the Arduino HID Project
"Library, which extends the functionality of the Arduino standard USB keyboard and mouse library. This also applies to the March 2021 release of SAMD21. In fact, the Arduino standard USB keyboard library has a specification, and the key ID of the USB keyboard can only output 0 to 115 (0x73). (0x87) and the "/|(-)" key (0x89) have a limitation that cannot be sent even if you want to, and you can make a compatible library of USB HID keyboards, USB HID mice, and USB HID gamepads (DirectInput specification).
step3・Use TinyUSB for arduino.
"TinyUSB" is a universal USB protocol stack for small embedded systems, and "Adafruit TinyUSB Library for Arduino", ported by Adafruit for Arduino compatible boards, is open source development and open to the public.
Since the mechanism of the base part is different from the Arduino standard USB, it is divided into "USB Stack" in the Arduino IDE.
Seeed uses the Seeeduino XIAO USB library and the TinyUSB library for arduino's Adafruit.
step4・Use CircuitPython.
"CircuitPython is an open source programming language for education and beginners that originated from MicroPython. Developed by Adafruit Industries, CircuitPython is a C software implementation of the Python 3 programming language. ”WiKi Than。
Seeeduino XIAO seems to be able to build USB devices using CircuitPython.
From here, we will use the Arduino library called the "Arduino HID Project".
Arduino HID Project
https://github.com/NicoHood/HID
Note: When using the library, set "USB Stack" to "Arduino" in the tools of the Arduino IDE.
If you want to enter the key code directly (USERID of USB HID), write it as follows.
Example 1: Use Japanese keyboard prerequisites to set up when entering "" from Arduino (hardware keyboard layout is 106 or 109)
Keyboard.write(KeyboardKeycode(0x89)); The 0x89 is [¥ |
Example 2: Use Japanese keyboard prerequisites when typing "_" (underscore) from Arduino (hardware keyboard layout is 106 or 109)
Keyboard.press(KEY_LEFT_SHIFT); Press and hold the left move key
Keyboard.press(KeyboardKeycode(0x87)); "10 Keyboard/Keypad Page (0x07)")
Keyboard.releaseAll();
Example 3: When using Japanese keyboard prerequisites to set up "convert" input from Arduino (hardware keyboard layout is 106 or 109)
Keyboard.write(KeyboardKeycode(0x8A)); 0x8A is the Candidate Before Conversion key (see "10 Keyboard/Keypad Page (0x07)" in the USB HID specification "Hut1_12v2.pdf")
Note: USB keyboards have always had hardware layout issues, and the engraving on the top of the keys does not match the characters you type.
Basically, it is generally assumed that a U.S. keyboard is used (for example, 103 or 106).
If this library is not set to "Use American Keyboard" on the PC side, some symbols will be misaligned.
There is a solution to this problem for this library, or rather, I made a corresponding file.
More on that later in another article.
I wrote in the blog post below how to deal with ↓
Connect the "CardKB" of the "M5STACK" to the "Adafruit QT Py - SAMD21" and make it a USB keyboard
http://kato-h.cocolog-nifty.com/khweblog /2021/11/post-6bf792.html
In the Arduino HID Project, you can also enter various media keys.
For example, in setup(),
Consumer.begin();
By adding
Consumer.write(MEDIA_VOLUME_MUTE);
Mute/unmute.
For the available keywords, see here
https://github.com/NicoHood/HID/blob/master/src/HID-APIs/ConsumerAPI.h
*However, whether the key is valid or not depends on the system (operating system, etc.).
・USB mouse
Used as a 5-button mouse with a scroll wheel.
Added "Back" and "Forward".
You can use the following keywords.
MOUSE_LEFT // Left button
MOUSE_RIGHT // Right-click
MOUSE_MIDDLE // Wheel click (middle button)
MOUSE_PREV // Back button (added to this library)
MOUSE_NEXT // Forward button (added to this library)
* Depending on the host system, it may not work properly.
The video below shows the temporary assembly and operational inspection on the breadboard
Switch 1: The switch that stops the USB output when the USB output is out of control.
Switch 2: Left-click
Switch 3: Switch to the wheel operation.
Analog joystick: X, Y analog input to XIAO, center push to right button.
(Because I bought the stitches on the analog stick board, I glued it vertically to the breadboard)
OLED: Used to confirm I2C connectivity
・USB gamepad
Gamepad made using the "Arduino HID Project"
・DirectX specifications
・32 buttons
・4 analog 16-bit
Resolution・2 analog 8-bit resolution
・Simultaneously 2 x 8-way HAT switches (cross key, POV, D-Pad
)? A lot)
* X Input Specification Gamepad cannot be created using this library. In addition, gamepads that can be connected to the XBOX series need to have their own security chips, which is also impossible.
Note: The values to be placed in the analog stick function are signed (-32768 to 32767 for 16bit, zero for center, -128 to 127 for 8bit)
Before that, I was working on the XBOX ONE S controller and the Nintendo Switch Pro controller associated with the USB host library, but
Neither is signed, so in that process, "Arduino HID Project" but if you enter a number with no sign, of course things will go wrong.
It took me a long time to notice.
The video below shows the temporary setup made on the breadboard to check the operation (exactly the same hardware configuration as the mouse test above)
Switch 1: The switch that stops the USB output when the USB output is out of control.
Switch 2: Button 1
Switch 3: Button 2
Analog Joystick: X and Y analog inputs to XIAO, center button 3.
(Because I bought the stitches on the analog stick board, I glued it vertically to the breadboard)
OLED: Used to confirm I2C connectivity
加藤久 WEBLOG
Comments