我想提取由单芯片光学鼠标传感器(特别是 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()