USB-HID/usb_read_data.cpp

142 lines
3.5 KiB
C++
Raw Normal View History

2024-04-11 12:54:06 +08:00
/******************** (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
********************************************************************************/
2024-04-11 11:22:54 +08:00
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <libusb-1.0/libusb.h>
2024-04-11 12:54:06 +08:00
/**---------------------------------------------------------------------
* Function : print_devs
* Description : usbls 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));
}
}
2024-04-11 11:22:54 +08:00
int main()
{
int r;
ssize_t cnt;
2024-04-11 12:54:06 +08:00
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);
2024-04-18 15:04:09 +08:00
dev_handle = libusb_open_device_with_vid_pid(NULL, 0x0483, 0x5750);
2024-04-11 12:54:06 +08:00
if (dev_handle == NULL)
{
2024-04-11 11:22:54 +08:00
printf("Cannot open device\n");
return 1;
2024-04-11 12:54:06 +08:00
}else{
2024-04-11 11:22:54 +08:00
printf("Device Opened\n");
2024-04-11 12:54:06 +08:00
}
// free the list, unref the devices in it
libusb_free_device_list(devs, 1);
2024-04-11 11:22:54 +08:00
2024-04-11 12:54:06 +08:00
if (libusb_kernel_driver_active(dev_handle, 0) == 1)
{ // find out if kernel driver is attached
2024-04-11 11:22:54 +08:00
printf("Kernel Driver Active\n");
2024-04-11 12:54:06 +08:00
if (libusb_detach_kernel_driver(dev_handle, 0) == 0) // detach it
2024-04-11 11:22:54 +08:00
printf("Kernel Driver Detached!\n");
}
2024-04-11 12:54:06 +08:00
// claim interface 0 (the first) of device (mine had jsut 1)
r = libusb_claim_interface(dev_handle, 0);
if (r < 0){
2024-04-11 11:22:54 +08:00
printf("Cannot Claim Interface\n");
return 1;
}
printf("Claimed Interface\n");
2024-04-11 12:54:06 +08:00
sleep(1);
2024-04-11 11:22:54 +08:00
2024-04-11 12:54:06 +08:00
unsigned char data[64];
2024-04-11 11:22:54 +08:00
int transferred;
2024-04-11 12:54:06 +08:00
// 用于实际判断传输的数据长度
int actual;
2024-04-19 10:48:03 +08:00
data[0] = 0x00;
r = libusb_interrupt_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_OUT), data, 64, &transferred, 1000);
if (r == 0 && actual == 127){
// 解析GeekIMU的磁力计信息
printf("Set Success.\n");
}else{
printf("Set Error.\n");
}
2024-04-11 12:54:06 +08:00
while (1)
{
2024-04-18 15:04:09 +08:00
r = libusb_interrupt_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN), data, 64, &transferred, 1000);
if (r == 0 && actual == 127){
// 解析GeekIMU的磁力计信息
int16_t mx = *(int16_t *)(data + 3);
int16_t my = *(int16_t *)(data + 5);
int16_t mz = *(int16_t *)(data + 7);
printf("mag:%d,%d,%d\n", mx, my, mz);
}else{
2024-04-11 11:22:54 +08:00
printf("Read Error\n");
2024-04-18 15:04:09 +08:00
}
//printf("%i,%i\n", r, actual);
2024-04-11 11:22:54 +08:00
2024-04-11 12:54:06 +08:00
// 睡眠延时100ms
2024-04-18 15:04:09 +08:00
usleep(1000 * 10);
2024-04-11 12:54:06 +08:00
// 清屏
// printf("%s","\033[1H\033[2J");//clear display
}
// 释放 the claimed interface
r = libusb_release_interface(dev_handle, 0);
if (r != 0)
{
2024-04-11 11:22:54 +08:00
printf("Cannot Release Interface\n");
return 1;
}
printf("Released Interface\n");
2024-04-11 12:54:06 +08:00
// 关闭设备
libusb_close(dev_handle);
// 结束时调用
libusb_exit(NULL);
2024-04-11 11:22:54 +08:00
return 0;
2024-04-18 15:04:09 +08:00
}