I need to create a simulated USB device. The device should behave like a USB mouse when plugged in to a PC or tablet. The motive is to check the mouse driver of the system. I would like to make one of the Raspberry Pi USB ports act like a mouse.
When I connect that Raspberry Pi USB port to my PC, it should show a mouse is connected.
How do I make this kind of virtual/simulated device?
Also I need to monitor and send click messages to the PC.
To emulate a USB mouse, you need to act as a USB HID device. HID is easy to implement, and many microcontrollers with simple USB device ports have sufficient resources to do it. There is even an implementation of low-speed USB HID using bit-banged IO on an AVR ATTiny with no USB hardware at all.
The problem is that in a typical Linux-based board, you have only USB host ports. Most models of the RPi have that restriction, even though the Broadcom SOC at the core of the RPi supports USB OTG and in principle can be used as a USB device.
According to this answer at the RPi Stack Exchange, the new RPi model zero board has two ports properly wired as USB OTG and can be used as a device.
The USB connection is only the first hurdle. Second, you need Linux kernel support for OTG, and kernel support for implementing a USB device. This is known in Linux as "Gadget Mode", and is supported by the kernel for the Broadcom SOC, and can be used in the RPi0 according to a tutorial at Adafruit. See the linked answer above for more links and discussion.
With all that infrastructure in place, you then need to use the gadget API to act as a HID and issue HID reports that will be understood as mouse motion.
The PC and the Raspberry Pi are both USB hosts; while a USB mouse is a USB device you cannot connect a host to a host.
You need hardware with a USB device controller and then implement the HID device class. Your best bet is probably a simple microcontroller development board; Using a Linux system to present an HID device is a bit over-the-top. An STM32 based board is an easy way to get started, ST's STM32Cube USB device library includes HID device class support for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With