2421浏览
查看: 2421|回复: 0

[ESP8266/ESP32] USB 鼠标转蓝牙鼠标的装置

[复制链接]
本帖最后由 zoologist 于 2022-8-8 09:04 编辑

ESP32-S3 是乐鑫科技推出的一款 WIFI/蓝牙 MCU,它可以看作是 ESP32-S2 的升级版本,相比S2增加了蓝牙功能,因此我们有机会设计各种蓝牙和USB相关的作品。这次带来的就是能够将 USB 鼠标转为蓝牙鼠标的装置。
电路比较简单,基本上相当于ESP32-S3最小系统引出 USB 接口即可:
USB 鼠标转蓝牙鼠标的装置图3
板子上分别有一个USB公头和母头,公头用于取电,母头用于连接设备:
USB 鼠标转蓝牙鼠标的装置图4
[img=623,710]
程序基本结构是:
1.     USB  Host 负责获得和接收解析鼠标数据
2.     BLeMouse 负责将收到的数据重新发送出去
其中的 USB Host 基于esp32-usb-host-demos-main库。BLE 鼠标基于ESP32-BLE-Mouse-Main库。因为这个库并不支持直接发送 RAW 数据,所以对其进行简单修改,增加了一个函数

  1.   void SendData(unsigned char *Data);
复制代码
代码如下:
  1. void BleMouse::SendData(unsigned char *Data)
  2. {
  3.   if (this->isConnected())
  4.   {
  5.     uint8_t m[5];
  6.     m[0] = Data[0];
  7.     m[1] = Data[1];
  8.     m[2] = Data[2];
  9.     m[3] = Data[3];
  10.     m[4] = 0;
  11.     this->inputMouse->setValue(m, 5);
  12.     this->inputMouse->notify();
  13.   }
  14. }
复制代码
这个 BLEMouse库发送的数据格式是 5字节,分别是鼠标按键,X,Y,垂直滚轮和水平滚轮。我是用的是微软鼠标 IntelliMouseOptical 1.1A,没有水平滚轮。



