“USB鼠标报告中的X分量始终为0”

huangapple go评论62阅读模式
英文:

X component in USB mouse report is always 0

问题

我对USB协议还不熟悉,但我最终成功上传了ESP32S3板上的HOST USB HID示例项目。我希望它能够与USB鼠标通信。示例项目似乎适用于键盘,但对于鼠标,它只响应水平移动。然而,水平移动会更改报告的Y分量,也许这是问题的线索?

我查看了Espressif的示例代码,但没有看到明显的错误。这里是终端输出的示例:X: 000000 Y: 003992 | | |。当连接到计算机时,鼠标可以正常工作,因此我认为我们可以排除任何硬件问题。

如果我修改代码,使所有报告都计入一个变量,Y分量为0的报告都计入另一个变量,然后尽量向上移动鼠标(我猜是X方向,因为Y是左右),如果我这样做,那么我会得到Y分量为0的非零数量的报告。事实上,我可以获得大约50%的Y分量为0的报告;

我认为鼠标正在发送数据,但代码在某个地方丢弃了数据。否则,为什么鼠标会报告移动呢?如果我不移动鼠标,那么就没有报告,所以它不会随意发送报告。此外,如果我在Y轴上移动它,那么几乎没有Y为0的报告。也许这是一个线索。

是否有人有关于如何获取鼠标移动的X和Y分量的建议?

英文:

I am new to the USB protocol, and I just finally have been able to upload the HOST USB HID example project for a ESP32S3 board. I want it to be able to communicate with a USB mouse. The example project seems to be fine with keyboards, but for a mouse, it only responds to horizontal movements. However, horizontal movements change the Y component of the report, perhaps this is a clue to the issue?

I have looked through Espressif's example code and I don't see any obvious errors. Here's an example of the terminal output X: 000000 Y: 003992 | | |. The mouse works fine when connected to a computer, so I think we can rule out any hardware issues.

If I modify the code so that I count all reports in one variable, and all reports where the Y component is 0 in another variable, and then move the mouse as straight as I can upward (the X direction I guess because the Y is left and right), If I do those things then I get a non-zero number of reports where the Y component is 0. In fact, I can get about 50% of reports where the Y component is 0;

I think that the mouse is sending the data, but the code is throwing out the data somewhere. Why else would the mouse report movement if there was no movement. If I don't move the mouse, then there are no reports, so its not just sending reports willy nilly. Also, if I move it in the Y, then there are almost no reports where the Y is 0. Perhaps this is a clue.

Does anyone have advice on how to get both x and y components of the mouse's movements?

答案1

得分: 1

Short answer: 重要数据以每隔一个字节的间隔排列,因此请修改第314行的代码,以搜索第0字节以获取按钮点击,第2字节以获取X位置,第4字节以获取Y位置。

x_pos += (int8_t)data[2];
y_pos += (int8_t)data[4];

Long answer: 关于Y数据左右移动的现象是一个线索。HID协议指出报告以3个8位字节的数组形式传输,第一个字节表示按钮按下情况,第二个字节表示X轴变化,第三个字节表示Y轴变化。鼠标按钮在数组的开头工作,而"Y"数据在数组的"末尾"工作。因此,数据并不是不完整传输的,否则只会收到2个字节的数据。

(值得注意的是,"Y"数据在向右移动时范围为0-20,向左移动时范围为230-255。这是合理的,因为它是一个带符号的8位整数)

我开始怀疑是否在报告的3个字节之外还有其他数据。我读取了data[3],如果向右移动,得到的值是0,如果向左移动,得到的值是255。这是真正的Y轴数据,data[2]是真正的X轴数据。

我认为这可能是由于几个因素造成的,也许我测试的所有鼠标都是某种超级精密的16位鼠标,以每次8位以小端模式发送。这会解释其他数字如何与0xffFA(-5)和0x0005(5)对齐。

此外,我注意到其中一个引用库中的结构hid_mouse_input_report_boot_t具有(packed)属性。我不太熟悉这个属性,但它似乎是编译器试图将所有字节紧密压缩在一起。因此,也许程序的其他部分没有将它们打包得那么紧密,这就是它们不匹配的原因之一。我不确定。

希望这对某人有帮助(或者至少对未来的我有帮助)。

英文:

Short answer: The important data was spaced out every other byte, so modify the code on line 314 to search for the 0th byte for the button clicks, 2nd byte for the x position, and 4th byte for the Y position
<s>

x_pos += mouse_report-&gt;x_displacement;&lt;br&gt;
y_pos += mouse_report-&gt;y_displacement;

</s>

x_pos += (int8_t)data[2];
y_pos += (int8_t)data[4];

Long answer: The Y data being left and right was a clue. The HID protocol says that the report comes in an array of 3 eight bit bytes and the first byte is the button presses, the second byte is the change in X and the third byte is the change in Y. The buttons were working on my mouse which is at the beginning of the array, and the "Y" was working at the "end" of the array. So it's not like the data is being sent incompletely like if it lobbed off the end, then it would have just been 2 bytes received.

(it is worth noting the "Y" would range from 0-20 if going right and 230-255 if going left. This makes sense since it is a signed 8 bit integer)

I wondered about if there was any other data lurking beyond the 3 bytes that were in the report. I read data[3] and got 0 if I was going right, and 255 if I was going left.
hmmm....strange behavior. What else is lurking in the shadows. I then read the next byte data[4] and got a value that changed when moving up and down. This is the true Y and data[2] is the true X.

I think this is probably due to a few things, perhaps all of my mice that I tried this out on were some sort of super fancy 16 bit mice or something that send 8 bits at a time in little endian mode. This would make sense because the other numbers would line up with 0xffFA for negative 5 and 0x0005 for positive 5 for example.

Also, I noticed one of the referenced libraries, the struct hid_mouse_input_report_boot_t has an attribute (packed). I'm not very familiar with that, but it looks like the compiler tries to really squish all the bytes together. So maybe some other part of the program doesn't pack it all up so close and thats why they mismatch or something. IDK.

I hope this goes on to help someone (or at least future me)

huangapple
  • 本文由 发表于 2023年6月9日 10:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定