4

我想提取由单芯片光学鼠标传感器(特别是 ADNS-2700)捕获的实际图像。与互联网上使用微控制器与成像芯片的 SPI 接口(像这样)通信的各种其他教程相反,我正在尝试使用的芯片集成了一个 USB 接口。

ADNS-2700 数据表

系统:Windows 7、Python2.7、PyUSB 1.0

按照这个例子,我已经成功提取了按钮按下、速度和滚轮:

import usb.core
import usb.util

VENDOR_ID = 6447
PRODUCT_ID = 2326

# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
                       idProduct=PRODUCT_ID)

# use the first/default configuration
device.set_configuration()

# first endpoint
endpoint = device[0][(0,0)][0]

# read a data packet
attempts = 10
data = None
while attempts > 0:
    try:
        data = device.read(endpoint.bEndpointAddress,
                           endpoint.wMaxPacketSize)
        print data

    except usb.core.USBError as e:
        data = None
        if e.args == ('Operation timed out',):
            attempts -= 1
            continue

它提取如下数据:

array('B', [0, 0, 16, 0, 0])
array('B', [0, 0, 240, 255, 0])
array('B', [0, 0, 16, 0, 0])
array('B', [0, 0, 240, 255, 0])

我需要帮助提取原始图像数据!

我是一个 USB 菜鸟,这可能是导致大部分问题的原因。

在数据表的第 18 页,有一个 USB 命令列表。看起来很有希望的是:

Mnemonic            Command                    Notes
---------------------------------------------------------------
Get_Vendor_Test     C0 01 00 00 xx 00 01 00    Read register xx

然后在第 28 页,有一个看起来很有希望的寄存器列表:

Address     Register Name    Register Type    Access       Reset Value
----------------------------------------------------------------------
0x0D        PIX_GRAB         Device           Read only    0x00

但是,我尝试过:

device.write(endpoint.bEndpointAddress,'C0:01:00:00:0A:00:01:00',0)

这导致:

usb.core.USBError: [Errno None] libusb0-dll:err [_usb_setup_async] invalid endpoint 0x81

也:

device.read(endpoint.bEndpointAddress, 0x0D)

这只是超时。


完整的解决方案:

import usb.core
import usb.util
import matplotlib.pyplot as plt
import numpy as np

VENDOR_ID = 6447
PRODUCT_ID = 2326

# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
                       idProduct=PRODUCT_ID)

# use the first/default configuration
device.set_configuration()


# In order to read the pixel bytes, reset PIX_GRAB by sending a write command
response = self.device.ctrl_transfer(bmRequestType = 0x40, #Write
                                     bRequest = 0x01,
                                     wValue = 0x0000,
                                     wIndex = 0x0D, #PIX_GRAB register value
                                     data_or_wLength = None
                                     )

# Read all the pixels (360 in this chip)
pixList = []
for i in range(361):
    response = self.device.ctrl_transfer(bmRequestType = 0xC0, #Read
                                         bRequest = 0x01,
                                         wValue = 0x0000,
                                         wIndex = 0x0D, #PIX_GRAB register value
                                         data_or_wLength = 1
                                         )
    pixList.append(response)

pixelArray = np.asarray(pixList)
pixelArray = pixelArray.reshape((19,19))

plt.imshow(pixelArray)
plt.show()
4

2 回答 2

4

您可能需要执行 ctrl_transfer() ,如pyUSB 教程中所示。

您还需要将数据表中的十六进制字节转换为单独的参数到 ctrl_transfer。有关格式,请参阅此页面

Get_Vendor_Test C0 01 00 00 xx 00 01 00可以通过 ctrl_transfer() 调用发出,如下所示:

ret = dev.ctrl_transfer(bmRequestType=0xc0, # byte[0]
                        bRequest=0x01,      # byte[1]
                        wValue=0x0000,      # byte[2,3]
                        wIndex=register,    # byte[4,5]
                        data_or_wLength = 1)# byte[6,7]
于 2014-04-23T00:26:49.927 回答
1

我只是自己尝试了这段代码,其中有一个相当大的错误。ADNS-2700 的数据表指出,没有设置最高有效位的响应值是无效的。所以循环不应该精确地进行 361 次读取,而是应该读取直到它获得 361 个有效像素,丢弃任何没有设置“有效”位的字节。

我做了一些其他的小改动,比如在将返回的数组添加到列表之前对其进行解包,并从返回值中删除 MSb,因为它不代表实际的像素数据。

我已经使用 Amazon Basics USB 鼠标测试了这段代码,它输出了一个有效的图像。

import usb.core
import usb.util
import matplotlib.pyplot as plt
import numpy as np

VENDOR_ID = 0x04F2
PRODUCT_ID = 0x0939

# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
                       idProduct=PRODUCT_ID)

# use the first/default configuration
device.set_configuration()


# In order to read the pixel bytes, reset PIX_GRAB by sending a write command
response = device.ctrl_transfer(bmRequestType = 0x40, #Write
                                     bRequest = 0x01,
                                     wValue = 0x0000,
                                     wIndex = 0x0D, #PIX_GRAB register value
                                     data_or_wLength = None
                                     )

# Read all the pixels (360 in this chip)
pixList = []

while len(pixList) < 361:
    temp = 0
    response = device.ctrl_transfer(bmRequestType = 0xC0, #Read
                                         bRequest = 0x01,
                                         wValue = 0x0000,
                                         wIndex = 0x0D, #PIX_GRAB register value
                                         data_or_wLength = 1
                                         )
    if response[0] >= 0x80:
        temp = response[0] & 0x7F
        pixList.append(temp)

pixelArray = np.asarray(pixList)
pixelArray = pixelArray.reshape((19,19))

plt.imshow(pixelArray)
plt.show()
于 2018-06-22T16:34:36.343 回答