完整代码如下:
  1. #include <elapsedMillis.h>
  2. #include <BleMouse.h>
  3. #include <usb/usb_host.h>
  4. #include "show_desc.hpp"
  5. #include "usbhhelp.hpp"
  6. BleMouse bleMouse;
  7. bool isMouse = false;
  8. bool isMouseReady = false;
  9. uint8_t MouseInterval;
  10. bool isMousePolling = false;
  11. elapsedMillis MouseTimer;
  12. const size_t Mouse_IN_BUFFER_SIZE = 8;
  13. usb_transfer_t *MouseIn = NULL;
  14. // 对Mouse发送 Setup Pakcage的 usb_transfer
  15. usb_transfer_t *MouseOut = NULL;
  16. void Mouse_transfer_cb(usb_transfer_t *transfer)
  17. {
  18.   if (Device_Handle == transfer->device_handle) {
  19.     isMousePolling = false;
  20.     if (transfer->status == 0) {
  21.       ESP_LOGI("", "HID report: %02x", transfer->actual_num_bytes);
  22.       if (transfer->actual_num_bytes == 0x04) {
  23.         uint8_t *const p = transfer->data_buffer;
  24.         ESP_LOGI("", "HID report: %02x %02x %02x %02x",
  25.             p[0], p[1], p[2], p[3]);
  26.         bleMouse.SendData(p);
  27.       }
  28.       else {
  29.         ESP_LOGI("", "Mouse boot hid transfer too short or long");
  30.       }
  31.     }
  32.     else {
  33.       ESP_LOGI("", "transfer->status %d", transfer->status);
  34.     }
  35.   }
  36. }
  37. void check_interface_desc_boot_Mouse(const void *p)
  38. {
  39.   const usb_intf_desc_t *intf = (const usb_intf_desc_t *)p;
  40.   if ((intf->bInterfaceClass == USB_CLASS_HID) &&
  41.       (intf->bInterfaceSubClass == 1) &&
  42.       (intf->bInterfaceProtocol == 2)) {
  43.     isMouse = true;
  44.     ESP_LOGI("", "Claiming a boot Mouse!");
  45.     esp_err_t err = usb_host_interface_claim(Client_Handle, Device_Handle,
  46.                     intf->bInterfaceNumber, intf->bAlternateSetting);
  47.     if (err != ESP_OK) ESP_LOGI("", "usb_host_interface_claim failed: %x", err);
  48.   }
  49. }
  50. void prepare_endpoint(const void *p)
  51. {
  52.   const usb_ep_desc_t *endpoint = (const usb_ep_desc_t *)p;
  53.   esp_err_t err;
  54.   // must be interrupt for HID
  55.   if ((endpoint->bmAttributes & USB_BM_ATTRIBUTES_XFERTYPE_MASK) != USB_BM_ATTRIBUTES_XFER_INT) {
  56.     ESP_LOGI("", "Not interrupt endpoint: 0x%02x", endpoint->bmAttributes);
  57.     return;
  58.   }
  59.   if (endpoint->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK) {
  60.     err = usb_host_transfer_alloc(Mouse_IN_BUFFER_SIZE, 0, &MouseIn);
  61.     if (err != ESP_OK) {
  62.       MouseIn = NULL;
  63.       ESP_LOGI("", "usb_host_transfer_alloc In fail: %x", err);
  64.       return;
  65.     }
  66.     MouseIn->device_handle = Device_Handle;
  67.     MouseIn->bEndpointAddress = endpoint->bEndpointAddress;
  68.     MouseIn->callback = Mouse_transfer_cb;
  69.     MouseIn->context = NULL;
  70.     isMouseReady = true;
  71.     MouseInterval = endpoint->bInterval;
  72.     ESP_LOGI("", "USB boot Mouse ready");
  73.   }
  74.   else {
  75.     ESP_LOGI("", "Ignoring interrupt Out endpoint");
  76.   }
  77. }
  78. void show_config_desc_full(const usb_config_desc_t *config_desc)
  79. {
  80.   // Full decode of config desc.
  81.   const uint8_t *p = &config_desc->val[0];
  82.   static uint8_t USB_Class = 0;
  83.   uint8_t bLength;
  84.   for (int i = 0; i < config_desc->wTotalLength; i += bLength, p += bLength) {
  85.     bLength = *p;
  86.     if ((i + bLength) <= config_desc->wTotalLength) {
  87.       const uint8_t bDescriptorType = *(p + 1);
  88.       switch (bDescriptorType) {
  89.         case USB_B_DESCRIPTOR_TYPE_DEVICE:
  90.           ESP_LOGI("", "USB Device Descriptor should not appear in config");
  91.           break;
  92.         case USB_B_DESCRIPTOR_TYPE_CONFIGURATION:
  93.           show_config_desc(p);
  94.           break;
  95.         case USB_B_DESCRIPTOR_TYPE_STRING:
  96.           ESP_LOGI("", "USB string desc TBD");
  97.           break;
  98.         case USB_B_DESCRIPTOR_TYPE_INTERFACE:
  99.           USB_Class = show_interface_desc(p);
  100.           check_interface_desc_boot_Mouse(p);
  101.           break;
  102.         case USB_B_DESCRIPTOR_TYPE_ENDPOINT:
  103.           show_endpoint_desc(p);
  104.           if (isMouse && MouseIn == NULL) prepare_endpoint(p);
  105.           break;
  106.         case USB_B_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
  107.           // Should not be config config?
  108.           ESP_LOGI("", "USB device qual desc TBD");
  109.           break;
  110.         case USB_B_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION:
  111.           // Should not be config config?
  112.           ESP_LOGI("", "USB Other Speed TBD");
  113.           break;
  114.         case USB_B_DESCRIPTOR_TYPE_INTERFACE_POWER:
  115.           // Should not be config config?
  116.           ESP_LOGI("", "USB Interface Power TBD");
  117.           break;
  118.         case 0x21:
  119.           if (USB_Class == USB_CLASS_HID) {
  120.             show_hid_desc(p);
  121.           }
  122.           break;
  123.         default:
  124.           ESP_LOGI("", "Unknown USB Descriptor Type: 0x%x", bDescriptorType);
  125.           break;
  126.       }
  127.     }
  128.     else {
  129.       ESP_LOGI("", "USB Descriptor invalid");
  130.       return;
  131.     }
  132.   }
  133. }
  134. void setup()
  135. {
  136.   ESP_LOGI("", "Starting BLE work!");
  137.   bleMouse.begin();
  138.   usbh_setup(show_config_desc_full);
  139. }
  140. void loop()
  141. {
  142.   usbh_task();
  143.   if (isMouseReady && !isMousePolling && (MouseTimer > MouseInterval)) {
  144.     MouseIn->num_bytes = 4;
  145.     esp_err_t err = usb_host_transfer_submit(MouseIn);
  146.     if (err != ESP_OK) {
  147.       ESP_LOGI("", "usb_host_transfer_submit In fail: %x", err);
  148.     }
  149.     isMousePolling = true;
  150.     MouseTimer = 0;
  151.   }
  152. }
复制代码
测试视频

修改后的库:


电路图和 PCB

特别声明:本制作使用立创EDA设计电路图和PCB,其中BOM提及的元件编号属于立创商城。但是这并不表示本人认可、推荐、认同、建议用户使用立创商城所售商品。本人并不保证、承诺任何人根据本涉及购买使用任何立创商城商品复现本作品能够正常工作。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail