【问题标题】:How to create a virtual/Simulated USB Mouse using raspberry Pi or any other linux PC or device如何使用树莓派或任何其他 linux PC 或设备创建虚拟/模拟 USB 鼠标
【发布时间】:2016-05-15 00:33:03
【问题描述】:

我需要创建一个模拟 USB 设备。插入 PC 或平板电脑时,该设备的行为应类似于 USB 鼠标。动机是检查系统的鼠标驱动程序。我想让其中一个 Raspberry Pi USB 端口像鼠标一样工作。

当我将 Raspberry Pi USB 端口连接到我的 PC 时,它应该显示鼠标已连接。

如何制作这种虚拟/模拟设备?

我还需要监控并向 PC 发送点击消息。

【问题讨论】:

    标签: raspberry-pi embedded usb linux-device-driver embedded-linux


    【解决方案1】:

    要模拟 USB 鼠标,您需要充当 USB HID 设备。 HID 易于实现,许多具有简单 USB 设备端口的微控制器都有足够的资源来实现。甚至还有一个使用 bit-banged IO on an AVR ATTiny 的低速 USB HID 实现,根本没有 USB 硬件。

    问题在于,在典型的基于 Linux 的开发板中,您只有 USB 主机端口。尽管 RPi 核心的 Broadcom SOC 支持 USB OTG 并且原则上可以用作 USB 设备,但大多数 RPi 型号都有这个限制。

    根据RPi Stack Exchangethis answerRPi Stack Exchange,新的RPi模型零板有两个端口正确连接为USB OTG,可以用作设备。

    USB 连接只是第一个障碍。其次,您需要 Linux 内核支持 OTG,以及实现 USB 设备的内核支持。这在 Linux 中被称为“小工具模式”,并且受到 Broadcom SOC 内核的支持,并且可以根据a tutorial at Adafruit 在 RPi0 中使用。有关更多链接和讨论,请参阅linked answer above

    在所有基础设施到位后,您需要使用小工具 API 充当 HID 并发出 HID 报告,这些报告将被理解为鼠标移动。

    【讨论】:

      【解决方案2】:

      PC 和 Raspberry Pi 都是 USB 主机;虽然 USB 鼠标是 USB 设备,但您无法将主机连接到主机。

      您需要带有 USB 设备控制器的硬件,然后实现 HID 设备类。您最好的选择可能是一个简单的微控制器开发板;使用 Linux 系统来展示 HID 设备有点过头了。基于 STM32 的开发板是一种简单的入门方式,例如 ST 的 STM32Cube USB device library 包括 HID 设备类支持。

      【讨论】:

      • 这个答案部分错误;虽然 Pi A/B 型号只能在 USB 主机模式下运行,但 Pi Zero 和 Zero W 实际上可以在 IDB 设备/从机模式下运行。这通常称为“小工具模式”。
      【解决方案3】:

      您只需要一个 4 美元的 HID 模式下的 Raspberry Pi Pico。使用本学习指南:

      https://learn.adafruit.com/circuitpython-essentials/circuitpython-hid-keyboard-and-mouse

      """CircuitPython Essentials HID Mouse example"""
      import time
      import analogio
      import board
      import digitalio
      import usb_hid
      from adafruit_hid.mouse import Mouse
      
      mouse = Mouse(usb_hid.devices)
      
      x_axis = analogio.AnalogIn(board.A0)
      y_axis = analogio.AnalogIn(board.A1)
      select = digitalio.DigitalInOut(board.A2)
      select.direction = digitalio.Direction.INPUT
      select.pull = digitalio.Pull.UP
      
      pot_min = 0.00
      pot_max = 3.29
      step = (pot_max - pot_min) / 20.0
      
      
      def get_voltage(pin):
          return (pin.value * 3.3) / 65536
      
      
      def steps(axis):
          """ Maps the potentiometer voltage range to 0-20 """
          return round((axis - pot_min) / step)
      
      
      while True:
          x = get_voltage(x_axis)
          y = get_voltage(y_axis)
      
          if select.value is False:
              mouse.click(Mouse.LEFT_BUTTON)
              time.sleep(0.2)  # Debounce delay
      
          if steps(x) > 11.0:
              # print(steps(x))
              mouse.move(x=1)
          if steps(x) < 9.0:
              # print(steps(x))
              mouse.move(x=-1)
      
          if steps(x) > 19.0:
              # print(steps(x))
              mouse.move(x=8)
          if steps(x) < 1.0:
              # print(steps(x))
              mouse.move(x=-8)
      
          if steps(y) > 11.0:
              # print(steps(y))
              mouse.move(y=-1)
          if steps(y) < 9.0:
              # print(steps(y))
              mouse.move(y=1)
      
          if steps(y) > 19.0:
              # print(steps(y))
              mouse.move(y=-8)
          if steps(y) < 1.0:
              # print(steps(y))
              mouse.move(y=8)
          
      

      这是使用操纵杆的鼠标的 CP 示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        相关资源
        最近更新 更多