我看到1.7版本的新增了鼠标功能,能接在USB0上吗?

Viewed 105

问题描述



我用的是图上的开发板,镜像也是图上这个版本,在USB0上通过TYPE-C转USB-A接鼠标,万用表测输出有4.9V硬件上应该处于host状态,代码如最底下,我测试后发现鼠标灯不亮,串口3一直输出“waiting for USB mouse: open failed, -2”,所以我想问2个问题
1:USB0能做为host接鼠标吗?还是只能接电脑调试用
2:现在这个镜像能自动识别如GL850G或CH334等USB扩展芯片吗?如果USB0不能接鼠标,我想把USB1扩展下接USB转以太网和鼠标,现在镜像能不能支持
感谢解答疑问

import time
import usb # 导入底层的 usb 模块
from usb import Mouse
from machine import UART

初始化串口3 (波特率设为115200)

uart3 = UART(3, 115200)

设备启动时,在串口3输出 "hello"

uart3.write("hello\r\n")

def open_mouse():
mouse = Mouse(timeout_ms=1000, auto_reconnect=True)

while True:
    try:
        mouse.open()
        info = mouse.info()
        
        # 鼠标接上时,在串口3打印就绪信息
        msg = "USB mouse ready: %s\r\n" % str(info)
        uart3.write(msg)
        
        # 打印按键掩码信息
        btn_msg = "button masks: L=%d, R=%d, M=%d\r\n" % (
            Mouse.BTN_LEFT_MASK, 
            Mouse.BTN_RIGHT_MASK, 
            Mouse.BTN_MIDDLE_MASK
        )
        uart3.write(btn_msg)
        
        return mouse
    except OSError as err:
        # 等待鼠标接入时,也在串口3打印状态
        uart3.write("waiting for USB mouse: %s\r\n" % str(err))
        time.sleep_ms(500)

mouse = open_mouse()

while True:
frame = mouse.read(1000)
if not frame:
continue

moved = frame["has_rel"] or frame["has_abs"]
button_edge = frame["pressed_mask"] or frame["released_mask"]
wheel_move = frame["wheel"] or frame["hwheel"]

if not moved and not button_edge and not wheel_move:
    continue

# 格式化鼠标数据,并通过串口3输出
log_msg = (
    "buttons=%d pressed=%d released=%d "
    "rel=(%d,%d) abs=(%d,%d) wheel=(%d,%d)\r\n" % (
        frame["buttons"],
        frame["pressed_mask"],
        frame["released_mask"],
        frame["rel_x"],
        frame["rel_y"],
        frame["abs_x"],
        frame["abs_y"],
        frame["wheel"],
        frame["hwheel"],
    )
)
uart3.write(log_msg)


## 复现步骤
----------
![](/uploads/post/5NT2xDvYVE1.jpg)

## 硬件板卡
----------
CanMV K230 V3.0

## 软件版本
----------
CanMV_K230_V3P0_micropython_v1.7-rc1-10-g9d4fd1d_nncase_v2.11.0.img.gz
1 Answers

你好,V3.0的板子USB1接了以太网卡,现在的usb协议栈只允许存在1个host,因此在V3.0的板子的USB0不能用鼠标。

这样的限制不太合理吧,后续有考虑修改吗

你好,后续没有修改的计划,可以自行定制,把usb0从usb device切为host,然后把usb1关掉

好吧,那就没办法了,我需要的是USB1接太网,USB0接鼠标同时使用