USB-HID/usb_read_data.cpp

128 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/******************** (C) COPYRIGHT 2024 UPBot***********************************
* File Name : usb_read_data.cpp
* Current Version : V1.0
* Author : UPBot Group
* Date of Issued : 2024.04.11
* Comments : UWB USB-HID驱动
********************************************************************************/
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <libusb-1.0/libusb.h>
/**---------------------------------------------------------------------
* Function : print_devs
* Description : 列举usb设备信息和ls usb功能差不多
* Date : 2024/04/11 zhanli
*---------------------------------------------------------------------**/
static void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0;
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0)
{
fprintf(stderr, "Failed to get device descriptor.");
return;
}
printf("USB Dev VID = %04x,PID = %04x (Bus %d, Device %d)\n",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
}
int main()
{
int r;
ssize_t cnt;
libusb_device_handle *dev_handle; // a device handle
libusb_device **devs; // devices
// libusb_context **ctx=NULL;
// 初始化libusb
r = libusb_init(NULL);
if (r < 0)
{
printf("failed to init libusb\n");
return 1;
}
// 获取设备列表
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
{
printf("failed to get device list\n");
return 1;
}
// 打印usb设备信息
// print_devs(devs);
dev_handle = libusb_open_device_with_vid_pid(NULL, 0x2833, 0x0002);
if (dev_handle == NULL)
{
printf("Cannot open device\n");
return 1;
}else{
printf("Device Opened\n");
}
// free the list, unref the devices in it
libusb_free_device_list(devs, 1);
if (libusb_kernel_driver_active(dev_handle, 0) == 1)
{ // find out if kernel driver is attached
printf("Kernel Driver Active\n");
if (libusb_detach_kernel_driver(dev_handle, 0) == 0) // detach it
printf("Kernel Driver Detached!\n");
}
// claim interface 0 (the first) of device (mine had jsut 1)
r = libusb_claim_interface(dev_handle, 0);
if (r < 0){
printf("Cannot Claim Interface\n");
return 1;
}
printf("Claimed Interface\n");
sleep(1);
unsigned char data[64];
int transferred;
// 用于实际判断传输的数据长度
int actual;
while (1)
{
r = libusb_bulk_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN), data, 64, &transferred, 1000);
if (r == 0 && actual == 64) // we read the 64 bytes successfully
printf("Read Successful\n");
else
printf("Read Error\n");
printf("%i,%i\n", r, actual);
// 解析GeekIMU的磁力计信息
int16_t mx = *(int16_t *)(data + 56);
int16_t my = *(int16_t *)(data + 58);
int16_t mz = *(int16_t *)(data + 60);
printf("mag:%d,%d,%d\n", mx, my, mz);
// 睡眠延时100ms
usleep(1000 * 100);
// 清屏
// printf("%s","\033[1H\033[2J");//clear display
}
// 释放 the claimed interface
r = libusb_release_interface(dev_handle, 0);
if (r != 0)
{
printf("Cannot Release Interface\n");
return 1;
}
printf("Released Interface\n");
// 关闭设备
libusb_close(dev_handle);
// 结束时调用
libusb_exit(NULL);
return 0;
}