Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a virtual/Simulated USB Mouse using raspberry Pi or any other linux PC or device

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.

like image 319
NIRMAL T M Avatar asked Feb 07 '23 20:02

NIRMAL T M


2 Answers

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.

like image 169
RBerteig Avatar answered Feb 10 '23 10:02

RBerteig


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.

like image 24
Clifford Avatar answered Feb 10 '23 08:02

Clifford